Skip to content

Instantly share code, notes, and snippets.

@bholmesdev
Created October 28, 2020 22:10
Show Gist options
  • Save bholmesdev/4bcb91ac4e52e75c70eec0c648ac31ad to your computer and use it in GitHub Desktop.
Save bholmesdev/4bcb91ac4e52e75c70eec0c648ac31ad 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 formStates = (SUBMIT) => ({
incomplete: {
on: {
ERRORS_CLEARED: 'complete',
SUBMIT: {
actions: ['touchAll', 'scrollToErrors']
},
}
},
complete: {
on: {
ERRORS_FOUND: 'incomplete',
SUBMIT
}
},
})
const checkoutMachine = Machine({
id: 'digitalCheckout',
initial: 'step1',
context: {
showErrorBanner: false,
errorMessage: '',
},
states: {
step1: {
initial: 'incomplete',
states: {
...formStates({
target: 'validating',
actions: 'validate'
}),
validating: {
entry: assign({
showErrorBanner: false,
}),
on: {
VALID: '#digitalCheckout.step2',
INVALID: {
target: 'complete',
actions: assign({
showErrorBanner: true,
errorMessage: 'contentfulKey.invalid'
})
},
}
}
}
},
step2: {
initial: 'incomplete',
states: {
...formStates({
target: 'checkingOut',
actions: 'requestCheckout'
}),
checkingOut: {
entry: assign({
showErrorBanner: false,
}),
on: {
SUCCESS: '#digitalCheckout.success',
FAIL: {
target: 'complete',
actions: assign({
showErrorBanner: true,
errorMessage: 'contentfulKey.apiFailure'
})
}
}
}
}
},
success: { type: 'final' }
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment