Skip to content

Instantly share code, notes, and snippets.

@korshunad
Last active June 24, 2021 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save korshunad/a7376c8afb9b1edcdec7847b4995bca7 to your computer and use it in GitHub Desktop.
Save korshunad/a7376c8afb9b1edcdec7847b4995bca7 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: 'p2p-listing-purchase',
initial: 'idle',
context: { selectedMoment: {}, errorMessage: { i18nKey: '' } },
states: {
idle: {
id: 'idle',
on: {
SELECT: '#select',
},
},
select: {
id: 'select',
initial: 'fetch',
entry: 'clearSelectedMoment',
on: {
CANCEL: { target: '#idle', actions: 'clearSelectedMoment' },
REFETCH: '#select.fetch',
},
states: {
fetch: {
invoke: {
src: 'getListings',
onDone: [
{
target: '#select.idle',
cond: 'listingHasMoments',
actions: ['assignGetListings'],
},
{
target: '#select.empty',
},
],
onError: '#select.error',
},
},
fetch_more: {
invoke: {
src: 'getListings',
onDone: [
{
target: '#select.idle',
actions: ['assignGetListings'],
},
],
onError: {
target: '#select.idle',
actions: ['assignFetchMoreError'],
},
},
},
idle: {
initial: 'idle',
on: {
SELECT_MOMENT: {
actions: 'assignSelectedMoment',
target: '#select.idle.idle',
},
CLEAR_SELECTION: {
actions: 'clearSelectedMoment',
target: '#select.idle.idle',
},
SET_SORT: {
target: '#select.fetch',
actions: ['assignSort'],
},
FETCH_MORE: {
target: '#select.fetch_more',
},
INTEND_TO_BUY: {
target: '#select.idle.fetchCurrentOrders',
},
BUY: {
target: '#buyingMoment',
},
},
states: {
idle: {},
fetchCurrentOrders: {
invoke: {
src: 'checkExistingOrders',
onDone: [
{
target: '#select.idle.showCurrentOrdersWarning',
cond: 'hasExistingOrders',
},
{
target: '#buyingMoment',
},
],
onError: '#buyingMoment',
},
},
showCurrentOrdersWarning: {},
},
},
empty: {},
error: {},
},
},
buyingMoment: {
id: 'buyingMoment',
on: {
RESET_PURCHASE: 'idle',
},
invoke: {
id: 'buyMoment',
src: 'buyMoment',
data: {
client: (context) => context.client,
flags: (context) => context.flags,
currentUrlLink: (context) => context.currentUrlLink,
moment: (context, event) => event.moment ?? context.moment,
executeRecaptcha: (context) => context.executeRecaptcha,
startPurchaseWhenInitialized: () => true,
},
onDone: '#success',
onError: {
target: '#error',
actions: ['assignError'],
},
},
},
error: {
id: 'error',
on: {
SELECT: '#select.fetch',
},
},
success: {
id: 'success',
type: 'final',
},
},
}, {
actions: {
assignGetListings: () => {},
assignSelectedMoment: () => {},
clearSelectedMoment:() => {},
assignSort: () => {},
assignFetchMoreError: () => {},
assignError: () => {},
},
guards: {
listingHasMoments: () => true,
hasExistingOrders: () => true,
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment