Last active
June 5, 2017 12:12
-
-
Save erezrokah/559836b1aadd3b0729a2d791f39ae915 to your computer and use it in GitHub Desktop.
This file contains 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
//@flow | |
import createReducer from '../createReducer' | |
export const metaTypes = { | |
messages: 'messages', | |
userContacts: 'userContacts', | |
} | |
function getInitialState() { | |
let initialState = { } | |
Object.keys(metaTypes).forEach((metaType) => { | |
initialState[metaType] = { inProgress: false, items: { } } | |
}) | |
return initialState | |
} | |
const initialState = getInitialState() | |
//createReducer taken from here http://redux.js.org/docs/recipes/ReducingBoilerplate.html | |
export const reducer = createReducer(initialState, { | |
['FIREBASE_LISTEN_REQUESTED'](state, action) { | |
const newState = { | |
...state, | |
[action.metaType]: { | |
...state[action.metaType], inProgress: true, error: '', | |
} | |
} | |
return newState | |
}, | |
['FIREBASE_LISTEN_FULFILLED'](state, action) { | |
const newState = { | |
...state, | |
[action.metaType]: { | |
...state[action.metaType], | |
inProgress: false, error: '', items: action.items | |
} | |
} | |
return newState | |
}, | |
['FIREBASE_LISTEN_REJECTED'](state, action) { | |
const error = action.error | |
const newState = { | |
...state, | |
[action.metaType]: { | |
...state[action.metaType], inProgress: false, error | |
} | |
} | |
return newState | |
}, | |
['FIREBASE_LISTEN_CHILD_ADDED'](state, action) { | |
const currentItems = state[action.metaType].items | |
const items = { ...currentItems, [action.id]: action.value } | |
const newState = { | |
...state, | |
[action.metaType]: { | |
...state[action.metaType], inProgress: false, error: '', items | |
} | |
} | |
return newState | |
}, | |
['FIREBASE_LISTEN_CHILD_CHANGED'](state, action) { | |
const currentItems = state[action.metaType].items | |
const items = { ...currentItems, [action.id]: action.value } | |
const newState = { | |
...state, | |
[action.metaType]: { | |
...state[action.metaType], inProgress: false, error: '', items | |
} | |
} | |
return newState | |
}, | |
['FIREBASE_LISTEN_CHILD_REMOVED'](state, action) { | |
const currentItems = state[action.metaType].items | |
const items = { ...currentItems } | |
delete items[action.id] | |
const newState = { | |
...state, | |
[action.metaType]: { | |
...state[action.metaType], inProgress: false, error: '', items | |
} | |
} | |
return newState | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment