Skip to content

Instantly share code, notes, and snippets.

@simonmd
Last active November 6, 2020 21:18
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 simonmd/3269e2118f57a51e7a608d820f9dd5b0 to your computer and use it in GitHub Desktop.
Save simonmd/3269e2118f57a51e7a608d820f9dd5b0 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const workOrderMachine = Machine({
id: 'workOrder',
initial: 'created',
context: {
userId: '',
userRole: '',
work_order_id: 1,
reportCreated: false,
previousState: "here goes the previous state"
},
states: {
created: {
entry: 'setPreviousState',
on: {
VALIDATE: {
target: 'validated',
actions: ["createReport"]
},
REJECT: 'rejected',
CANCEL: 'cancelled'
}
},
validated: {
entry: 'setPreviousState',
on: {
REJECT: 'rejected',
CANCEL: 'cancelled',
REMOVE: 'removed'
}
},
rejected: {
entry: 'setPreviousState',
on: {
REJECT: 'rejected',
CANCEL: 'cancelled',
REMOVE: 'removed',
RESOLVE: [
{target: 'created', cond: 'canResolveToCreated'},
{target: 'validated', cond: 'canResolveToValidated'}
]
}
},
cancelled: {
type: 'final'
},
removed: {}
}
},
{
actions: {
createReport: (context, _event) => {
context.reportCreated = true
},
setPreviousState: (context, _event, meta) => {
context.previousState = meta.state?.history?.value
}
},
activities: {
/* ... */
},
delays: {
/* ... */
},
guards: {
canResolveToCreated: (context) => {
// Not correct behavior for rejected to rejected
return context.previousState == 'created' || context.userRole === 'client_admin'
// Also check for no open issues
},
canResolveToValidated: (context) => {
// Not correct behavior for rejected to rejected
return context.previousState == 'validated' || context.userRole === 'client_admin'
// Also check for no open issues
},
},
services: {
/* ... */
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment