Skip to content

Instantly share code, notes, and snippets.

@lifedraft
Last active April 12, 2021 14:19
Show Gist options
  • Save lifedraft/e9b0e45574d5b913cc8a20f932181cc9 to your computer and use it in GitHub Desktop.
Save lifedraft/e9b0e45574d5b913cc8a20f932181cc9 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 fetchMachine = Machine({
id: 'billing',
initial: 'billing',
context: {},
states: {
billing: {
initial: 'fetchBillingInformation',
states: {
fetchBillingInformation: {
entry: assign({
loading: (_, _event) => true,
errors: (_, _event) => false,
}),
invoke: {
src: 'fetchBillingInformation',
onDone: {
target: 'idle',
actions: assign({
loading: (_, _event) => false,
errors: (_, _event) => false,
}),
},
onError: {
actions: ['redirectToCart'],
},
},
},
idle: {
entry: ['trackCheckoutBillingStep'],
on: {
SUBMIT_BILLING_FORM: {
target: 'updateBillingInformation',
},
},
},
updateBillingInformation: {
entry: assign({
loading: (_, _event) => true,
errors: (_, _event) => false,
billingInformationValidationErrors: (_, _event) => ({
fieldErrors: [],
generalError: false,
}),
}),
invoke: {
src: 'updateBillingInformation',
onDone: {
target: 'setPaymentSelection',
actions: assign({
loading: (_, _event) => false,
}),
},
onError: {
target: 'idle',
actions: assign({
loading: (_, _event) => false,
billingInformationValidationErrors: (_, event) => ({
...event.data.fieldErrors,
}),
errors: (_, event) => event.data.generalError,
}),
},
},
},
setPaymentSelection: {
entry: assign({
loading: (_, _event) => true,
errors: (_, _event) => false,
}),
invoke: {
src: 'setPaymentSelection',
onDone: {
target: 'success',
actions: assign({
loading: (_, _event) => false,
}),
},
onError: {
target: 'idle',
actions: assign({
loading: (_, _event) => false,
errors: (_, _event) => true,
}),
},
},
},
success: {
type: 'final',
},
},
onDone: {
target: '#success',
},
},
success: {
id: 'success',
type: 'final',
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment