Skip to content

Instantly share code, notes, and snippets.

@totallymike
Last active April 29, 2020 14:06
Show Gist options
  • Save totallymike/cc0407e66774514af95b62cfc376d421 to your computer and use it in GitHub Desktop.
Save totallymike/cc0407e66774514af95b62cfc376d421 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
console.dir(actions);
const StepMachine = (
validateFunction = (obj) => Promise.resolve(obj)
) =>
Machine({
context: {
validate: validateFunction,
data: {},
},
initial: 'empty',
states: {
empty: {
on: {
UPDATE: {
actions: [
assign({
data: (context, event) => ({
...context.data,
...event.data,
}),
}),
sendParent((context) => ({
type: 'UPDATE_CHILD',
data: context.data,
})),
]
},
VALIDATE: {
target: 'validating',
},
},
},
validating: {
invoke: {
id: 'validateData',
src: ({ validate, data }) => validate(data),
onDone: 'validated',
onError: {
target: 'invalid',
actions: assign({
error: (context, event) => event.data,
}),
},
},
},
validated: {
type: 'final',
},
invalid: {
on: {
UPDATE: {
actions: assign({
data: ({ data }, { data: eventData }) => ({
...data,
...eventData,
}),
}),
},
VALIDATE: 'validating',
},
},
},
});
const alwaysValid = (obj) => Promise.resolve(obj)
const OnboardingMachine = Machine({
id: 'onboarding-machine',
context: {},
initial: 'stepOne',
states: {
stepOne: {
invoke: {
id: 'step-one',
autoForward: true,
src: StepMachine(),
onDone: {
target: 'stepTwo',
actions: assign({ stepOne: (context, event) => event.data }),
},
},
on: {
'UPDATE_CHILD': {
actions: assign({ stepOne: (context, event) => event.data })
}
}
},
stepTwo: {
invoke: {
id: 'step-two',
autoForward: true,
src: StepMachine(),
onDone: {
target: 'stepThree',
actions: assign({ stepTwo: (context, event) => event.data }),
},
},
},
stepThree: {
invoke: {
id: 'step-three',
src: StepMachine(),
autoForward: true,
onDone: {
target: 'complete',
actions: assign({ stepThree: (context, event) => event.data }),
},
},
},
complete: {
type: 'final',
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment