Skip to content

Instantly share code, notes, and snippets.

@mrassili
Last active June 20, 2020 13:12
Show Gist options
  • Save mrassili/4a3e931c2f4f045bc26096cac9dca022 to your computer and use it in GitHub Desktop.
Save mrassili/4a3e931c2f4f045bc26096cac9dca022 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 settingsMachine = Machine(
{
id: 'settings',
initial: "editing",
context: {
interests: [],
frequency: "weekly",
message: "",
errors: {},
},
states: {
editing: {
initial: "pristine",
pristine: {
on: {
SUBMIT: {
actions: assign({
message: (_ctx, e) => e.value
})
}
}
},
on: {
CHANGE_FREQUENCY: {
target: "editing.dirty",
actions: ["changeFrequency"],
},
REMOVE_INTEREST: {
target: "editing.dirty",
actions: ["removeInterest"],
},
ADD_INTEREST: {
target: "editing.dirty",
actions: ["addInterest"],
},
SUBMIT: "submitting",
},
states: {
pristine: {},
dirty: {},
error: {},
},
},
submitting: {
invoke: {
src: "onSubmit",
onDone: "success",
onError: {
target: "editing.error",
actions: ["onError"],
},
},
},
success: {
on: {
AGAIN: "editing",
},
},
},
},
{
actions: {
changeFrequency: assign({
frequency: (_ctx, e) => e.value,
}),
removeInterest: assign({
interests: (ctx, e) => ctx.interests.filter((item) => item !== e.value),
}),
addInterest: assign({
interests: (ctx, e) => ctx.interests.concat([e.value]),
}),
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