Skip to content

Commit 36794e9

Browse files
committed
fix: clear fetchable data value to initial value
1 parent 5e349d6 commit 36794e9

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

example/typed/typings/reducktion/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ declare module 'reducktion' {
3434
loading: Reducer<State>;
3535
success: Reducer<State>;
3636
failure: Reducer<State>;
37+
clear: Reducer<State>;
3738
}
3839

3940
type NoopReducer = (state: any) => any;

src/helpers.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ const createFetchableReducers = ({
115115
successField,
116116
overrides,
117117
updater,
118+
initialValue,
118119
}) => {
119120
const defaultReducers = {
120121
[types.loading]: state => ({
@@ -144,7 +145,7 @@ const createFetchableReducers = ({
144145
[types.clear]: state => ({
145146
...state,
146147
[successField]: {
147-
...state[successField],
148+
data: initialValue,
148149
status: FETCHABLE_STATUS.INITIAL,
149150
error: null,
150151
},
@@ -171,7 +172,12 @@ const createFetchableAction = types => {
171172
return action;
172173
};
173174

174-
export function handleFetchableAction(args, actionName, modelName) {
175+
export function handleFetchableAction({
176+
args,
177+
actionName,
178+
modelName,
179+
initialState,
180+
}) {
175181
const typePrefix = `${modelName}/${actionName}`;
176182

177183
const t = {
@@ -190,14 +196,21 @@ export function handleFetchableAction(args, actionName, modelName) {
190196
// User can either provide only reducer field name for success case
191197
// or reducer overrides for `loading` / `success` / `failure` cases
192198
const successField = args.length > 0 ? args[0] : null;
193-
const overrides = args.length > 1 ? args[1] || {} : {};
194-
const updater = args.length > 2 ? args[2] : defaultDataUpdater;
195199

196200
// If no success field is provided -> create reducers for for tracking
197201
// the status and error of the fetchable action
202+
203+
/* eslint-disable indent */
198204
const reducers = successField
199-
? createFetchableReducers({ types: t, successField, overrides, updater })
205+
? createFetchableReducers({
206+
types: t,
207+
successField,
208+
overrides: args.length > 1 ? args[1] || {} : {},
209+
updater: args.length > 2 ? args[2] : defaultDataUpdater,
210+
initialValue: initialState[successField].data,
211+
})
200212
: createSimpleFetchableReducers({ types: t, actionName });
213+
/* eslint-enable indent */
201214

202215
// Return types that are inlined to the other types instead of accessing them
203216
// via `types.fetchSomething.success` you access them normally

src/reducktion.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ interface FetchableReducers<State> {
3333
loading: Reducer<State>;
3434
success: Reducer<State>;
3535
failure: Reducer<State>;
36+
clear: Reducer<State>;
3637
}
3738

3839
type NoopReducer = (state: any) => any;

src/reducktion.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ export const createModel = model => {
3636
([actionName, reducerHandler]) => {
3737
if (isFetchableAction(reducerHandler)) {
3838
// Handle async API actions
39-
const fetchableX = handleFetchableAction(
40-
reducerHandler.args,
39+
const fetchableX = handleFetchableAction({
4140
actionName,
42-
model.name
43-
);
41+
initialState,
42+
args: reducerHandler.args,
43+
modelName: model.name,
44+
});
4445

4546
// Inline fetchable types
4647
Object.entries(fetchableX.types).forEach(([k, v]) => {

0 commit comments

Comments
 (0)