Last active
August 20, 2021 09:04
-
-
Save mathewtrivett/69c169f3bc542eec0cf255b27a6932f0 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const createStep = ({ | |
previousStep, | |
nextStep, | |
validation | |
}) => ({ | |
initial: 'editing', | |
onDone: { | |
target: nextStep, | |
}, | |
states: { | |
editing: { | |
on: { | |
UPDATE: { | |
target: 'editing', | |
actions: ['updateContext'], | |
}, | |
NEXT: { | |
target: 'validating', | |
}, | |
BACK: { | |
target: previousStep, | |
} | |
}, | |
}, | |
validating: { | |
invoke: { | |
id: 'validator', | |
src: validation, | |
onDone: { | |
target: 'valid', | |
}, | |
onError: { | |
target: 'invalid', | |
actions: ['setErrors'], | |
}, | |
}, | |
}, | |
valid: { | |
type: 'final', | |
}, | |
invalid: { | |
on: { | |
UPDATE: { | |
target: 'editing', | |
actions: [ | |
'updateContext', | |
'clearErrors', | |
], | |
}, | |
}, | |
}, | |
}, | |
}) | |
const orderJourneyMachine = Machine({ | |
id: 'orderJourney', | |
initial: 'stepOne', | |
states: { | |
stepOne: { | |
...createStep({ | |
nextStep: '#orderJourney.stepTwo', | |
validation: (context,event) => 'validation' | |
}) | |
}, | |
stepTwo: { | |
...createStep({ | |
previousStep: '#orderJourney.stepOne', | |
nextStep: '#orderJourney.stepThree', | |
validation: (context,event) => 'validation' | |
}) | |
}, | |
stepThree: { | |
...createStep({ | |
previousStep: '#orderJourney.stepTwo', | |
validation: (context,event) => 'validation' | |
}) | |
} | |
} | |
}, { | |
actions: { | |
updateContext: 'shared', | |
clearErrors: 'shared', | |
setErrors: 'shared' | |
}, | |
services: { | |
validator: 'loads different validator for each step' | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment