Skip to content

Instantly share code, notes, and snippets.

@the-main-thing
Last active April 9, 2020 12:53
Show Gist options
  • Save the-main-thing/70ede3dd06bd1bb04ab9f0b9cd53dd55 to your computer and use it in GitHub Desktop.
Save the-main-thing/70ede3dd06bd1bb04ab9f0b9cd53dd55 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const editinMachine = Machine({
id: 'editingMachine',
initial: 'idle',
context: {
id: null,
name: null,
email: null,
errorMessage: null,
},
on: {
ERROR: {
actions: ['setErrorMessage']
}
},
states: {
idle: {
on: {
ADD: {
target: 'editing',
actions: ['onNew', 'removeErrorMessage'],
},
EDIT: [{
target: 'editing',
actions: ['onEdit', 'removeErrorMessage'],
cond: 'isExistingRecord'
}, {
actions: ['setNoIdError']
}],
DELETE: [
{
cond: 'isExistingRecord',
target: 'delete',
actions: ['setDeleteId', 'removeErrorMessage'],
},
],
},
},
editing: {
on: {
CHANGE: {
actions: [
'removeErrorMessage',
'setEmailAndName'
]
},
SAVE: [{
target: 'saving',
actions: ['removeErrorMessage'],
cond: 'isEmailAndNameAreSet'
}, {
actions: ['setNoRequiredError']
}],
CANCEL: {
target: 'idle',
actions: ['removeErrorMessage'],
},
},
},
saving: {
invoke: {
src: 'saveMutation',
onDone: {
target: 'idle',
actions: ['resetContext'],
},
onError: {
target: 'editing',
actions: ['setErrorMessage'],
},
},
},
delete: {
on: {
CONFIRM: {
target: 'deleting',
actions: ['removeErrorMessage'],
},
CANCEL: {
target: 'idle',
actions: ['removeErrorMessage'],
},
},
},
deleting: {
invoke: {
src: 'deleteMutation',
onDone: {
target: 'idle',
actions: ['resetContext'],
},
onError: {
target: 'delete',
actions: ['setErrorMessage'],
},
},
},
},
}, {
guards: {
isExistingRecord: () => true,
isEmailAndNameAreSet: () => true
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment