|
| 1 | +import get from 'lodash/get'; |
| 2 | + |
| 3 | +export const DYNAMIC_WIZARD_TYPES = ['function', 'object']; |
| 4 | + |
| 5 | +const createSchema = ({ formOptions, fields }) => { |
| 6 | + const { values } = formOptions.getState(); |
| 7 | + let schema = []; |
| 8 | + let field = fields[0]; // find first wizard step |
| 9 | + let index = -1; |
| 10 | + |
| 11 | + while (field) { |
| 12 | + index += 1; |
| 13 | + schema = [ |
| 14 | + ...schema, |
| 15 | + { |
| 16 | + title: field.title, |
| 17 | + substepOf: field.substepOf, |
| 18 | + index, |
| 19 | + primary: !schema[schema.length - 1] || !field.substepOf || field.substepOf !== schema[schema.length - 1].substepOf |
| 20 | + } |
| 21 | + ]; |
| 22 | + |
| 23 | + let nextStep = field.nextStep; |
| 24 | + |
| 25 | + if (typeof field.nextStep === 'object') { |
| 26 | + nextStep = nextStep.stepMapper[get(values, nextStep.when)]; |
| 27 | + } |
| 28 | + |
| 29 | + if (typeof field.nextStep === 'function') { |
| 30 | + nextStep = field.nextStep({ values }); |
| 31 | + } |
| 32 | + |
| 33 | + if (nextStep) { |
| 34 | + field = fields.find(({ name }) => name === nextStep); |
| 35 | + } else { |
| 36 | + field = undefined; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return schema; |
| 41 | +}; |
| 42 | + |
| 43 | +const handleNext = (state, nextStep, formOptions, fields) => { |
| 44 | + const newActiveIndex = state.activeStepIndex + 1; |
| 45 | + const shouldInsertStepIntoHistory = state.prevSteps.includes(state.activeStep); |
| 46 | + |
| 47 | + return { |
| 48 | + ...state, |
| 49 | + registeredFieldsHistory: { ...state.registeredFieldsHistory, [state.activeStep]: formOptions.getRegisteredFields() }, |
| 50 | + activeStep: nextStep, |
| 51 | + prevSteps: shouldInsertStepIntoHistory ? state.prevSteps : [...state.prevSteps, state.activeStep], |
| 52 | + activeStepIndex: newActiveIndex, |
| 53 | + maxStepIndex: newActiveIndex > state.maxStepIndex ? newActiveIndex : state.maxStepIndex, |
| 54 | + navSchema: state.isDynamic |
| 55 | + ? createSchema({ |
| 56 | + ...state, |
| 57 | + fields, |
| 58 | + formOptions, |
| 59 | + currentIndex: newActiveIndex |
| 60 | + }) |
| 61 | + : state.navSchema |
| 62 | + }; |
| 63 | +}; |
| 64 | + |
| 65 | +export const findCurrentStep = (activeStep, fields) => fields.find(({ name }) => name === activeStep); |
| 66 | + |
| 67 | +const jumpToStep = (state, index, valid, fields, crossroads, formOptions) => { |
| 68 | + const clickOnPreviousStep = state.prevSteps[index]; |
| 69 | + |
| 70 | + if (clickOnPreviousStep) { |
| 71 | + let originalActiveStep; |
| 72 | + |
| 73 | + const includeActiveStep = state.prevSteps.includes(state.activeStep, fields); |
| 74 | + originalActiveStep = state.activeStep; |
| 75 | + |
| 76 | + const newState = { |
| 77 | + ...state, |
| 78 | + activeStep: state.prevSteps[index], |
| 79 | + prevSteps: includeActiveStep ? state.prevSteps : [...state.prevSteps, state.activeStep], |
| 80 | + activeStepIndex: index |
| 81 | + }; |
| 82 | + |
| 83 | + const INDEXING_BY_ZERO = 1; |
| 84 | + |
| 85 | + const currentStep = findCurrentStep(newState.prevSteps[index], fields); |
| 86 | + |
| 87 | + const currentStepHasStepMapper = DYNAMIC_WIZARD_TYPES.includes(typeof currentStep.nextStep); |
| 88 | + |
| 89 | + const hardcodedCrossroads = crossroads; |
| 90 | + const dynamicStepShouldDisableNav = newState.isDynamic && currentStepHasStepMapper; |
| 91 | + |
| 92 | + const invalidStepShouldDisableNav = valid === false; |
| 93 | + |
| 94 | + let updatedState = { |
| 95 | + ...newState |
| 96 | + }; |
| 97 | + |
| 98 | + if (dynamicStepShouldDisableNav && !hardcodedCrossroads) { |
| 99 | + updatedState = { |
| 100 | + ...updatedState, |
| 101 | + navSchema: createSchema({ |
| 102 | + ...updatedState, |
| 103 | + formOptions, |
| 104 | + fields, |
| 105 | + currentIndex: index |
| 106 | + }), |
| 107 | + prevSteps: newState.prevSteps.slice(0, index), |
| 108 | + maxStepIndex: index |
| 109 | + }; |
| 110 | + } else if (currentStep.disableForwardJumping) { |
| 111 | + updatedState = { |
| 112 | + ...updatedState, |
| 113 | + prevSteps: newState.prevSteps.slice(0, index), |
| 114 | + maxStepIndex: index |
| 115 | + }; |
| 116 | + } else if (invalidStepShouldDisableNav) { |
| 117 | + const indexOfCurrentStep = newState.prevSteps.indexOf(originalActiveStep); |
| 118 | + |
| 119 | + updatedState = { |
| 120 | + ...updatedState, |
| 121 | + prevSteps: newState.prevSteps.slice(0, indexOfCurrentStep + INDEXING_BY_ZERO), |
| 122 | + maxStepIndex: newState.prevSteps.slice(0, indexOfCurrentStep + INDEXING_BY_ZERO).length - INDEXING_BY_ZERO |
| 123 | + }; |
| 124 | + } |
| 125 | + |
| 126 | + return updatedState; |
| 127 | + } |
| 128 | +}; |
| 129 | + |
| 130 | +const reducer = (state, { type, payload }) => { |
| 131 | + switch (type) { |
| 132 | + case 'finishLoading': |
| 133 | + return { |
| 134 | + ...state, |
| 135 | + loading: false, |
| 136 | + navSchema: createSchema({ |
| 137 | + ...state, |
| 138 | + fields: payload.fields, |
| 139 | + formOptions: payload.formOptions, |
| 140 | + currentIndex: 0 |
| 141 | + }) |
| 142 | + }; |
| 143 | + case 'handleNext': |
| 144 | + return handleNext(state, payload.nextStep, payload.formOptions, payload.fields); |
| 145 | + case 'setPrevSteps': |
| 146 | + return { |
| 147 | + ...state, |
| 148 | + prevSteps: state.prevSteps.slice(0, state.activeStepIndex), |
| 149 | + maxStepIndex: state.activeStepIndex, |
| 150 | + navSchema: createSchema({ |
| 151 | + ...state, |
| 152 | + fields: payload.fields, |
| 153 | + formOptions: payload.formOptions, |
| 154 | + currentIndex: state.activeStepIndex |
| 155 | + }) |
| 156 | + }; |
| 157 | + case 'jumpToStep': |
| 158 | + return jumpToStep(state, payload.index, payload.valid, payload.fields, payload.crossroads, payload.formOptions); |
| 159 | + default: |
| 160 | + return state; |
| 161 | + } |
| 162 | +}; |
| 163 | + |
| 164 | +export default reducer; |
0 commit comments