Created
February 9, 2021 14:33
-
-
Save Lorezz/5ec4a2a334414165b993020e586a52bb to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const saveOrder = assign({ | |
order: (context, event) => { | |
console.log('ev', event); | |
return event.data; | |
}, | |
}); | |
function hasCustomer(context, event) { | |
console.log('event', event); | |
console.log('order', context.order); | |
return context.order.customer_email != null; | |
} | |
const checkoutMachine = Machine( | |
{ | |
id: 'checkout', | |
initial: 'fetch_order', | |
context: { | |
order: null, | |
}, | |
states: { | |
fetch_order: { | |
on: { | |
RECEIVED: [ | |
{ | |
target: 'choose_address', | |
actions: saveOrder, | |
cond: hasCustomer, | |
}, | |
{ | |
target: 'create_address', | |
actions: saveOrder, | |
}, | |
], | |
}, | |
}, | |
create_address: { | |
on: { | |
DONE: 'choose_address', | |
actions: saveOrder, | |
}, | |
}, | |
choose_address: { | |
on: { | |
DONE: { | |
target: 'choose_payment', | |
actions: saveOrder, | |
}, | |
NEW: 'create_address', | |
}, | |
}, | |
choose_payment: { | |
on: { | |
DONE: { | |
target: 'payment', | |
actions: saveOrder, | |
}, | |
}, | |
}, | |
payment: { | |
on: { | |
DONE: { | |
target: 'place', | |
actions: saveOrder, | |
}, | |
CHANGE: 'choose_payment', | |
}, | |
}, | |
place: { | |
type: 'final', | |
}, | |
}, | |
}, | |
{ | |
actions: { | |
saveOrder, | |
}, | |
guards: { | |
hasCustomer, | |
}, | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment