Skip to content

Instantly share code, notes, and snippets.

@phpnode
Created July 22, 2020 09:19
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 phpnode/39aa9ff84cec8dd2f8e73369b1ad90f7 to your computer and use it in GitHub Desktop.
Save phpnode/39aa9ff84cec8dd2f8e73369b1ad90f7 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
function createApprovalMachine(context) {
return Machine({
id: 'approval',
initial: 'initializing',
context,
states: {
initializing: {
on: {
'': [
{ target: 'APPROVAL_STATE_UNKNOWN', cond: { type: 'isLevel', level: 0 } },
{ target: 'APPROVAL_STATE_DRAFT' }
]
}
},
APPROVAL_STATE_UNKNOWN: {
type: 'final'
},
APPROVAL_STATE_DRAFT: {
on: {
SUBMIT: [
{ target: 'APPROVAL_STATE_APPROVED', cond: { type: 'isLevel', level: 1 }, actions: ['sendApproval'] },
{ target: 'APPROVAL_STATE_SUBMITTED', cond: { type: 'isLevel', level: 2 }, actions: ['sendSubmitted'] }
]
}
},
APPROVAL_STATE_APPROVED: {
type: 'final'
},
APPROVAL_STATE_SUBMITTED: {
on: {
APPROVE: { target: 'APPROVAL_STATE_APPROVED', actions: ['sendApproval'] },
REQUEST_CHANGES: {
target: 'APPROVAL_STATE_REQUESTED_CHANGES',
actions: [assign({ notes: (context, event) => event.notes }), 'sendRequestChanges']
}
}
},
APPROVAL_STATE_REQUESTED_CHANGES: {
on: {
SUBMIT: { target: 'APPROVAL_STATE_SUBMITTED', actions: ['sendSubmitted'] }
}
}
}
}, {
actions: {
sendApproval: () => console.log('Approved'),
sendSubmitted: () => console.log('Submitted'),
sendRequestChanges: () => console.log('Requested Changes')
},
guards: {
isLevel: (context, event, { cond }) => context.level === cond.level
}
});
}
const approval = createApprovalMachine({ level: 1 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment