Skip to content

Instantly share code, notes, and snippets.

@junoatwork
Last active February 9, 2021 04:49
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 junoatwork/81bfa1d633810579e8d13a9ca6cb898c to your computer and use it in GitHub Desktop.
Save junoatwork/81bfa1d633810579e8d13a9ca6cb898c to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
/* eslint-disable no-undef */
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const toInactive = [
{ target: 'COMPLIMENTARY', cond: 'isAdmin' },
{ target: 'ORGANIZATION', cond: 'isOrgUser' },
{ target: 'INACTIVE' },
];
const fetchMachine = Machine(
{
id: 'subscription',
initial: 'TRIAL',
context: {
period: 1,
isConfirmed: false,
paymentEntered: false,
isAdmin: false,
isOrgUser: false,
},
on: {
makeAdmin: { target: '.LAPSED', actions: ['setAdmin'] },
},
states: {
TRIAL: {
initial: '_',
states: {
_: {
on: {
'': [
{ target: 'PAID_TRIAL', cond: 'paymentEntered' },
{ target: 'FREE_TRIAL', cond: 'isConfirmed' },
{ target: 'UNCONFIRMED_TRIAL' },
],
},
},
UNCONFIRMED_TRIAL: {},
FREE_TRIAL: {},
PAID_TRIAL: {},
},
on: {
confirm: {
target: '._',
actions: ['setConfirmed'],
},
enterCard: {
target: '._',
actions: ['setPaymentEntered'],
},
periodEnd: [
{
target: 'PAYING',
cond: 'paymentEntered',
actions: ['newPeriod'],
},
{ target: 'LAPSED' },
],
},
},
PAYING: {
initial: 'PAID_ACTIVE',
states: {
PAID_ACTIVE: {},
AWAITING_PAYMENT: {},
},
on: {
lapse: 'LAPSED',
paymentFail: '.AWAITING_PAYMENT',
paymentSuccess: '.PAID_ACTIVE',
periodEnd: [{ target: '.PAID_ACTIVE', actions: ['newPeriod'] }],
},
},
LAPSED: {
initial: '_',
states: {
_: {
on: {
'': [
{
target: 'ORGANIZATION',
cond: 'isOrgUser',
actions: ['newIndefPeriod'],
},
{
target: 'COMPLIMENTARY',
cond: 'isAdmin',
actions: ['newIndefPeriod'],
},
{ target: 'INACTIVE', actions: ['newIndefPeriod'] },
],
},
},
INACTIVE: {},
ORGANIZATION: {},
COMPLIMENTARY: {},
},
on: {
reactivate: {target:'PAYING', cond: 'paymentEntered'},
reactivateWithTrial: 'TRIAL',
},
},
},
},
{
actions: {
'setConfirmed': assign({ isConfirmed: true }),
'setAdmin': assign({ isAdmin: true }),
'setPaymentEntered': assign({ paymentEntered: true }),
'newPeriod': assign({ period: (ctx) => ctx.period + 1 }),
'newIndefPeriod': assign({ period: (ctx) => ctx.period + 1 }),
},
guards: {
'isAdmin': (ctx) => ctx.isAdmin,
'isOrgUser': (ctx) => ctx.isOrgUser,
'isConfirmed': (ctx) => ctx.isConfirmed,
'paymentEntered': (ctx) => ctx.paymentEntered,
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment