Last active
May 25, 2021 07:31
-
-
Save virtualdesigner/b0e5f56c14f5292ba9e442d09d5330e0 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
| Machine({ | |
| id: 'userMachine', | |
| initial: 'notARegisteredUser', | |
| context: { | |
| user: { | |
| hasBillableCard: false, | |
| apiTransactionCount: 0, | |
| hadApiTrial: false, | |
| hadWebTrial: false, | |
| apiPlanName: '', | |
| webPlanName: '', | |
| hasApiDues: false, | |
| hasWebDues: false | |
| } | |
| }, | |
| states: { | |
| notARegisteredUser: { | |
| on: { | |
| REGISTER_USER: "registeredUser" | |
| } | |
| }, | |
| registeredUser: { | |
| on: { | |
| DELETE_USER: {target: 'notARegisteredUser', | |
| cond: 'hasNoDuesApiAndWeb'} | |
| }, | |
| type: "parallel", | |
| states: { | |
| api: { | |
| initial: 'notSubscribedApi', | |
| // invoke: { | |
| // id: 'apiEventListener', | |
| // src: apiEventListener | |
| // }, | |
| states: { | |
| trialApi: { | |
| initial: 'sentNothing', | |
| entry: assign({ | |
| hadApiTrial: true | |
| }), | |
| states: { | |
| sentReminder: {}, | |
| sentNothing: { | |
| on: { | |
| SEND_API_TRIAL_REMINDER: 'sentReminder' | |
| } | |
| } | |
| }, | |
| on: { | |
| TRIAL_END_API: [ | |
| { | |
| target: 'subscribedApi', | |
| cond: 'hasBillableCardAndTransacted' | |
| }, | |
| { | |
| target: 'notSubscribedApi' | |
| } | |
| ], | |
| TRIAL_WILL_END_API: { | |
| target: '.sentReminder', | |
| cond: 'NoTransactionOrNoBillableCard' | |
| }, | |
| SUBSCRIBE_API: {target: 'subscribedApi', | |
| actions: assign({ | |
| apiPlanName: (event) => event.planName | |
| }) | |
| }, | |
| UNSUBSCRIBE_API: 'notSubscribedApi', | |
| ADD_TRANSACTION_COUNT: { | |
| actions: assign({ | |
| apiTransactionCount: (context,event) => context.user.apiTransactionCount + event.count | |
| }) | |
| } | |
| } | |
| }, | |
| unpaidApi: { | |
| initial: 'sentNothing', | |
| states: { | |
| sentReminder: {}, | |
| sentNothing: { | |
| on: { | |
| SEND_API_UNPAID_REMINDER: 'sentReminder' | |
| } | |
| } | |
| }, | |
| on: { | |
| UNPAID_API_PAID: {target: 'subscribedApi', | |
| actions: assign({ | |
| hasApiDues: false | |
| }) | |
| }, | |
| UNPAID_API_WAIT_END: {target: 'notSubscribedApi', | |
| actions: assign({ | |
| hasApiDues: false | |
| }) | |
| } | |
| } | |
| }, | |
| subscriptionIncompleteApi: { | |
| entry: assign({ | |
| hasApiDues: true | |
| }) | |
| , | |
| on: { | |
| ALL_PAYMENT_TRIES_FAILED_API: 'unpaidApi', | |
| PAID_INCOMPLETE_API: { | |
| target: 'subscribedApi', | |
| actions: assign({ | |
| hasApiDues: false | |
| }) | |
| } | |
| } | |
| }, | |
| subscribedApi: { | |
| on: { | |
| UNSUBSCRIBE_API: "notSubscribedApi", | |
| ADD_TRANSACTION_COUNT: { | |
| actions: assign({ | |
| apiTransactionCount: (context,event) => context.user.apiTransactionCount + event.count | |
| }) | |
| }, | |
| CHANGE_SUB_API: { | |
| actions: assign({ | |
| apiPlanName: (event) => event.planName | |
| }) | |
| }, | |
| PAYMENT_FAILED_API: 'subscriptionIncompleteApi' | |
| } | |
| }, | |
| notSubscribedApi: { | |
| id: 'notSubscribedApi', | |
| on: { | |
| SUBSCRIBE_API: { | |
| target: "subscribedApi", | |
| actions: assign({ | |
| apiPlanName: (event) => event.planName | |
| }) | |
| }, | |
| ACTIVATE_TRIAL_API: { | |
| target: 'trialApi', | |
| cond: (context) => !context.user.hadApiTrial | |
| } | |
| } | |
| } | |
| }, | |
| }, | |
| web: { | |
| initial: 'notSubscribedWeb', | |
| // invoke: { | |
| // id: 'webEventListener', | |
| // src: webEventListener | |
| // }, | |
| states: { | |
| trialWeb: { | |
| initial: 'sentNothing', | |
| entry: assign({ | |
| hadWebTrial: true | |
| }), | |
| states: { | |
| sentReminder: {}, | |
| sentNothing: { | |
| on: { | |
| SEND_WEB_TRIAL_REMINDER: 'sentReminder' | |
| } | |
| } | |
| }, | |
| on: { | |
| TRIAL_END_WEB: [ | |
| { | |
| target: 'subscribedWeb', | |
| cond: 'hasBillableCard' | |
| }, | |
| { | |
| target: 'notSubscribedWeb' | |
| } | |
| ], | |
| TRIAL_WILL_END_WEB: { | |
| target: '.sentReminder', | |
| cond: 'hasNoBillableCard' | |
| }, | |
| SUBSCRIBE_WEB: {target: 'subscribedWeb', | |
| actions: assign({ | |
| webPlanName: (event) => event.planName | |
| }) | |
| }, | |
| UNSUBSCRIBE_WEB: 'notSubscribedWeb' | |
| } | |
| }, | |
| unpaidWeb: { | |
| initial: 'sentNothing', | |
| states: { | |
| sentReminder: {}, | |
| sentNothing: { | |
| on: { | |
| SEND_WEB_UNPAID_REMINDER: 'sentReminder' | |
| } | |
| } | |
| }, | |
| on: { | |
| UNPAID_WEB_PAID: {target: 'subscribedWeb', | |
| actions: assign({ | |
| hasWebDues: false | |
| }) | |
| }, | |
| UNPAID_WEB_WAIT_END: {target: 'notSubscribedWeb', | |
| actions: assign({ | |
| hasWebDues: false | |
| }) | |
| } | |
| } | |
| }, | |
| subscriptionIncompleteWeb: { | |
| entry: assign({ | |
| hasWebDues: true | |
| }) | |
| , | |
| on: { | |
| ALL_PAYMENT_TRIES_FAILED_WEB: 'unpaidWeb', | |
| PAID_INCOMPLETE_WEB: { | |
| target: 'subscribedWeb', | |
| actions: assign({ | |
| hasWebDues: false | |
| }) | |
| } | |
| } | |
| }, | |
| subscribedWeb: { | |
| on: { | |
| UNSUBSCRIBE_WEB: "notSubscribedWeb", | |
| CHANGE_SUB_WEB: { | |
| actions: assign({ | |
| webPlanName: (event) => event.planName | |
| }) | |
| }, | |
| PAYMENT_FAILED_WEB: 'subscriptionIncompleteWeb' | |
| } | |
| }, | |
| notSubscribedWeb: { | |
| id: 'notSubscribedWeb', | |
| on: { | |
| SUBSCRIBE_WEB: {target: 'subscribedWeb', | |
| actions: assign({ | |
| webPlanName: (event) => event.planName | |
| }) | |
| }, | |
| ACTIVATE_TRIAL_WEB: { | |
| target: 'trialWeb', | |
| cond: (context) => !context.user.hadWebTrial | |
| } | |
| } | |
| } | |
| }, | |
| }, | |
| card: { | |
| initial: 'noCard', | |
| states: { | |
| noCard: { | |
| on: { | |
| ADD_CARD: 'hasCard', | |
| } | |
| }, | |
| hasCard: { | |
| initial: 'chargeable', | |
| on: { | |
| REMOVE_CARD: { target: 'noCard', actions: assign({ | |
| hasBillableCard: false | |
| }) } | |
| }, | |
| states: { | |
| chargeable: { | |
| entry: assign({ | |
| hasBillableCard: true | |
| }), | |
| on: { | |
| MAKE_CARD_INACTIVE: 'notChargeable' | |
| } | |
| }, | |
| notChargeable: { | |
| entry: assign({ | |
| hasBillableCard: false | |
| }), | |
| on: { | |
| MAKE_CARD_ACTIVE: 'chargeable', | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| }, | |
| { | |
| guards: { | |
| hasBillableCardAndTransacted: (context, event) => { | |
| return context.user.apiTransactionCount > 0 && context.user.hasBillableCard | |
| }, | |
| NoTransactionOrNoBillableCard: (context, event) => { | |
| return context.user.apiTransactionCount === 0 || !context.user.hasBillableCard | |
| }, | |
| hasBillableCard: (context, event) => { | |
| return context.user.hasBillableCard | |
| }, | |
| hasNoBillableCard: (context, event) => { | |
| return !context.user.hasBillableCard | |
| }, | |
| hasTransacted: (context, event) => { | |
| return context.user.apiTransactionCount > 0 | |
| }, | |
| hasNoDuesApiAndWeb: (context) => { | |
| return !context.hasApiDues && !context.hasWebDues | |
| } | |
| } | |
| } | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment