/
storeWithAPIActions.ts
128 lines (110 loc) · 3.03 KB
/
storeWithAPIActions.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
import { ModuleStore, IApiAction } from '../src';
import IAction from '@src/src/interfaces/IAction';
// CREATING STORE
/* tslint:disable */
interface IFetchErrorResponse {
status: number;
message: string;
}
interface IFetchListResponse {
data: string[];
}
interface IFetchItemResponse {
data: string;
}
interface IState {
list: string[];
currentItem: null | string;
errorMsg: null | string;
}
type IActions = {
fetchList: IApiAction<[], [IFetchListResponse], [IFetchErrorResponse]>;
fetchItem: IApiAction<[number], [IFetchItemResponse], [IFetchErrorResponse]>;
reset: [];
};
const moduleName = 'TEST_MODULE_NAME';
const initialState: IState = {
list: [],
currentItem: null,
errorMsg: null,
};
const moduleStore = new ModuleStore<IState, IActions>(
moduleName,
{
fetchList: {
request: state => state,
success: (state, action) => ({ ...state, list: action.payload[0].data }),
failure: (state, action) => ({
...state,
errorMsg: action.payload[0].message,
}),
},
fetchItem: {
request: state => state,
success: (state, action) => ({ ...state, item: action.payload[0].data }),
failure: (state, action) => ({
...state,
errorMsg: action.payload[0].message,
}),
},
reset: () => initialState,
},
initialState,
{
// custom selectors
errorMsg: (state, store) =>
state.errorMsg // also `store[moduleName].errorMsg`
? `${state.errorMsg} is error`
: null,
},
);
// STORE USING
const dispatch = (action: IAction) => console.log(action);
dispatch(moduleStore.actions.fetchList.request());
// { payload: [], type: 'TEST_MODULE_NAME/fetchList.request' }
dispatch(
moduleStore.actions.fetchList.success({
data: ['test', 'best'],
}),
);
// { payload: [ { data: ['test', 'best'] } ], type: 'TEST_MODULE_NAME/fetchList.success' }
dispatch(
moduleStore.actions.fetchList.failure({
message: 'Incorrect',
status: 499,
}),
);
// { payload: [ { message: 'Incorrect', status: 499 } ], type: 'TEST_MODULE_NAME/fetchList.failure' }
dispatch(moduleStore.actions.fetchItem.request(3));
// { payload: [ 3 ], type: 'TEST_MODULE_NAME/fetchItem.request' }
dispatch(
moduleStore.actions.fetchItem.success({
data: 'test',
}),
);
// { payload: [ { data: 'test' } ], type: 'TEST_MODULE_NAME/fetchItem.success' }
dispatch(
moduleStore.actions.fetchItem.failure({
message: 'Incorrect',
status: 499,
}),
);
// { payload: [ { message: 'Incorrect', status: 499 } ], type: 'TEST_MODULE_NAME/fetchItem.failure' }
dispatch(moduleStore.actions.reset());
// { payload: [], type: 'TEST_MODULE_NAME/reset' }
console.log(
moduleStore.selectors({
[moduleName]: { ...initialState, errorMsg: 'bad auth' },
}),
);
// { list: [], currentItem: null, errorMsg: 'bad auth is error' }
console.log(
moduleStore.reducer(
initialState,
moduleStore.actions.fetchList.success({
data: ['test'],
}),
),
);
// { list: ['test'], currentItem: null, errorMsg: null }
const { actions, reducer, selectors } = moduleStore;