Skip to content

Instantly share code, notes, and snippets.

@korshunad
Last active June 29, 2021 23:18
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/c1681f067806ee366cdbe9e304ef0b2f to your computer and use it in GitHub Desktop.
Save korshunad/c1681f067806ee366cdbe9e304ef0b2f 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_modal',
},
},
select_modal: {
id: 'select_modal',
type: 'parallel',
on: {
CANCEL: { target: '#idle', actions: 'clearSelectedMoment' },
},
states: {
listings_table: {
id: 'listings_table',
initial: 'fetch',
entry: 'clearSelectedMoment',
on: {
REFETCH: '#fetch_listings',
},
states: {
fetch: {
id: 'fetch_listings',
invoke: {
src: 'getListings',
onDone: [
{
target: '#idle_listings',
cond: 'listingHasMoments',
actions: ['assignGetListings'],
},
{
target: '#no_listings',
},
],
onError: '#errored_listings',
},
},
empty: {
id: 'no_listings',
},
error: {
id: 'errored_listings',
},
fetch_more: {
id: 'fetch_more_listings',
invoke: {
src: 'getListings',
onDone: [
{
target: '#idle_listings',
actions: ['assignGetListings'],
},
],
onError: {
target: '#idle_listings',
actions: ['assignFetchMoreError'],
},
},
},
idle_listings: {
id: 'idle_listings',
on: {
SELECT_MOMENT: {
actions: 'assignSelectedMoment',
target: '#noCurrentOrders',
},
CLEAR_SELECTION: {
actions: 'clearSelectedMoment',
target: '#noCurrentOrders',
},
SET_SORT: {
target: '#fetch_listings',
actions: ['assignSort'],
},
FETCH_MORE: {
target: '#fetch_more_listings',
},
BUY: [
{
target: '#fetchCurrentOrders',
cond: 'shouldCheckExistingOrders',
},
{
target: '#buyingMoment',
},
],
},
},
},
},
currentOrdersWarning: {
id: 'currentOrdersWarning',
initial: 'noCurrentOrders',
states: {
noCurrentOrders: {
id: 'noCurrentOrders',
},
fetchCurrentOrders: {
id: 'fetchCurrentOrders',
invoke: {
src: 'checkExistingOrders',
onDone: [
{
target: '#showWarning',
cond: 'hasExistingOrders',
},
{
target: '#buyingMoment',
},
],
onError: '#buyingMoment',
},
},
showWarning: {
id: 'showWarning',
on: {
CONTINUE_PURCHASE: {
target: '#buyingMoment',
},
},
},
},
},
},
},
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: '#fetch_listings',
},
},
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