-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseReduxStore.ts
229 lines (214 loc) · 7.83 KB
/
BaseReduxStore.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import * as _ from 'lodash';
import {NgRedux} from '@angular-redux/store';
import {
asyncFailureAction,
asyncInitialState,
asyncRequestAction,
asyncSuccessAction,
nonAsyncFailureAction,
nonAsyncInitialState,
nonAsyncRequestAction
} from './action-types';
import {Router} from '@angular/router';
export interface ReduxActionType {
request: string;
success: string;
failure: string;
}
export class ReduxAction {
constructor(action, actionTypes, reducer) {
this.action = action;
this.actionTypes = actionTypes;
this.reducer = reducer;
}
action: any;
actionTypes: ReduxActionType;
reducer: any;
}
export class ReduxParameters {
/* storeIndex will be where the reducer will store your desired data (loading/error/data)
If null, will just go to global scope
Example Values:
{storeIndex: 'currentDistributor'}
{storeIndex: null} <-- in this case, you can just omit storeIndex entirely
*/
storeIndex?: string;
/* actionData will be whatever action function you are desiring
Possible values: function returning a promise, normal function, or just data
Example Values:
{actionData: axios.get('/url')}
{actionData: (data)=>{return {someData:data}}
{actionData: someVariableData}
*/
actionData: any;
/*
routeOnFinish will automatically route after the action has finished (will be _SUCCESS for async)
routeOnError will automatically route if the action has failed (any time it reaches _FAILURE)
Example Values
{routeOnFinish: '/home'}
{routeOnError: '/error-page'}
*/
routeOnFinish: string;
routeOnError: string;
successParam?: string;
}
export abstract class BaseReduxStore {
constructor(protected ngRedux: NgRedux<any>, protected router: Router) {
}
reduxBuilder(actionName: string,
actionFunction,
async: boolean = false,
successReducer: Function = null,
requestReducer: Function = null,
failureReducer: Function = null): ReduxAction {
const actionRequest = actionName + (async ? '_REQUEST' : '');
const actionSuccess = actionName + '_SUCCESS';
const actionFailure = actionName + '_FAILURE';
const initialState = async ? asyncInitialState : nonAsyncInitialState;
const reducer = (state = initialState, action) => {
//Init failure Action before the switch statement, so that we can use it if either our Request/Success reducers fail
let failureAction: Function = async ? asyncFailureAction : nonAsyncFailureAction;
if (!_.isNil(failureReducer)) {
failureAction = failureReducer;
}
try {
switch (action.type) {
case actionRequest:
let requestAction: Function = async ? asyncRequestAction : nonAsyncRequestAction;
if (!_.isNil(requestReducer)) {
requestAction = requestReducer;
}
//I added this case, because I kept thinking, I want to overload my reducer action when its finished "on success"
//However there is no success state for non-async actions so I added this for when you accidentally used a success reducer
if (!_.isNil(successReducer) && _.isNil(requestReducer) && !async) {
requestAction = successReducer;
}
return requestAction(state, action);
case actionSuccess: //Success action only applies to ASYNC actions
let successAction: Function = asyncSuccessAction;
if (!_.isNil(successReducer)) {
successAction = successReducer;
}
return successAction(state, action);
case actionFailure:
return failureAction(state, action);
default:
return state;
}
} catch (e) {
console.error(`Error in Reducer ${action.type}. Data:`, {error: e, action: action});
action.error = e.message;
if (action.type = actionFailure) {
//If the error is with the failure function, we just use a default error reducer instead
failureAction = async ? asyncFailureAction : nonAsyncFailureAction;
}
return failureAction(state, action);
}
};
// Not using arrow function, because need args binding here
const action = function () {
const args = arguments;
let storeIndex;
try {
//First we get our action response, should be formatted like the following:
/* { storeIndex, actionData} */
let actionResponse: ReduxParameters = actionFunction.apply(null, args);
storeIndex = actionResponse.storeIndex || null; //StoreIndex is where the data will be stored
let actionData = actionResponse.actionData;
let routeOnFinish = actionResponse.routeOnFinish;
let routeOnError = actionResponse.routeOnError;
let successParam = actionResponse.successParam;
if (!_.isNil(actionData)) {
// If it's an async promise, then we wait for the promise before checking success/failure
if (typeof actionData.then === 'function') { //.then is a function means a promise
this.ngRedux.dispatch({
type: actionRequest,
storeIndex: storeIndex
});
actionData.then(data => {
//Async request is successful
this.ngRedux.dispatch({
type: actionSuccess,
storeIndex: storeIndex,
newRoute: routeOnFinish,
successParam:successParam,
data
});
}
)
//Caught async error
.catch(error => {
this.ngRedux.dispatch({
type: actionFailure,
storeIndex: storeIndex,
error
});
});
} else {
// Action returned is non-async function
let data = actionData; //The format can either be a function or just an object
if (typeof actionData === 'function') {
data = actionData(); //Evaluate function if this is the case
}
this.ngRedux.dispatch({
type: actionRequest,
newRoute: routeOnFinish,
successParam:successParam,
storeIndex: storeIndex,
data
});
}
} else { //The data returned is null
this.ngRedux.dispatch({
type: actionRequest,
newRoute: routeOnFinish,
successParam:successParam,
storeIndex: storeIndex,
data: null
});
}
// If its not a promise, we really dont want to see "_SUCCESS" just return the try/catch error results
} catch (error) {
console.log(error.message);
this.ngRedux.dispatch({
type: actionFailure,
storeIndex: storeIndex,
error: error.message
});
}
}.bind(this);
return new ReduxAction(
action,
{
request: actionRequest,
success: actionSuccess,
failure: actionFailure
},
reducer);
};
masterReducer(state, action) {
if (_.isNil(state)) {
state = {};
}
for (let key of Object.keys(this)) {
let prop = this[key];
if (_.isNil(prop)) {
continue;
}
if (prop instanceof ReduxAction) {
if (Object.values(prop.actionTypes).find(type => type === action.type)) {
let newState = prop.reducer(state, action);
if (!_.isNil(action.storeIndex)) {
newState = {[action.storeIndex]: newState};
}
if (!_.isNil(action.newRoute)) {
this.router.navigateByUrl(action.newRoute);
}
return _.cloneDeep(_.merge(state, newState));
}
}
}
//All else failing we will leave state the same as before
return state;
}
}