Skip to content

Instantly share code, notes, and snippets.

@virtualdesigner
Last active May 24, 2021 12:51
Show Gist options
  • Save virtualdesigner/d660eeee50c92ad9371ac5d44cc5ebfb to your computer and use it in GitHub Desktop.
Save virtualdesigner/d660eeee50c92ad9371ac5d44cc5ebfb to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
Machine({
id: 'userMachine',
initial: 'notARegisteredUser',
context: {
user: {
hasBillableCard: false,
apiTransactionCount: 0,
hadApiTrial: false,
hadWebTrial: false,
apiPlanName: '',
webPlanName: ''
}
},
states: {
notARegisteredUser: {
on: {
REGISTER_USER: "registeredUser"
}
},
registeredUser: {
on: {
DELETE_USER: 'notARegisteredUser'
},
type: "parallel",
states: {
api: {
initial: 'notSubscribedApi',
// invoke: {
// id: 'apiEventListener',
// src: apiEventListener
// },
states: {
trialApi: {
initial: 'sentNothing',
entry: assign({
user: (context) => ({
...context.user, 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: 'subscribedApi',
UNPAID_API_WAIT_END: 'notSubscribedApi'
}
},
incompleteApi: {
entry: {
actions: assign({
hasApiDues: true
})
}
,
on: {
MOVE_TO_UNPAID: 'unpaidApi',
PAID_API: {
target: 'subscribedApi',
actions: assign({
hasApiDues: false
})
}
}
},
subscribedApi: {
on: {
UNSUBSCRIBE_API: "notSubscribedApi",
SUB_UNPAID_API: 'unpaidApi',
ADD_TRANSACTION_COUNT: {
actions: assign({
apiTransactionCount: (context,event) => context.user.apiTransactionCount + event.count
})
},
CHANGE_SUB_API: {
actions: assign({
apiPlanName: (event) => event.planName
})
}
}
},
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({
user: (context) => ({
...context.user, 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: 'subscribedWeb',
UNPAID_WEB_WAIT_END: 'notSubscribedWeb'
}
},
subscribedWeb: {
on: {
UNSUBSCRIBE_WEB: "notSubscribedWeb",
SUB_UNPAID_WEB: 'unpaidWeb',
CHANGE_SUB_WEB: {
actions: assign({
webPlanName: (event) => event.planName
})
}
}
},
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: 'noCard'
},
states: {
chargeable: {
entry: assign({
user: (context) => ({
...context.user, hasBillableCard: true
})
}),
on: {
MAKE_CARD_INACTIVE: 'notChargeable'
}
},
notChargeable: {
entry: assign({
user: (context) => ({
...context.user, 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
},
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment