Skip to content

Instantly share code, notes, and snippets.

@ivorpad
Created September 1, 2020 12:19
Show Gist options
  • Save ivorpad/7061271348fafdd9b877e58da83bf135 to your computer and use it in GitHub Desktop.
Save ivorpad/7061271348fafdd9b877e58da83bf135 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({
initial: "editing",
context: {
values: {},
errors: {},
newValues: {},
},
states: {
editing: {
initial: "pristine",
on: {
CHANGE: {
actions: ["onChange", "onNewValueChange"],
},
SUBMIT: "submitting",
DISCARD: {
actions: ["clearForm"]
}
},
states: {
pristine: {},
error: {},
},
},
submitting: {
invoke: {
src: "onSubmit",
onDone: "success",
onError: {
target: "editing.error",
actions: ["onError"],
},
},
},
success: {
id: "success",
on: {
AGAIN: "editing",
},
},
},
},
{
actions: {
onChange: assign({
values: (ctx, e) => ({
...ctx.values,
[e.key]: { value: e.value },
}),
}),
onNewValueChange: assign({
newValues: (ctx, e) => ({
...ctx.newValues,
[e.key ? e.key : e.value.id]: { value: e.value.id ? e.value.value : e.value },
})
}),
clearForm: assign({
values: {},
newValues: {},
errors: {},
}),
onError: assign({
errors: (_ctx, e) => e.data,
}),
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment