Skip to content

Instantly share code, notes, and snippets.

@wlee221
Last active May 5, 2021 17:32
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 wlee221/9e18e5c3e8388620e67ebda52fa8be39 to your computer and use it in GitHub Desktop.
Save wlee221/9e18e5c3e8388620e67ebda52fa8be39 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: "signIn",
initial: "edit",
states: {
edit: {
type: "parallel",
states: {
inputError: {
initial: "none",
states: { none: {}, error: {} }
},
submitError: {
initial: "none",
states: { none: {}, error: {} }
}
},
on: {
SUBMIT: "validate",
INPUT: {
actions: "onInput"
},
ERROR: [
{
target: ".inputError.error",
actions: "setInputError",
cond: "hasInputError"
},
{
target: ".inputError.none"
}
],
}
},
validate: {
on: {
SIGN_IN: "signIn",
ERROR: {
target: "edit.submitError.error",
actions: "setValidationError"
}
}
},
signIn: {
invoke: {
src: "signIn",
onDone: {
actions: "setUser",
target: "resolved"
},
onError: {
target: "edit.submitError.error",
actions: assign({
error: (_, event) => {
const message = event.data?.message || event.data;
return { cross_field: [message] };
}
})
}
}
},
resolved: {
type: "final"
}
},
}, {
guards: {
hasInputError: (context, event) => {
return Math.random() < 0.5;
}
}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment