Skip to content

Instantly share code, notes, and snippets.

@adamscott
Created March 16, 2021 05:20
Show Gist options
  • Save adamscott/a9cb72ba676cc0759a90b9dce4f361df to your computer and use it in GitHub Desktop.
Save adamscott/a9cb72ba676cc0759a90b9dce4f361df 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 fetchJwt = (loginData) => {
return fetch(`url/to/user/${loginData}`).then((response) => response.json());
};
const loginMachine = Machine({
id: 'login',
initial: 'idle',
context: {
retry: 0
},
states: {
idle: {
on: {
FETCH: 'loading'
}
},
loading: {
invoke: {
id: 'getJwt',
src: (context, event) => fetchJwt(event.login),
onDone: {
target: 'success',
actions: assign({ user: (context, event) => event.data })
},
onError: {
target: 'failure',
actions: assign({ error: (context, event) => event.data })
}
}
},
success: {
type: 'final'
},
failure: {}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment