Skip to content

Instantly share code, notes, and snippets.

@simontegg
Created July 15, 2020 03:25
Show Gist options
  • Select an option

  • Save simontegg/ae79d618288e8fcb3b501cdb4edc4948 to your computer and use it in GitHub Desktop.

Select an option

Save simontegg/ae79d618288e8fcb3b501cdb4edc4948 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
function submitWizard(context) {
return new Promise((resolve, reject) => {
setTimeOut(() => {
const isSuccess = Math.random() > 0
return isSuccess ? resolve({ data: { your: "result" } }) : reject(Error('did not work'))
}, 3000)
})
}
const wizard = Machine({
id: "wizard",
initial: "idle",
context: {
},
states: {
idle: { id: 'idle', on: { START: "step1" } },
step1: {
on: { NEXT: "step2" }
},
step2: {
on: {
PREVIOUS: "step1",
NEXT: "step3"
}
},
step3: {
on: {
PREVIOUS: "step2",
NEXT: "step4"
}
},
step4: {
initial: "review",
states: {
review: {
on: { FINISH: "submitting" }
},
submitting: {
invoke: {
id: "submit-wizard",
src: submitWizard,
onDone: "submitted",
onError: "failed"
}
},
submitted: { on: { CLOSE: "#idle" } },
failed: { on: { RETRY: "submitting" } }
},
},
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment