Skip to content

Instantly share code, notes, and snippets.

@mathewtrivett
Last active August 20, 2021 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathewtrivett/69c169f3bc542eec0cf255b27a6932f0 to your computer and use it in GitHub Desktop.
Save mathewtrivett/69c169f3bc542eec0cf255b27a6932f0 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// 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