Last active
July 6, 2020 14:07
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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