Skip to content

Instantly share code, notes, and snippets.

@ccorcos
Created August 29, 2016 23:35
Show Gist options
  • Save ccorcos/4594418d305b4795a2a18b2b5e1de18b to your computer and use it in GitHub Desktop.
Save ccorcos/4594418d305b4795a2a18b2b5e1de18b to your computer and use it in GitHub Desktop.
// mutators.js
export const closeModal = s => s.set('modalOpen', false)
export const stopLoading = s => s.set('loading', false)
export const updateLoan = loan => s => s.setIn(['loans', loan.id], Immutable.fromJS(loan))
export const setPaymentError = error => s => s.set('paymentError', error)
// utils.js
const applyFn = (state, fn) => fn(state)
export const pipe = (fns, state) =>
state.withMutations(s => fns.reduce(applyFn, s))
// reducer.js
import * as mutate from './mutators'
export default function reducer(state=initialState, action) {
switch(action.type) {
case defs.MAKE_PAYMENT_SUCCESS:
return pipe([
mutate.closeModal,
mutate.stopLoading,
mutate.updateLoan(action.loan),
], state)
case defs.MAKE_PAYMENT_FAILED:
return pipe([
mutate.closeModal,
mutate.stopLoading,
mutate.setPaymentError(action.error),
], state)
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment