Last active
May 28, 2021 14:24
-
-
Save EQuimper/c3631b6ba22ad9db81ed64f71e35e024 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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 paymentService = (context, event) => | |
new Promise((resolve, reject) => { | |
// TODO: change this, it's MOCK | |
setTimeout(() => { | |
if (context.plan === 'annual') { | |
return reject('something went wrong'); | |
} | |
return resolve(); | |
}, 2000); | |
}); | |
const subscribeToCoachMachine = Machine({ | |
id: 'subscribeToCoach', | |
context: { | |
plan: 'free', | |
error: undefined, | |
}, | |
initial: 'planSelection', | |
states: { | |
planSelection: { | |
on: { | |
SUBSCRIBE: [ | |
// only go to payment if the plan is not a free one | |
{ target: 'enterPaymentInfo', cond: 'notFreePlan' }, | |
{ target: 'subscribed', cond: 'isFreePlan' }, | |
], | |
SELECT: { | |
actions: 'selectPlan', | |
}, | |
}, | |
}, | |
enterPaymentInfo: { | |
on: { | |
PAYMENT: { | |
target: 'paymentProcess', | |
}, | |
}, | |
}, | |
paymentProcess: { | |
invoke: { | |
id: 'paymentService', | |
src: paymentService, | |
onDone: { | |
target: 'subscribed', | |
actions: [], | |
}, | |
onError: { | |
target: 'enterPaymentInfo', | |
actions: assign({ error: (context, event) => event.data }), | |
}, | |
}, | |
}, | |
subscribed: {}, | |
}, | |
}, | |
{ | |
actions: { | |
selectPlan: assign({ | |
plan: (_, event) => event.plan, | |
}), | |
}, | |
guards: { | |
notFreePlan: (context) => { | |
return context.plan !== 'free'; | |
}, | |
isFreePlan: (context) => { | |
return context.plan === 'free'; | |
}, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment