Skip to content

Instantly share code, notes, and snippets.

@scytalezero
Last active August 13, 2021 13:14
Show Gist options
  • Save scytalezero/b3d94d94fa1cfecd4886e4f0d20292b3 to your computer and use it in GitHub Desktop.
Save scytalezero/b3d94d94fa1cfecd4886e4f0d20292b3 to your computer and use it in GitHub Desktop.
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const fetchMachine = Machine({
id: 'member',
initial: 'NotSubscribed',
context: {
isAdmin: false,
hasStandard: false,
hasMulti: false,
},
on: {
SETADMIN: {
actions: 'setAdmin',
},
SETSTANDARD: {
actions: 'setStandard',
},
SETMULTI: {
actions: 'setMulti',
},
},
states: {
NotSubscribed: {
on: {
ADD_ADDON: 'Active',
}
},
Active: {
on: {
CANCEL_ALL: 'Canceled',
ADD_ADDON: 'Active',
CANCEL_ADDON: 'Active',
ADD_FACILITY: {
target: 'Active',
cond: 'canAdd',
},
SWITCH: 'Active',
WAIT: 'ExpiredStillEligible',
TERM: {
target: 'Expired',
cond: 'isAdmin',
}
}
},
Canceled: {
on: {
REACTIVATE: 'Active',
WAIT: 'Expired',
}
},
ExpiredStillEligible: {
on: {
WAIT: 'Expired',
}
},
Expired: {
on: {
SUBSCRIBE: 'Active',
}
}
}
},
{
actions: {
setAdmin: assign({ isAdmin: true }),
setStandard: assign({ hasStandard: true }),
setMulti: assign({ hasMulti: true }),
},
guards: {
isAdmin: context => context.isAdmin,
hasStandard: context => context.hasStandard,
canAdd: context => context.hasMulti && context.hasStandard,
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment