Skip to content

Instantly share code, notes, and snippets.

@aigoncharov
Created February 11, 2019 19:50
Show Gist options
  • Save aigoncharov/5eb2935b1656c96c625f938936f1f7c9 to your computer and use it in GitHub Desktop.
Save aigoncharov/5eb2935b1656c96c625f938936f1f7c9 to your computer and use it in GitHub Desktop.
Reducer organization - taking a step further
// We move that generic code to a dedicated module
import { Action, createClassReducer } from 'utils/reducer-class'
const actionTypeJediCreateInit = 'jedi-app/jedi-create-init'
const actionTypeJediCreateSuccess = 'jedi-app/jedi-create-success'
const actionTypeJediCreateError = 'jedi-app/jedi-create-error'
class ReducerJedi {
// Take a look at "Class field delcaratrions" proposal, which is now at Stage 3.
// https://github.com/tc39/proposal-class-fields
initialState = {
loading: false,
data: [],
error: undefined,
}
@Action(actionTypeJediCreateInit)
startLoading(state) {
return {
...state,
loading: true,
}
}
@Action(actionTypeJediCreateSuccess)
addNewJedi(state, action) {
return {
loading: false,
data: [...state.data, action.payload],
error: undefined,
}
}
@Action(actionTypeJediCreateError)
error(state, action) {
return {
...state,
loading: false,
error: action.payload,
}
}
}
export const reducerJedi = createClassReducer(ReducerJedi)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment