Skip to content

Instantly share code, notes, and snippets.

@larrybotha
Created October 22, 2019 14:02
Show Gist options
  • Save larrybotha/41bcf51516e1ff26faa161b1b7daaea3 to your computer and use it in GitHub Desktop.
Save larrybotha/41bcf51516e1ff26faa161b1b7daaea3 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Luna Med user auth machine
const authUserMachine = Machine({
id: 'user',
initial: 'noUser',
context: {
error: undefined,
user: undefined,
},
states: {
noUser: {
on: {FETCH: 'fetching'},
},
fetching: {
invoke: {
id: 'fetchUser',
src: 'fetchUser',
onDone: {
actions: 'handleUserFound',
target: 'found',
},
onError: [
{
actions: 'handleError',
cond: 'userNotFound',
target: 'creating',
},
{
actions: 'handleError',
target: 'error',
},
],
},
on: {FETCH_CANCEL: 'noUser'},
},
creating: {
invoke: {
id: 'createUser',
src: 'createUser',
onDone: {
actions: 'handleUserFound',
target: 'found',
},
onError: {
actions: 'handleError',
target: 'error',
},
},
on: {CREATE_CANCEL: 'noUser'},
},
error: {
on: {FETCH: 'fetching'},
},
found: {
data: {
user: (context) => context.user,
},
type: 'final',
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment