Skip to content

Instantly share code, notes, and snippets.

@signaes
Created September 21, 2020 20:32
Show Gist options
  • Save signaes/bef20b57ca0b20d8100cb2db3f192d9e to your computer and use it in GitHub Desktop.
Save signaes/bef20b57ca0b20d8100cb2db3f192d9e 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 defineWorkoutMachine = Machine(
{
id: 'defineWorkoutMachine',
initial: 'idle',
context: {
id: null,
title: '',
description: '',
createdAt: '',
startAt: '',
endAt: '',
unchangedExercises: [],
exercises: [],
search: null,
clientId: null,
},
states: {
idle: {
on: {
EDIT: {
target: 'editing',
actions: sendParent(({ id }) => {
return { type: 'WORKOUT.EDIT', id }
}),
},
},
},
editing: {
on: {
'EXERCISE.TOGGLE': {
target: 'editing',
},
'EXERCISE.ADD': {
target: 'editing',
actions: 'addExercise',
},
'SEARCH.CANCEL': 'editing',
TOGGLE_ACTIVE: {
target: 'toggling_existent',
},
CREATE: {
target: 'creating',
},
UPDATE: {
target: 'updating',
},
SEARCH: 'searching',
'EXERCISE_DETAILS.UPDATE': {
target: 'editing',
},
'EXERCISE_DETAILS.SAVE': 'editing',
'EXERCISE_DETAILS.UNDO_CHANGES': 'editing',
'EXERCISE.REMOVE': {
target: 'editing',
},
DRAG_EXERCISE: {
target: 'editing',
},
DELETE: {
target: 'deleting',
actions: [],
},
CANCEL: {
target: 'idle',
},
},
},
creating: {
on: {
DONE: {
target: 'idle',
},
ERROR: {
target: 'unnable_to_create',
actions: 'handle_error',
},
},
},
updating: {
on: {
DONE: {
target: 'idle',
},
ERROR: {
target: 'unnable_to_update',
actions: 'handle_error',
},
},
},
deleting: {
invoke: {
id: 'delete',
src: 'delete',
onDone: {
target: 'idle',
},
onError: {
target: 'unnable_to_update',
actions: 'handle_error',
},
},
},
toggling_existent: {
invoke: {
src: 'toggle_active',
onDone: {
target: 'idle',
actions: [],
},
onFailure: 'failure',
},
},
searching: {
on: {
'EXERCISE.ADD': {
target: 'editing',
actions: 'addExercise',
},
'EXERCISE.TOGGLE': {
target: 'editing',
},
'SEARCH.CANCEL': 'editing',
},
},
unnable_to_create: {
on: {
RETRY: 'creating',
},
},
unnable_to_update: {
on: {
RETRY: 'updating',
},
},
},
},
{
actions: {
handle_error: assign({
error: (ctx, e) => {
console.error(e)
return e.error
},
}),
},
}
)
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment