Skip to content

Instantly share code, notes, and snippets.

@2WheelCoder
Last active May 13, 2020 23:08
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 2WheelCoder/ababa3bb6d473973f29d9a8b84b7b27e to your computer and use it in GitHub Desktop.
Save 2WheelCoder/ababa3bb6d473973f29d9a8b84b7b27e to your computer and use it in GitHub Desktop.
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