Skip to content

Instantly share code, notes, and snippets.

@alizbazar
Last active September 3, 2020 21:47
Show Gist options
  • Save alizbazar/8c1b6217a064c1866d1b2b068afb0885 to your computer and use it in GitHub Desktop.
Save alizbazar/8c1b6217a064c1866d1b2b068afb0885 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const assessmentMachine = Machine(
{
id: "assessment",
initial: "phq9_1",
context: {
phq9: 0,
gad7: 0,
mhq: false,
answers: []
},
on: {
PHQ9: {
actions: ["addPHQ9", raise("NEXT")]
},
GAD7: {
actions: ["addGAD7", raise("NEXT")]
},
MHQ: {
actions: ["addMHQ", raise("NEXT")]
}
},
states: {
phq9_1: {
on: {
NEXT: "phq9_2"
}
},
phq9_2: {
on: {
NEXT: [
{
target: "phq9_3",
cond: "isPHQ2High"
},
{ target: "gad7_1" }
]
}
},
phq9_3: {
on: {
NEXT: "therapy"
}
},
gad7_1: {
on: {
NEXT: "gad7_2"
}
},
gad7_2: {
on: {
NEXT: [
{
cond: "isGAD2High",
target: "gad7_3"
},
{ target: "mhq_1" }
]
}
},
gad7_3: {
on: {
NEXT: "therapy"
}
},
mhq_1: {
on: {
NEXT: [
{
cond: "isMHQ",
target: "exclusion"
},
{
target: "coaching"
}
]
}
},
exclusion: { type: "final" },
coaching: { type: "final" },
therapy: { type: "final" }
}
},
{
actions: {
addPHQ9: assign({
phq9: (ctx, e) => ctx.phq9 + e.answer,
answers: (ctx, e) => [
...ctx.answers,
{ question: e.question, answer: e.answer }
]
}),
addGAD7: assign({
gad7: (ctx, e) => ctx.gad7 + e.answer,
answers: (ctx, e) => [
...ctx.answers,
{ question: e.question, answer: e.answer }
]
}),
addMHQ: assign({
mhq: (ctx, e) => Boolean(ctx.mhq || e.answer === "yes"),
answers: (ctx, e) => [
...ctx.answers,
{ question: e.question, answer: e.answer }
]
})
},
guards: {
isPHQ2High: (ctx) => ctx.phq9 > 2,
isGAD2High: (ctx) => ctx.gad7 > 2,
isMHQ: (ctx) => ctx.mhq
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment