Skip to content

Instantly share code, notes, and snippets.

@signaes
Last active August 25, 2020 23:00
Show Gist options
  • Save signaes/98b1436dab977a82fc85f0cc7acb65bd to your computer and use it in GitHub Desktop.
Save signaes/98b1436dab977a82fc85f0cc7acb65bd 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 programsMachine = Machine({
id: 'programs',
initial: 'idle',
context: {
workoutsSource: null,
hasData: false,
},
states: {
idle: {
on: {
LAUNCH: [
{
target: 'list',
cond: 'hasData',
},
{
target: 'empty'
}
]
}
},
list: {
on: {
CANCEL: 'list',
CREATE: [
{
target: 'create',
cond: 'hasSelectedWorkoutsSource',
},
{
target: 'select_workouts_source'
}
],
EDIT: 'edit',
}
},
empty: {
on: {
CANCEL: 'empty',
CREATE: 'select_workouts_source',
}
},
select_workouts_source: {
on: {
SELECT: 'create',
}
},
create: {
initial: 'idle',
states: {
idle: {
on: {
CANCEL: [
{
target: '#programs.list',
cond: 'hasData',
},
{
target: '#programs.empty'
}
],
DEFINE_WORKOUTS: 'define_workouts',
EDIT_WORKOUTS: 'edit_workouts',
SAVE: '#programs.list',
}
},
define_workouts: {
on: {
DEFINE: 'idle'
}
},
edit_workouts: {
on: {
SAVE: 'idle'
}
}
},
},
edit: {
initial: 'idle',
states: {
idle: {
on: {
CANCEL: [
{
target: '#programs.list',
cond: 'hasData',
},
{
target: '#programs.empty'
}
],
DEFINE_WORKOUTS: 'define_workouts',
EDIT_WORKOUTS: 'edit_workouts',
SAVE: '#programs.list',
}
},
define_workouts: {
on: {
DEFINE: 'idle'
}
},
edit_workouts: {
on: {
SAVE: 'idle'
}
}
},
},
}
},
{
guards: {
hasData: ({ hasData }) => !!hasData,
hasSelectedWorkoutsSource: ({ workoutsSource }) => !!workoutsSource
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment