Skip to content

Instantly share code, notes, and snippets.

@mrassili
Last active June 20, 2020 16:09
Show Gist options
  • Save mrassili/4e14905742e4f651b54e8b275637825b to your computer and use it in GitHub Desktop.
Save mrassili/4e14905742e4f651b54e8b275637825b 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 settings = Machine({
id: "settings",
context: {
frequency: "weekly",
topics: ["health", "sports"],
},
initial: "editing",
states: {
editing: {
on: {
CHANGE_FREQUENCY: {
target: "editing",
actions: "changeFrequency"
},
ADD_TOPIC: {
target: "editing",
actions: "addTopic"
},
REMOVE_TOPIC: {
target: "editing",
actions: "removeTopic"
},
SUBMIT: {
target: "submitting"
},
}
},
submitting: {
meta: {
message: "Saving…"
}
},
success: {
meta: {
message: "Settings saved successfully!"
}
},
failure: {
meta: {
message: "Settings could not be saved. Please try again!"
}
}
},
},
{
actions: {
changeFrequency: assign({
frequency: (_ctx,e) => e.value
}),
addTopic: assign({
topics: (ctx, e) => ctx.topics.concat([e.value])
}),
removeTopic: assign({
topics: (ctx, e) => ctx.topics.filter(item => item !== e.value)
})
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment