Skip to content

Instantly share code, notes, and snippets.

@dimaip
Created May 13, 2021 16:25
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 dimaip/8379463eac79040e66b548690070698f to your computer and use it in GitHub Desktop.
Save dimaip/8379463eac79040e66b548690070698f 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 buyMachine = Machine({
id: 'buy',
initial: 'outside',
context: {
loggedIn: false,
subscription: null,
bookId: null,
initialLocation: null,
alreadySubscribedChosen: false
},
states: {
outside: {
on: {
CLICK_PROTECTED: [
{
target: 'bookView',
cond: 'subscribed'
},
{
target: 'paywall'
}]
}
},
bookView: {
type: 'final'
},
paywall: {
always: [{
target: 'bookView',
cond: 'subscribed'
}],
on: {
SUBSCRIBE: {
target: 'authenticating',
},
ALREADY_SUBSCRIBED: {
target: 'authenticating',
action: assign({
alreadySubscribedChosen: true
}),
},
CLOSE: {
target: 'outside',
actions: 'redirectToInitial'
}
}
},
authenticating: {
always: [{
target: 'bookView',
cond: 'loggedIn'
}],
invoke: {
id: "authenticateUser",
src: "authenticateUser",
onDone: {
target: "hasSubscription",
},
onError: {
target: "paymentFailure"
}
}
},
hasSubscription: {
always: [{
target: 'bookView',
cond: 'subscribed'
},
{
target: 'noSubscription',
cond: 'alreadySubscribedChosen'
},
{
target: 'payment'
}],
},
noSubscription: {
on: {
SUBSCRIBE: 'payment',
CLOSE: {
target: 'outside',
actions: 'redirectToInitial'
}
}
},
payment: {
always: [{
target: 'bookView',
cond: 'loggedIn'
}],
invoke: {
id: "pay",
src: "pay",
onDone: {
target: "paymentSuccess",
},
onError: {
target: "paymentFailure"
}
}
},
paymentSuccess: {
on: {
OK: {
target: 'bookView'
}
}
},
paymentFailure: {
on: {
RETRY: {
target: 'payment'
}
}
}
}
}, {
guards: {
subscribed: (c, e) => Boolean(c.subscription),
alreadySubscribedChosen: (c, e) => Boolean(c.alreadySubscribedChosen)
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment