Skip to content

Instantly share code, notes, and snippets.

@davidgovea
Created November 24, 2020 10:15
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 davidgovea/4e4c07c18b4be4d68ce0e01d14d5606c to your computer and use it in GitHub Desktop.
Save davidgovea/4e4c07c18b4be4d68ce0e01d14d5606c 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 fetchMachine = Machine(
{
id: 'startReset',
initial: 'boot',
context: {
error: undefined,
resetKey: undefined,
userId: undefined,
},
states: {
boot: {
always: [
{ target: 'loggedIn', cond: 'isLoggedIn' },
{ target: 'submit', cond: 'hasResetKey' },
{ target: 'forgot' },
],
},
loggedIn: {
on: {
LOGOUT: 'forgot',
},
},
forgot: {
on: {
SUBMIT: 'starting',
// MANUAL_KEY: 'manualKey',
ADD_KEY: {
target: 'submit',
actions: assign({ resetKey: (_context, event) => event.payload }),
},
},
},
starting: {
invoke: {
src: 'startReset',
onDone: 'email',
onError: {
target: 'forgot',
actions: assign({ error: (_context, event) => event.data }),
},
},
},
email: {
on: {
// MANUAL_KEY: 'manualKey',
ADD_KEY: {
target: 'submit',
actions: assign({ resetKey: (_context, event) => event.payload }),
},
},
},
// manualKey: {
// on: {
// ADD_KEY: {
// target: 'submit',
// actions: assign({ resetKey: (_context, event) => event.payload }),
// },
// },
// },
submit: {
initial: 'reset',
states: {
reset: {
invoke: {
src: 'checkKey',
onDone: 'newPassword',
onError: 'expired',
},
},
expired: {
type: 'final',
},
newPassword: {
on: {
SUBMIT: 'submitting',
},
},
submitting: {
invoke: {
src: 'performReset',
onDone: 'complete',
onError: {
target: 'reset',
actions: assign({ error: (context, event) => event.data }),
},
},
},
complete: {
type: 'final',
},
},
},
},
},
{
guards: {
isLoggedIn: (context) => !!context.userId,
hasResetKey: (context) => !!context.resetKey,
},
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment