Skip to content

Instantly share code, notes, and snippets.

@parasquid
Last active July 6, 2020 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parasquid/c940739bb4bfa0900f52e7fa2799085d to your computer and use it in GitHub Desktop.
Save parasquid/c940739bb4bfa0900f52e7fa2799085d to your computer and use it in GitHub Desktop.
See https://github.com/erikras/ducks-modular-redux (but here we add epics as well)
export const UPDATE_EDITOR_CONTENT = 'mock_test/UPDATE_EDITOR_CONTENT';
export const updateMockTestContent = (testId: number, content: string) => ({
type: UPDATE_EDITOR_CONTENT,
testId,
content,
});
handlers[UPDATE_EDITOR_CONTENT] = (state, action) => {
const { testId, content } = action;
return state
.setIn(['tests', 'items', testId, 'answer'], content)
.setIn(['tests', 'items', testId, 'lastUpdated'], Date.now());
};
const updateEditorContentEpic = action$ => action$
.ofType(UPDATE_EDITOR_CONTENT)
.debounceTime(500)
.mergeMap(({ testId, content }) => (
EJApi.updateMockTestContent(testId, { content })),
)
.map(responseToAction(SUCCESS_UPDATE_EDITOR_CONTENT));
export const SUCCESS_UPDATE_EDITOR_CONTENT = 'mock_test/SUCCESS_UPDATE_EDITOR_CONTENT';
handlers[SUCCESS_UPDATE_EDITOR_CONTENT] = (state, action) => {
const data = fromJS(action.data);
const testId = data.get('id');
// we shouldn't use the data from the server
return state.setIn(['tests', 'items', testId, 'isFetching'], false);
};
// epics
export const mockTestEpics = [
updateEditorContentEpic,
];
// default export
export default createReducer(initialState, handlers);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment