Skip to content

Instantly share code, notes, and snippets.

@the-main-thing
Last active March 26, 2020 09:03
Show Gist options
  • Save the-main-thing/0fb764702c7ef8ab54fc165d65a2b122 to your computer and use it in GitHub Desktop.
Save the-main-thing/0fb764702c7ef8ab54fc165d65a2b122 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const CREATING_NEW_DOC = true
const workPeriodMachine = Machine({
id: 'work-period',
initial: 'idle',
context: {},
states: {
idle: {
on: {
SAVE: {
target: 'saving'
},
DELETE: {
target: 'confirmDelete'
},
CHANGE: {
actions: ['updateContext']
},
RESET: {
actions: ['resetContext']
}
}
},
saving: {
on: {
'': [
{
target: 'savingNew',
cond: 'creatingNewDoc'
},
{
target: 'savingChange',
cond: 'editingExistingDoc'
}
]
}
},
savingNew: {
invoke: {
src: 'saveNewMutation',
onDone: {
target: 'idle',
actions: ['updateContext']
},
onError: {
target: 'mutationError'
}
}
},
savingChange: {
invoke: {
src: 'saveChangesMutation',
onDone: {
target: 'idle',
},
onError: {
target: 'mutationError'
}
}
},
confirmDelete: {
on: {
CONFIRM: 'deleting',
CANCEL: 'idle'
}
},
deleting: {
invoke: {
'src': 'removeMutation',
onDone: {
target: 'deleted'
},
onError: {
target: 'mutationError',
}
}
},
mutationError: {
on: {
CHANGE: 'idle',
DELETE: 'confirmDelete',
SAVE: 'saving',
RESET: {
target: 'idle',
actions: ['resetContext']
}
}
},
deleted: {
type: 'final'
}
}
}, {
guards: {
creatingNewDoc: () => CREATING_NEW_DOC,
editingExistingDoc: () => !CREATING_NEW_DOC
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment