Skip to content

Instantly share code, notes, and snippets.

@jankei
Created January 3, 2020 22:14
Show Gist options
  • Save jankei/2b292ecc5dc22ab51ac7988f500b188a to your computer and use it in GitHub Desktop.
Save jankei/2b292ecc5dc22ab51ac7988f500b188a to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const prebillerVerifiedMachine = Machine(
{
id: 'prebillerVerified',
initial: 'disabled',
context: {
warnings: [],
},
states: {
disabled: {
id: 'disabled',
on: {
ENABLE: {
target: 'enabled',
cond: 'noWarnings',
},
ADD_WARNING: {
actions: ['addWarning'],
},
REMOVE_WARNING: {
actions: ['removeWarning'],
},
},
},
enabled: {
initial: 'unchecked',
states: {
unchecked: {
on: {
CHECK: 'checked',
},
},
checked: {
on: { UNCHECK: 'unchecked' },
},
},
},
},
},
{
actions: {
addWarning: assign({
warnings: (context, event) => context.warnings.splice(event.index, 0, event.message),
}),
removeWarning: assign({
warnings: (context, event) => {
if (event.index && event.message) {
context.warnings.splice(event.index, 0, event.message).filter(Boolean);
} else return event.warnings;
},
}),
},
guards: {
noWarnings: context => context.warnings.length === 0,
},
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment