Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
isSaving: false, // Bool.
isTouched: false,
valid: {}, // index of valid values.
value: null, // Anything.
}
export const resetFields = fields => mergeWith(pick(fields, defaultState))
export const getDragCount = get('dragCount')
export const setFocus = mergeWith({ blur: false, focus: true, isTouched: true })
export const setClose = resetFields(['blur', 'focus'])
export const saving = set('isSaving', true)
export const isNewValue = (state, payload) => (!isUndefined(payload) && payload !== state.value)
export const setValue = overBranch(isNewValue, setIn('value'))
export const touched = overBranch(negate(get('isTouched')), set('isTouched', true))
export const changeReducer = flow(setValue, touched)
export const blurReducer = flow(
changeReducer,
resetFields(['dragCount', 'focus']),
set('blur', true),
)
export const closeReducer = flow(setClose, touched)
export const applyError = flow(setIn('error'), touched)
export const clearError = set('error', defaultState.error)
export const errorReducer = (state, payload) => {
if (payload.error && payload.value) {
payload.initialValue,
),
isTouched: true,
value: defaultTo(defaultTo(null, payload.initialValue), state.value),
})
export const setSaved = resetFields(['error', 'isSaving', 'savedProgress'])
export const savedReducer = (state, payload) => ({
...setSaved(state),
id: getOr(state.id, 'id', payload),
initialValue: state.value,
savedValue: getOr(state.value, 'value', payload),
value: getOr(state.value, 'value', payload),
})
export const saveProgressReducer = flow(setIn('savedProgress'), saving)
export const submitReducer = flow(
setValue,
setClose,
saving,
)
export const metaReducer = ({ meta, ...state }, payload) => ({
...state,
isTouched: true,
meta: merge(meta, payload),
})
export const reducers = {
[CLEAR]: () => defaultState,
[CLEAR_ERROR]: clearError,
// Should close also change initialValue?
initialValue: null, // Anything.
invalid: {}, // index of invalid values.
meta: null, // Anything.
savedProgress: 0, // Percentage Number 0-99.
savedValue: null,
isSaving: false, // Bool.
isTouched: false,
valid: {}, // index of valid values.
value: null, // Anything.
}
export const resetFields = fields => mergeWith(pick(fields, defaultState))
export const getDragCount = get('dragCount')
export const setFocus = mergeWith({ blur: false, focus: true, isTouched: true })
export const setClose = resetFields(['blur', 'focus'])
export const saving = set('isSaving', true)
export const isNewValue = (state, payload) => (!isUndefined(payload) && payload !== state.value)
export const setValue = overBranch(isNewValue, setIn('value'))
export const touched = overBranch(negate(get('isTouched')), set('isTouched', true))
export const changeReducer = flow(setValue, touched)
export const blurReducer = flow(
changeReducer,
resetFields(['dragCount', 'focus']),
set('blur', true),
)
export const closeReducer = flow(setClose, touched)
export const saving = set('isSaving', true)
export const isNewValue = (state, payload) => (!isUndefined(payload) && payload !== state.value)
export const setValue = overBranch(isNewValue, setIn('value'))
export const touched = overBranch(negate(get('isTouched')), set('isTouched', true))
export const changeReducer = flow(setValue, touched)
export const blurReducer = flow(
changeReducer,
resetFields(['dragCount', 'focus']),
set('blur', true),
)
export const closeReducer = flow(setClose, touched)
export const applyError = flow(setIn('error'), touched)
export const clearError = set('error', defaultState.error)
export const errorReducer = (state, payload) => {
if (payload.error && payload.value) {
return setValue(applyError(state, payload.error), payload.value)
}
return applyError(state, payload)
}
export const dragEnterReducer = flow(
setFocus,
update('dragCount', add(1)),
)
export const dragLeaveReducer = flow(
update('dragCount', subtrahend(1)),
export const resetFields = fields => mergeWith(pick(fields, defaultState))