This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseOperation.js
62 lines (50 loc) · 1.68 KB
/
baseOperation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const createApi = () => {}; // Axios/Ajax/etc.
const handleError = () => {}; // Your default error handler func
export class Operation {
constructor(requestAction, receiveAction) {
this.requestAction = requestAction;
this.receiveAction = receiveAction;
}
dispatch = () => (dispatch) => {
this.onRequest(dispatch, this.requestAction);
const api = this.createApiService();
return this.getApiPromise(api)
.then(response => this.onGetResponse(response))
.then(data => this.onSucceed(dispatch, this.receiveAction, this.getSucceedData(data)))
.catch(err => this.onFailed(dispatch, this.receiveAction, err));
}
getId() {
let seedId = "";
let hash = "";
for (let key in this) {
if (this.hasOwnProperty(key) && typeof this[key] !== 'function') {
seedId += this[key];
}
}
for (let i = 0; i < seedId.length; i++) {
let chr = seedId.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
}
createApiService() {
return createApi();
}
onGetResponse(response) {
return response.data;
}
onRequest(dispatch, requestAction) {
dispatch(requestAction());
}
onSucceed(dispatch, receiveAction, data) {
return dispatch(receiveAction('success', data));
}
onFailed(dispatch, receiveAction, errors) {
return dispatch(receiveAction('fail', handleError(errors)));
}
getSucceedData(raw) {
return raw;
}
getApiPromise(api) { }
}