Created
July 9, 2021 06:21
-
-
Save korshunad/2cff36bcf59636ec02bc5724ebed0955 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 = { | |
IDLE: 'idle', | |
CHECK_ELIGIBILITY: 'check_eligibility', | |
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: 'idle', | |
context: { | |
orderID: '', | |
}, | |
states: { | |
[FS.IDLE]: { | |
on: { | |
PURCHASE: [ | |
{ | |
target: '#purchasePack.check_eligibility', | |
cond: 'isCheckingEligibility', | |
}, | |
{ | |
target: '#purchasePack.validating', | |
cond: 'isCaptchaEnabled', | |
}, | |
{ | |
target: '#purchasePack.purchasing', | |
}, | |
], | |
}, | |
}, | |
[FS.CHECK_ELIGIBILITY]: { | |
invoke: { | |
id: 'check_eligibility', | |
src: 'checkEligibility', | |
onDone: [ | |
{ | |
target: '#purchasePack.validating', | |
cond: 'isCaptchaEnabled', | |
}, | |
{ | |
target: '#purchasePack.purchasing', | |
}, | |
], | |
onError: { | |
target: '#purchasePack.idle', | |
actions: 'setErrorMessage', | |
}, | |
}, | |
}, | |
[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?.packListing?.levels, | |
isCaptchaEnabled: (context) => context?.flags?.purchaseRecaptcha, | |
isQueueFailed: (context, event) => event?.data?.isQueueFailed, | |
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