Created
July 13, 2021 01:01
-
-
Save korshunad/7ff6e633162d5d0499d4d0c3e1b9a25c to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains 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 FS = { | |
CHECK_ELIGIBILITY: 'check_eligibility', | |
IDLE: 'idle', | |
NO_RETRY: 'no_retry', | |
VALIDATING: 'validating', | |
PURCHASING: 'purchasing', | |
QUEUE_FAILED: 'queueFailed', | |
EXISTING_ORDER: 'existingOrder', | |
POLLING_ORDER: 'pollingOrder', | |
REDIRECT_TO_BUY: 'redirectToBuy', | |
RESET: 'reset', | |
DONE: 'done', | |
}; | |
const IGNORE_PACK_ORDER_ERRORS_DURATION = 180000; | |
Machine( | |
{ | |
id: 'purchasePack', | |
initial: 'initial', | |
context: { | |
orderID: '', | |
}, | |
states: { | |
initial: { | |
always: [ | |
{ | |
target: '#purchasePack.check_eligibility', | |
cond: 'isCheckingEligibility', | |
}, | |
{ | |
target: '#purchasePack.idle', | |
}, | |
], | |
}, | |
[FS.CHECK_ELIGIBILITY]: { | |
invoke: { | |
id: 'check_eligibility', | |
src: 'checkEligibility', | |
onDone: { | |
target: '#purchasePack.idle', | |
}, | |
onError: { | |
target: '#purchasePack.no_retry', | |
actions: 'setErrorMessage', | |
}, | |
}, | |
}, | |
[FS.IDLE]: { | |
on: { | |
PURCHASE: [ | |
{ | |
target: '#purchasePack.validating', | |
cond: 'isCaptchaEnabled', | |
}, | |
{ | |
target: '#purchasePack.purchasing', | |
}, | |
], | |
}, | |
}, | |
[FS.NO_RETRY]: { | |
type: 'final', | |
}, | |
[FS.VALIDATING]: { | |
invoke: { | |
id: 'validating', | |
src: 'executeRecaptcha', | |
onDone: { | |
target: '#purchasePack.purchasing', | |
}, | |
onError: { | |
target: '#purchasePack.idle', | |
actions: 'setErrorMessage', | |
}, | |
}, | |
}, | |
[FS.PURCHASING]: { | |
entry: ['updateQuantity'], | |
invoke: { | |
id: 'purchasing', | |
src: 'purchasePack', | |
onDone: [ | |
{ | |
target: '#purchasePack.queueFailed', | |
cond: 'isQueueFailed', | |
actions: 'setErrorMessage', | |
}, | |
{ | |
target: '#purchasePack.existingOrder', | |
cond: 'isExistingReservation', | |
actions: 'updateOrder', | |
}, | |
{ | |
target: '#purchasePack.pollingOrder', | |
actions: 'updateOrder', | |
}, | |
], | |
onError: { | |
target: '#purchasePack.idle', | |
actions: 'setErrorMessage', | |
}, | |
}, | |
}, | |
[FS.QUEUE_FAILED]: { | |
on: { | |
BACK_TO_THE_QUEUE: { | |
target: '#purchasePack.idle', | |
}, | |
}, | |
}, | |
[FS.EXISTING_ORDER]: { | |
on: { | |
CONTINUE_ORDER: { | |
target: '#purchasePack.pollingOrder', | |
}, | |
// @todo implement this when BE is ready | |
// https://github.com/dapperlabs/nba-app/issues/4336 | |
// CANCEL_ORDER: { | |
// target: '#purchasePack.existingOrder', | |
// }, | |
}, | |
}, | |
[FS.POLLING_ORDER]: { | |
after: { | |
[IGNORE_PACK_ORDER_ERRORS_DURATION]: { | |
actions: send('STOP', { to: 'pollingOrder' }), | |
}, | |
}, | |
invoke: { | |
id: 'pollingOrder', | |
src: 'pollOrder', | |
onError: { | |
target: '#purchasePack.idle', | |
actions: 'setErrorMessage', | |
}, | |
}, | |
on: { | |
POLL_ORDER_ERROR: { | |
target: '#purchasePack.idle', | |
actions: 'setErrorMessage', | |
}, | |
POLL_ORDER_SUCCESS: { | |
target: '#purchasePack.redirectToBuy', | |
}, | |
}, | |
}, | |
[FS.REDIRECT_TO_BUY]: { | |
invoke: { | |
id: 'redirectToBuy', | |
src: 'redirectToBuy', | |
type: 'final', | |
}, | |
}, | |
}, | |
}, | |
{ | |
guards: { | |
isCheckingEligibility: (context) => !!context?.levels, | |
isRecaptchaEnabled: (context) => context?.flags?.purchaseRecaptcha, | |
isExistingReservation: (context, event) => { | |
// IF the pack count is the same we can auto-continue and not display a message as they are just re-attempting the same purchase size. | |
if (context?.quantity === event?.data?.quantity) { | |
return false; | |
} else { | |
return event?.data?.existingReservation; | |
} | |
}, | |
}, | |
}, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment