feature(actions): Add reset all action
This commit is contained in:
parent
4925c77728
commit
db44dc69f5
|
@ -43,6 +43,7 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"coveralls": "^3.0.1",
|
||||
"lodash": "^4.17.10",
|
||||
"lodash.filter": "^4.6.0",
|
||||
"lodash.find": "^4.6.0",
|
||||
"lodash.flowright": "^3.5.0",
|
||||
|
|
|
@ -92,7 +92,8 @@ describe("buildStoreChunk", () => {
|
|||
"nested2",
|
||||
"nested3",
|
||||
"nested4",
|
||||
"nested5"
|
||||
"nested5",
|
||||
"resetAll"
|
||||
]);
|
||||
});
|
||||
it("Actions object has the correct top level structure for a non nested chunk", () => {
|
||||
|
@ -198,6 +199,37 @@ describe("buildStoreChunk", () => {
|
|||
});
|
||||
expect(chunk.selectors.nested3(store.getState())).toEqual([4, 6]);
|
||||
});
|
||||
|
||||
describe("reset all action", () => {
|
||||
it("Calling reset all will reset all store chunks", () => {
|
||||
store.dispatch(
|
||||
createCombinedAction({
|
||||
name: "batchUpdateFunsies",
|
||||
actions: [
|
||||
chunk.actions.nested2.update({
|
||||
foo: 4
|
||||
}),
|
||||
chunk.actions.nested2.update({
|
||||
bar: "boop!"
|
||||
}),
|
||||
chunk.actions.nested3.replace([4, 5, 6]),
|
||||
chunk.actions.nested3.removeAtIndex(1),
|
||||
chunk.actions.nested4.innerNested1.replace("boop!")
|
||||
]
|
||||
})
|
||||
);
|
||||
|
||||
store.dispatch(chunk.actions.resetAll());
|
||||
expect(chunk.selectors.nested2(store.getState())).toEqual({
|
||||
foo: 0,
|
||||
bar: ""
|
||||
});
|
||||
expect(chunk.selectors.nested3(store.getState())).toEqual([1, 2, 3]);
|
||||
expect(chunk.selectors.nested4.innerNested1(store.getState())).toBe(
|
||||
"bar"
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -9,13 +9,49 @@ import type { PartialStoreChunk } from "./reducers";
|
|||
// JS imports
|
||||
//==============================
|
||||
import { combineReducers } from "redux";
|
||||
import reduce from "lodash/reduce";
|
||||
import find from "lodash/find";
|
||||
import omit from "lodash/omit";
|
||||
import isFunction from "lodash/isFunction";
|
||||
import { get, isObject } from "lodash/fp";
|
||||
import { createReducer } from "./reducers";
|
||||
import { createCombinedAction } from "./reducers/batchUpdates";
|
||||
import { PROP_TYPES } from "./structure";
|
||||
|
||||
const reduce = require("lodash/fp/reduce").convert({ cap: false });
|
||||
const map = require("lodash/fp/map").convert({ cap: false });
|
||||
|
||||
/* Iterate through our reducers in order to find all reset actions */
|
||||
type ReducerActions = {
|
||||
reset: Function
|
||||
};
|
||||
/*prettier-ignore*/
|
||||
type Actions = {
|
||||
[key: string]: ReducerActions | {
|
||||
[key: string]: ReducerActions
|
||||
}
|
||||
};
|
||||
/* Iterate through the reducers to find all reset actions, and create a combined reducer
|
||||
which will call them all */
|
||||
const getResetAction = actions =>
|
||||
get("reset")(actions)
|
||||
? [get("reset")(actions)]
|
||||
: reduce(
|
||||
(resetActions, actions) => [
|
||||
...resetActions,
|
||||
...getResetAction(actions)
|
||||
],
|
||||
[]
|
||||
)(actions);
|
||||
|
||||
export const createResetAllAction = (
|
||||
storeChunkName: string,
|
||||
chunkActions: Actions
|
||||
) => () =>
|
||||
createCombinedAction({
|
||||
name: `${storeChunkName}-reset-all`,
|
||||
actions: map(action => action())(getResetAction(chunkActions))
|
||||
});
|
||||
|
||||
// Build a chunk of the eventual store. The selectors and actions
|
||||
// generated will specifically operate on the store chunk generated. Selectors will be
|
||||
// relative to the baseSelector provided or, if not specified, the root of the store, using
|
||||
|
@ -66,6 +102,16 @@ export function buildStoreChunk(
|
|||
};
|
||||
}
|
||||
|
||||
if (locationString === name) {
|
||||
return {
|
||||
...processedStructure,
|
||||
actions: {
|
||||
...processedStructure.actions,
|
||||
resetAll: createResetAllAction(name, processedStructure.actions)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return processedStructure;
|
||||
}
|
||||
|
||||
|
@ -79,7 +125,7 @@ export function determineStructureProcessing(
|
|||
processStructure(initialMemo, structure, name)
|
||||
);
|
||||
return combineStoreChunkReducers(
|
||||
reduce(structure, processStructure, initialMemo)
|
||||
reduce(processStructure, initialMemo)(structure)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -3279,7 +3279,7 @@ lodash.sortby@^4.7.0:
|
|||
version "4.7.0"
|
||||
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
|
||||
|
||||
lodash@^4.13.1, lodash@^4.14.0, lodash@^4.17.4:
|
||||
lodash@^4.13.1, lodash@^4.14.0, lodash@^4.17.10, lodash@^4.17.4:
|
||||
version "4.17.10"
|
||||
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"
|
||||
|
||||
|
|
Loading…
Reference in New Issue