Last active
June 28, 2018 14:36
-
-
Save stevenleeg/a8259de58fd61523209d90b9ad593e1d 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
import Immutable from 'immutable'; | |
import {put, take} from 'redux-saga/effects'; | |
import {ClientActions, ClientActionTypes} from 'services/clients/transitions'; | |
import {generateFormTransitions} from 'utils/form-transitions'; | |
import translateTransitions from 'utils/translate-transitions'; | |
const initialValues = Immutable.fromJS({ | |
name: '', | |
}); | |
const transitions = [ | |
...generateFormTransitions({initialValues}), | |
{ | |
name: 'fetchClient', | |
* saga({Actions}, {id}) { | |
yield put(ClientActions.fetchItem({id})); | |
const {type, item} = yield take([ | |
ClientActionTypes.FETCH_ITEM_SUCCESS, | |
ClientActionTypes.FETCH_ITEM_FAILURE, | |
]); | |
if (type !== ClientActionTypes.FETCH_ITEM_SUCCESS) return; | |
yield put(Actions.loadInputs({inputs: item})); | |
}, | |
}, | |
]; | |
export const initialState = Immutable.fromJS({ | |
inputs: initialValues, | |
}); | |
export const { | |
Actions, | |
ActionTypes, | |
reducers, | |
saga, | |
prefix, | |
} = translateTransitions({ | |
prefix: 'pages/clients/configuration', | |
transitions, | |
}); |
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
export const getInputs = (state, {viewPath}) => { | |
const path = viewPath.split('/'); | |
return state.getIn([...path, 'inputs']); | |
}; | |
export const generateFormTransitions = ({initialValues}) => { | |
return [ | |
{ | |
name: 'loadInputs', | |
reducer(state, {inputs}) { | |
return state.set('inputs', state.get('inputs').merge(inputs)); | |
}, | |
}, | |
{ | |
name: 'changeInput', | |
reducer(state, {name, value}) { | |
return state.setIn(['inputs', name], value); | |
}, | |
}, | |
{ | |
name: 'reset', | |
reducer(state) { | |
return state.set('inputs', initialValues); | |
}, | |
}, | |
]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment