Last active
March 12, 2021 20:13
-
-
Save austinsamsel/881d132305065a3363d1df926f63107f 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 FS = { | |
IDLE: 'idle', | |
AUTHENTICATING: 'authenticating', | |
NOT_AUTHENTICATED: 'not_authenticated', | |
COOLDOWN: 'cooldown', | |
ERROR: 'error', | |
NO_RETRY: 'no_retry', | |
CHECKING_MOMENT: 'checking_moment', | |
VALIDATING: 'validating', | |
PLACING_ORDER: 'placing_order', | |
GETTING_PURCHASE_INTENT: 'getting_purchase_intent', | |
REDIRECTING_TO_DAPPER: 'redirecting_to_dapper', | |
}; | |
const IGNORE_ERRORS_DURATION = 50000; | |
const fetchMachine = Machine({ | |
id: 'purchaseP2PMoment', | |
initial: FS.IDLE, | |
states: { | |
[FS.IDLE]: { | |
on: { | |
PURCHASE: [ | |
{ | |
target: `#purchaseP2PMoment.${FS.AUTHENTICATING}`, | |
}, | |
], | |
}, | |
}, | |
[FS.ERROR]: { | |
id: [FS.ERROR], | |
on: { | |
PURCHASE: [ | |
{ | |
target: `#purchaseP2PMoment.${FS.AUTHENTICATING}`, | |
}, | |
], | |
}, | |
}, | |
[FS.NO_RETRY]: { | |
id: [FS.NO_RETRY], | |
type: 'final', | |
}, | |
[FS.AUTHENTICATING]: { | |
id: 'authenticating', | |
invoke: { | |
src: 'checkIfUserIsLoggedIn', | |
onDone: [ | |
{ | |
cond: 'isCooldownEnabled', | |
target: `#purchaseP2PMoment.${FS.COOLDOWN}`, | |
}, | |
{ | |
target: `#purchaseP2PMoment.${FS.CHECKING_MOMENT}`, | |
}, | |
], | |
onError: { | |
target: `#purchaseP2PMoment.${FS.NOT_AUTHENTICATED}`, | |
}, | |
}, | |
}, | |
[FS.NOT_AUTHENTICATED]: { | |
on: { | |
RESET: { target: `#purchaseP2PMoment.${FS.IDLE}`, actions: 'reset' }, | |
}, | |
}, | |
[FS.COOLDOWN]: { | |
id: 'cooldown', | |
on: { | |
CONFIRM: '#checking_moment', | |
DISMISS: { target: `#purchaseP2PMoment.${FS.IDLE}`, actions: 'reset' }, | |
}, | |
}, | |
[FS.CHECKING_MOMENT]: { | |
id: FS.CHECKING_MOMENT, | |
invoke: { | |
src: 'checkIsMomentStillForSale', | |
onDone: [ | |
{ | |
target: `#purchaseP2PMoment.${FS.VALIDATING}`, | |
cond: 'isRecaptchaEnabled', | |
}, | |
{ | |
target: `#purchaseP2PMoment.${FS.PLACING_ORDER}`, | |
}, | |
], | |
onError: [ | |
{ | |
cond: 'isNoRetryError', | |
target: `#purchaseP2PMoment.${FS.NO_RETRY}`, | |
actions: 'setErrorMessage', | |
}, | |
{ | |
target: `#purchaseP2PMoment.${FS.ERROR}`, | |
actions: 'setErrorMessage', | |
}, | |
], | |
}, | |
}, | |
[FS.VALIDATING]: { | |
id: FS.VALIDATING, | |
invoke: { | |
src: 'executeRecaptcha', | |
onDone: { | |
target: `#purchaseP2PMoment.${FS.PLACING_ORDER}`, | |
}, | |
onError: { | |
target: `#purchaseP2PMoment.${FS.ERROR}`, | |
actions: 'setErrorMessage', | |
}, | |
}, | |
}, | |
[FS.PLACING_ORDER]: { | |
id: FS.PLACING_ORDER, | |
invoke: { | |
src: 'purchaseP2PMoment', | |
onDone: { | |
target: `#purchaseP2PMoment.${FS.GETTING_PURCHASE_INTENT}`, | |
}, | |
onError: { | |
target: `#purchaseP2PMoment.${FS.ERROR}`, | |
actions: 'setErrorMessage', | |
}, | |
}, | |
}, | |
[FS.GETTING_PURCHASE_INTENT]: { | |
id: FS.GETTING_PURCHASE_INTENT, | |
after: { | |
[IGNORE_ERRORS_DURATION]: { | |
actions: send('STOP', { to: 'pollOrderService' }), | |
}, | |
}, | |
invoke: { | |
id: 'pollOrderService', | |
src: 'pollOrder', | |
onError: { | |
target: `#purchaseP2PMoment.${FS.ERROR}`, | |
actions: 'setErrorMessage', | |
}, | |
}, | |
on: { | |
POLL_ORDER_ERROR: { | |
target: `#purchaseP2PMoment.${FS.ERROR}`, | |
actions: 'setErrorMessage', | |
}, | |
POLL_ORDER_SUCCESS: { | |
target: `#purchaseP2PMoment.${FS.REDIRECTING_TO_DAPPER}`, | |
}, | |
}, | |
}, | |
[FS.REDIRECTING_TO_DAPPER]: { | |
id: FS.REDIRECTING_TO_DAPPER, | |
type: 'final', | |
invoke: { | |
src: 'redirectToBuy', | |
}, | |
}, | |
}, | |
}, | |
{ | |
guards: { | |
isRecaptchaEnabled: (context) => context?.flags?.p2pPurchaseRecaptcha, | |
isCooldownEnabled: (context) => context?.flags?.feature_p2pCooldowns, | |
isNoRetryError: (_, event) => | |
false, | |
}, | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment