Skip to content

Commit

Permalink
fix: clear fetchable data value to initial value
Browse files Browse the repository at this point in the history
  • Loading branch information
Temzasse committed Mar 26, 2019
1 parent 5e349d6 commit 36794e9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions example/typed/typings/reducktion/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ declare module 'reducktion' {
loading: Reducer<State>;
success: Reducer<State>;
failure: Reducer<State>;
clear: Reducer<State>;
}

type NoopReducer = (state: any) => any;
Expand Down
23 changes: 18 additions & 5 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const createFetchableReducers = ({
successField,
overrides,
updater,
initialValue,
}) => {
const defaultReducers = {
[types.loading]: state => ({
Expand Down Expand Up @@ -144,7 +145,7 @@ const createFetchableReducers = ({
[types.clear]: state => ({
...state,
[successField]: {
...state[successField],
data: initialValue,
status: FETCHABLE_STATUS.INITIAL,
error: null,
},
Expand All @@ -171,7 +172,12 @@ const createFetchableAction = types => {
return action;
};

export function handleFetchableAction(args, actionName, modelName) {
export function handleFetchableAction({
args,
actionName,
modelName,
initialState,
}) {
const typePrefix = `${modelName}/${actionName}`;

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

// If no success field is provided -> create reducers for for tracking
// the status and error of the fetchable action

/* eslint-disable indent */
const reducers = successField
? createFetchableReducers({ types: t, successField, overrides, updater })
? createFetchableReducers({
types: t,
successField,
overrides: args.length > 1 ? args[1] || {} : {},
updater: args.length > 2 ? args[2] : defaultDataUpdater,
initialValue: initialState[successField].data,
})
: createSimpleFetchableReducers({ types: t, actionName });
/* eslint-enable indent */

// Return types that are inlined to the other types instead of accessing them
// via `types.fetchSomething.success` you access them normally
Expand Down
1 change: 1 addition & 0 deletions src/reducktion.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface FetchableReducers<State> {
loading: Reducer<State>;
success: Reducer<State>;
failure: Reducer<State>;
clear: Reducer<State>;
}

type NoopReducer = (state: any) => any;
Expand Down
9 changes: 5 additions & 4 deletions src/reducktion.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ export const createModel = model => {
([actionName, reducerHandler]) => {
if (isFetchableAction(reducerHandler)) {
// Handle async API actions
const fetchableX = handleFetchableAction(
reducerHandler.args,
const fetchableX = handleFetchableAction({
actionName,
model.name
);
initialState,
args: reducerHandler.args,
modelName: model.name,
});

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

0 comments on commit 36794e9

Please sign in to comment.