Skip to content

Instantly share code, notes, and snippets.

@jesstelford
Last active March 26, 2020 13:42
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 jesstelford/df265244645ddbd8aa27c61e31deac1c to your computer and use it in GitHub Desktop.
Save jesstelford/df265244645ddbd8aa27c61e31deac1c 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 formMachine = Machine({
id: 'formMachine',
context: {
authedUser: null,
},
initial: 'form',
states: {
form: {
on: {
INPUT_ERROR: 'formError',
SAVE: 'saving',
EDIT: 'form',
}
},
formError: {
on: {
EDIT: 'form'
}
},
saving: {
on: {
SUCCESS: 'success',
ERROR: 'formError'
}
},
success: {
type: 'final',
// Since this state doesn't handle the event, it bubbles up
entry: sendParent('SUCCESS'),
},
},
});
// While in `loading` state, sending an event of 'SUCCESS' expects an object with key 'authedUser' to be the currently authenticated user (or not)
const fetchMachine = Machine({
id: 'completeSetup',
context: {
authedUser: null,
},
initial: 'loading',
states: {
loading: {
on: {
'': { target: 'form', cond: ({ authedUser }) => authedUser },
ERROR: 'loadError',
SUCCESS: {
target: 'form',
actions: assign({
authedUser: (context, event) => event.authedUser,
}),
}
}
},
loadError: {
type: 'final',
},
form: {
initial: 'changePassword',
states: {
changePassword: {
invoke: {
id: 'formMachine',
src: formMachine,
data: {
authedUser: (context) => context.authedUser,
}
},
on: {
SUCCESS: [
{ target: 'success', cond: ({ authedUser }) => authedUser },
{ target: 'signin', cond: ({ authedUser }) => !authedUser },
],
},
},
signin: {
invoke: {
id: 'formMachine',
src: formMachine,
data: {
authedUser: (context) => context.authedUser,
}
},
on: {
SUCCESS: 'success',
},
},
success: {
type: 'final',
entry: sendParent('SUCCESS'),
}
}
}
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment