Skip to content

Instantly share code, notes, and snippets.

@nahumt
Last active December 4, 2020 20:14
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 nahumt/0d68b589b2d7c778545f7453c0eda6d2 to your computer and use it in GitHub Desktop.
Save nahumt/0d68b589b2d7c778545f7453c0eda6d2 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: 'checkout',
initial: 'Brasil',
context: {
user: '',
paymentType: 'online',
},
states: {
'Brasil': {
on: {
GUEST: 'Method',
LOGIN: {
target: 'Method',
actions: 'onLogin',
}
},
meta: {
message: 'Loading'
}
},
'Method': {
on:{
CARD: 'paymentSection.Ebanxform',
PAYPAL: 'paymentSection.PaypalButton',
TRANSFER: {
target: 'paymentSection.EbanxOffline',
actions: 'setOffline',
}
},
},
paymentSection:{
initial: 'Ebanxform',
states: {
Ebanxform: '',
PaypalButton: '',
EbanxOffline: '',
},
onDone: 'processing',
},
processing: {
on: {
SUCCESS: 'checkingAuth',
FAILED: 'rejected',
}
},
checkingAuth: {
on: {
'': [
{
cond: 'isOnlineLogged',
target: 'Thanks',
},
{
cond: 'isOflineLogged',
target: 'transferUrl'
},
{
target: 'CheckoutActivation',
},
],
},
},
CheckoutActivation: {
on: {
SIGNUP: 'checkingMethod',
LOGIN: 'checkingMethod'
}
},
checkingMethod: {
on: {
'': [
{
cond: 'isOnline',
target: 'Thanks'
},
{
target: 'transferUrl'
}
]
}
},
Thanks: {
type: 'final'
},
transferUrl: {
type: 'final'
},
rejected: {
type: 'final'
},
}}, {
guards: {
isOnlineLogged: (context) => {
return !!context.user && context.paymentType === 'online';
},
isOflineLogged: (context) => {
return !!context.user && context.paymentType === 'offline';
},
isOnline: (context) => {
return context.paymentType === 'online'
}
},
actions: {
setOffline: assign({
paymentType: 'offline'
}),
onLogin: assign({
user: 'Lucas'
})
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment