1
|
|
|
import { |
2
|
|
|
PAGE_LOCAL, |
3
|
|
|
PAGE_REMOTE, |
4
|
|
|
SET_DATA, |
5
|
|
|
ERROR_OCCURRED |
6
|
|
|
} from '../../../constants/ActionTypes'; |
7
|
|
|
|
8
|
|
|
import { setLoaderState } from '../../../actions/plugins/loader/LoaderActions'; |
9
|
|
|
|
10
|
|
|
import { dismissEditor } from '../../../actions/plugins/editor/EditorActions'; |
11
|
|
|
|
12
|
|
|
import Api from '../../../util/api'; |
13
|
|
|
|
14
|
|
|
export const setPage = ({ index, type, BUTTON_TYPES }) => { |
15
|
|
|
const pageIndex = type === BUTTON_TYPES.NEXT ? index + 1 : index - 1; |
16
|
|
|
|
17
|
|
|
return { type: PAGE_LOCAL, pageIndex }; |
18
|
|
|
}; |
19
|
|
|
|
20
|
|
|
export const setPageIndexAsync = ({ |
21
|
|
|
pageIndex, |
22
|
|
|
pageSize, |
23
|
|
|
dataSource, |
24
|
|
|
filterFields, |
25
|
|
|
sort, |
26
|
|
|
stateKey, |
27
|
|
|
afterAsyncFunc |
28
|
|
|
}) => { |
29
|
|
|
|
30
|
|
View Code Duplication |
if (typeof dataSource === 'function') { |
|
|
|
|
31
|
|
|
|
32
|
|
|
return (dispatch) => { |
33
|
|
|
|
34
|
|
|
dispatch(dismissEditor({ stateKey })); |
35
|
|
|
|
36
|
|
|
dispatch( |
37
|
|
|
setLoaderState({ state: true, stateKey }) |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
dataSource( |
41
|
|
|
{pageIndex, pageSize}, filterFields, sort |
42
|
|
|
).then((response) => { |
43
|
|
|
|
44
|
|
|
if (response && response.data) { |
45
|
|
|
|
46
|
|
|
dispatch({ |
47
|
|
|
type: PAGE_REMOTE, |
48
|
|
|
pageIndex: pageIndex, |
49
|
|
|
stateKey |
50
|
|
|
}); |
51
|
|
|
|
52
|
|
|
dispatch({ |
53
|
|
|
type: SET_DATA, |
54
|
|
|
data: response.data, |
55
|
|
|
total: response.total, |
56
|
|
|
currentRecords: response.items, |
57
|
|
|
success: true, |
58
|
|
|
stateKey |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
if (afterAsyncFunc |
62
|
|
|
&& typeof afterAsyncFunc === 'function') { |
63
|
|
|
afterAsyncFunc(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
else { |
68
|
|
|
|
69
|
|
|
if (response && !response.data) { |
70
|
|
|
/* eslint-disable no-console */ |
71
|
|
|
console.warn([ |
72
|
|
|
'A response was recieved but', |
73
|
|
|
'no data entry was found' |
74
|
|
|
].join(' ')); |
75
|
|
|
console.warn([ |
76
|
|
|
'Please see', |
77
|
|
|
'https://github.com/bencripps/react-redux-grid', |
78
|
|
|
'for documentation' |
79
|
|
|
].join(' ')); |
80
|
|
|
/* eslint-enable no-console */ |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
dispatch({ |
84
|
|
|
type: ERROR_OCCURRED, |
85
|
|
|
error: 'Unable to Retrieve Grid Data', |
86
|
|
|
errorOccurred: true, |
87
|
|
|
stateKey |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
dispatch( |
92
|
|
|
setLoaderState({ state: false, stateKey }) |
93
|
|
|
); |
94
|
|
|
}); |
95
|
|
|
}; |
96
|
|
|
} |
97
|
|
|
}; |
98
|
|
|
|
99
|
|
|
export const setPageAsync = ({ |
100
|
|
|
index, pageSize, type, BUTTON_TYPES, dataSource, stateKey |
101
|
|
|
}) => { |
102
|
|
|
|
103
|
|
|
const pageIndex = type === BUTTON_TYPES.NEXT ? index + 1 : index - 1; |
104
|
|
|
|
105
|
|
|
return (dispatch) => { |
106
|
|
|
|
107
|
|
|
dispatch( |
108
|
|
|
setLoaderState({ state: true, stateKey }) |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
return Api({ |
112
|
|
|
route: dataSource, |
113
|
|
|
method: 'GET', |
114
|
|
|
queryStringParams: { |
115
|
|
|
pageIndex: pageIndex, |
116
|
|
|
pageSize: pageSize |
117
|
|
|
} |
118
|
|
|
}).then((response) => { |
119
|
|
|
|
120
|
|
|
if (response && response.data) { |
121
|
|
|
|
122
|
|
|
dispatch({ |
123
|
|
|
type: PAGE_REMOTE, |
124
|
|
|
pageIndex: pageIndex, |
125
|
|
|
stateKey |
126
|
|
|
}); |
127
|
|
|
|
128
|
|
|
dispatch({ |
129
|
|
|
type: SET_DATA, |
130
|
|
|
data: response.data, |
131
|
|
|
total: response.total, |
132
|
|
|
currentRecords: response.data, |
133
|
|
|
success: true, |
134
|
|
|
stateKey |
135
|
|
|
}); |
136
|
|
|
|
137
|
|
|
dispatch( |
138
|
|
|
setLoaderState({ state: false, stateKey }) |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
else { |
143
|
|
|
dispatch({ |
144
|
|
|
type: ERROR_OCCURRED, |
145
|
|
|
error: response, |
146
|
|
|
errorOccurred: true, |
147
|
|
|
stateKey |
148
|
|
|
}); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
}); |
152
|
|
|
}; |
153
|
|
|
}; |
154
|
|
|
|
This check looks for functions where a
return
statement is found in some execution paths, but not in all.Consider this little piece of code
The function
isBig
will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly returnundefined
.This behaviour may not be what you had intended. In any case, you can add a
return undefined
to the other execution path to make the return value explicit.