Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stevenleeg/a8259de58fd61523209d90b9ad593e1d to your computer and use it in GitHub Desktop.
Save stevenleeg/a8259de58fd61523209d90b9ad593e1d to your computer and use it in GitHub Desktop.
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,
});
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