Skip to content

Instantly share code, notes, and snippets.

@wesleycoder
Last active July 11, 2021 02:35
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 wesleycoder/d3f5285684e467350718677605e73c8b to your computer and use it in GitHub Desktop.
Save wesleycoder/d3f5285684e467350718677605e73c8b to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const userStatusMachine = Machine({
id: 'userStatus',
initial: 'NEW',
context: {
userId: '12345',
validationAttempts: 0,
},
states: {
NEW: {
on: {
approved: 'WAITING_VALIDATION',
}
},
WAITING_VALIDATION: {
on: {
'': {
target: 'LIMITED',
cond: {
type: 'exceededMaxAttempts',
maxAttempts: 3
},
},
validated: 'OPERATIONAL',
rejected: 'BLOCKED',
submitValidation: {
target: '.',
actions: ['submitValidation']
},
}
},
OPERATIONAL: {
on: {
blocked: 'BLOCKED',
suspended: 'SUSPENDED',
limited: 'LIMITED',
validationExpired: 'WAITING_VALIDATION',
}
},
SUSPENDED: {
on: {
delete: 'DELETED',
manualApprove: 'OPERATIONAL',
}
},
LIMITED: {
on: {
delete: 'DELETED',
manualApprove: 'OPERATIONAL',
}
},
BLOCKED: {
on: {
delete: 'DELETED',
}
},
DELETED: {
type: 'final',
},
}
}, {
actions: {
submitValidation: assign((ctx) => ({
validationAttempts: ctx.validationAttempts + 1,
}))
},
guards: {
exceededMaxAttempts: ({ validationAttempts }, event, { maxAttempts }) => validationAttempts >= maxAttempts
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment