Created
March 16, 2021 05:20
-
-
Save adamscott/a9cb72ba676cc0759a90b9dce4f361df to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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