Created
October 28, 2020 22:10
-
-
Save bholmesdev/4bcb91ac4e52e75c70eec0c648ac31ad to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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