Skip to content

Instantly share code, notes, and snippets.

@2WheelCoder

2WheelCoder/machine.js

Last active May 13, 2020
Embed
What would you like to do?
Generated by XState Viz: https://xstate.js.org/viz
const requestCheckout = () => Promise.resolve({ id: '12345',
webUrl: 'https://google.com'
})
const queryForShippingRates = () => Promise.resolve({
shippingRates: [{test: 'test'}]
})
const setShippingRate = () => Promise.resolve()
const checkoutMachine = Machine({
id: 'checkout',
initial: 'acceptingUserInfo',
context: {
checkoutId: null,
webUrl: null,
deliveryMethod: 'pickup',
client: {
mutate: () => {}
},
shippingRates: null,
selectedShippingRate: null
},
states: {
acceptingUserInfo: {
on: {
SUBMIT_INFO: 'creatingCheckout'
}
},
creatingCheckout: {
invoke: {
id: 'requestCheckout',
src: requestCheckout,
onDone: {
target: 'loadingShippingRates',
actions: 'updateCheckout'
},
onError: 'failure'
},
},
loadingShippingRates: {
invoke: {
id: 'queryForShippingRates',
src: queryForShippingRates,
onDone: {
target: 'acceptingShippingRate',
actions: 'updateShippingRates'
},
onError: 'failure'
},
},
acceptingShippingRate: {
// entry: ['setShippingOptionIfPickup'],
on: {
SELECT: {
target: 'settingShippingRate',
actions: ['updateSelectedShippingRate']
}
}
},
settingShippingRate: {
invoke: {
id: 'setShippingRate',
src: setShippingRate,
onDone: 'checkoutComplete',
onError: 'failure'
},
},
checkoutComplete: {
type: 'final'
},
failure: {
type: 'final'
}
}
}, {
actions: {
updateShippingRates: assign({
shippingRates: (context, event) => event.data.shippingRates
}),
updateCheckout: assign((context, event) => ({
webUrl: event.data.webUrl,
checkoutId: event.data.id
})),
updateSelectedShippingRate: assign({
selectedShippingRate: (context, event) => event.shippingRate
}),
// setShippingOptionIfPickup: choose([
// {
// cond: context => context.deliveryMethod === 'pickup',
// actions: [
// assign({
// selectedShippingRate: 'test1'
// })
// ]
// }
// ])
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.