Skip to content

Instantly share code, notes, and snippets.

@nahumt
Last active December 4, 2020 20:12
Show Gist options
  • Save nahumt/6b0b2ac853ff96b96bb868b71467deef to your computer and use it in GitHub Desktop.
Save nahumt/6b0b2ac853ff96b96bb868b71467deef 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: 'Colombia',
context: {
user: '',
paymentType: 'online',
},
states: {
'Colombia': {
on: {
GUEST: 'Method',
LOGIN: {
target: 'Method',
actions: 'onLogin',
}
},
meta: {
message: 'Loading'
}
},
'Method': {
on:{
CARD: 'paymentSection.Stripeform',
PSE: {
actions: 'setOffline',
target: 'paymentSection.PSEForm',
} ,
PAYPAL: 'paymentSection.PaypalButton',
CASH: {
target: 'paymentSection.Efecty',
actions: 'setOffline',
}
},
},
paymentSection:{
initial: 'Stripeform',
states: {
Stripeform: '',
PSEForm: '',
PaypalButton: '',
Efecty: '',
},
onDone: 'processing',
},
processing: {
on: {
SUCCESS: 'checkingAuth',
FAILED: 'rejected',
}
},
checkingAuth: {
on: {
'': [
{
cond: 'isOnlineLogged',
target: 'Thanks',
},
{
cond: 'isOflineLogged',
target: 'payOrder'
},
{
target: 'CheckoutActivation',
},
],
},
},
CheckoutActivation: {
on: {
SIGNUP: 'checkingMethod',
LOGIN: 'checkingMethod'
}
},
checkingMethod: {
on: {
'': [
{
cond: 'isOnline',
target: 'Thanks'
},
{
target: 'payOrder'
}
]
}
},
Thanks: {
type: 'final'
},
payOrder: {
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