Skip to content

Instantly share code, notes, and snippets.

@m1010j
Last active May 11, 2020 21:48
Show Gist options
  • Save m1010j/950355029c76339aac337bd5dde664bb to your computer and use it in GitHub Desktop.
Save m1010j/950355029c76339aac337bd5dde664bb to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const questionMachine = Machine(
{
id: 'question',
initial: '47475c25-8e0f-4115-a55a-4dc643ae50a2',
context: {
currentStep: 1,
totalNumberOfSteps: 8,
},
states: {
'47475c25-8e0f-4115-a55a-4dc643ae50a2': {
on: {
ANSWER: { target: 'a8af0f85-fa49-4531-9223-39c7ce11af7a', actions: 'incrementCurrentStep' },
},
},
'a8af0f85-fa49-4531-9223-39c7ce11af7a': {
on: {
ANSWER: { target: '4268735b-be36-41cb-8a98-248525c3af17', actions: 'incrementCurrentStep' },
},
},
'4268735b-be36-41cb-8a98-248525c3af17': {
on: {
ANSWER: [
{
target: 'c46bbace-1ebf-4b7d-b148-77e6da411f8e',
cond: { type: 'eventValueIsCondValue', value: 'BIPOLAR' },
actions: 'incrementFollowUpQuestionStep',
},
{
target: '4f1c10f3-bcde-4019-8981-3ce282569681',
cond: { type: 'eventValueIsCondValue', value: 'CANCER' },
actions: 'incrementFollowUpQuestionStep',
},
{
target: '1ade8e02-f309-4d6e-9773-7ebc77b6127f',
cond: { type: 'eventValueIsCondValue', value: 'HEART-DISEASE' },
actions: 'incrementFollowUpQuestionStep',
},
{
target: 'b2e832d6-9790-4d33-9472-cb1ee09a83a8',
cond: { type: 'eventValueIsCondValue', value: 'KIDNEY-DISEASE' },
actions: 'incrementFollowUpQuestionStep',
},
{
target: '74a4e3ef-9fe2-4cd8-ab1b-357ffc0a0e6d',
cond: { type: 'eventValueIsCondValue', value: 'MS' },
actions: 'incrementFollowUpQuestionStep',
},
{
target: 'd68ced2c-47ca-41a0-9111-f7aed2bc2d1b',
cond: { type: 'eventValueIsCondValue', value: 'STROKE' },
actions: 'incrementFollowUpQuestionStep',
},
{ target: 'c211e782-a9c9-461a-939a-f94dd26bd709', actions: 'incrementCurrentStep' },
],
},
},
'c211e782-a9c9-461a-939a-f94dd26bd709': {
on: {
ANSWER: [
{
target: '7594a8c7-39b6-48b5-92d6-d8834f4c7525',
cond: { type: 'eventValueIsCondValue', value: 'Yes' },
actions: 'incrementFollowUpQuestionStep',
},
{
target: 'd2af9ab2-a155-4c20-9a71-24829d09a2dd',
actions: 'incrementCurrentStep',
},
],
},
},
'd2af9ab2-a155-4c20-9a71-24829d09a2dd': {
on: {
ANSWER: [
{
target: '3df8fe41-eff8-4402-9b7a-86c8b3925131',
cond: { type: 'eventValueIsCondValue', value: 'Yes' },
actions: 'incrementFollowUpQuestionStep',
},
{
target: '8f031865-fb1c-4667-ace9-bb2e9fe24a62',
actions: 'incrementCurrentStep',
},
],
},
},
'8f031865-fb1c-4667-ace9-bb2e9fe24a62': {
on: {
ANSWER: { target: 'c847b333-7ff0-412e-b6cf-3ee3bc67fc21', actions: 'incrementCurrentStep' },
},
},
'c847b333-7ff0-412e-b6cf-3ee3bc67fc21': {
on: {
ANSWER: [
{
target: 'a8efc3b9-dffc-49d9-920c-e7f3b6ae5053',
cond: { type: 'eventValueIsCondValue', value: 'Yes' },
actions: 'incrementFollowUpQuestionStep',
},
{
target: '14b91394-d722-434c-9255-f87547d834eb',
actions: 'incrementCurrentStep',
},
],
},
},
'14b91394-d722-434c-9255-f87547d834eb': {
on: {
ANSWER: [
{
target: '4eebcbba-c8da-4508-b4c8-0f7ce7d1b73e',
cond: { type: 'eventValueIsCondValue', value: 'Yes' },
actions: 'incrementFollowUpQuestionStep',
},
{
target: 'complete',
actions: 'incrementCurrentStep',
},
],
},
},
// follow-up questions
'c46bbace-1ebf-4b7d-b148-77e6da411f8e': {
on: {
ANSWER: { target: 'c211e782-a9c9-461a-939a-f94dd26bd709', actions: 'incrementCurrentStep' },
},
},
'4f1c10f3-bcde-4019-8981-3ce282569681': {
on: {
ANSWER: { target: 'c211e782-a9c9-461a-939a-f94dd26bd709', actions: 'incrementCurrentStep' },
},
},
'1ade8e02-f309-4d6e-9773-7ebc77b6127f': {
on: {
ANSWER: [
{
target: 'd0c96d0c-9adc-4884-843e-aa935d958df9',
cond: { type: 'eventValueIsCondValue', value: 'No' },
actions: 'incrementFollowUpQuestionStep',
},
{
target: 'c211e782-a9c9-461a-939a-f94dd26bd709',
actions: 'incrementCurrentStep',
},
],
},
},
'b2e832d6-9790-4d33-9472-cb1ee09a83a8': {
on: {
ANSWER: { target: 'c9146afd-e439-4d0e-9688-3ac0c038a63', actions: 'incrementFollowUpQuestionStep' },
},
},
'74a4e3ef-9fe2-4cd8-ab1b-357ffc0a0e6d': {
on: {
ANSWER: [
{
target: '5e223e4c-4647-4a51-8bc0-0c96c4a1f76d',
cond: { type: 'eventValueIsCondValue', value: 'No' },
actions: 'incrementFollowUpQuestionStep',
},
{
target: 'c211e782-a9c9-461a-939a-f94dd26bd709',
actions: 'incrementCurrentStep',
},
],
},
},
'd68ced2c-47ca-41a0-9111-f7aed2bc2d1b': {
on: {
ANSWER: [
{
target: '6ff65f00-5619-422f-8208-11f81ffec456',
cond: { type: 'eventValueIsCondValue', value: 'Yes' },
actions: 'incrementFollowUpQuestionStep',
},
{
target: 'c211e782-a9c9-461a-939a-f94dd26bd709',
actions: 'incrementCurrentStep',
},
],
},
},
'7594a8c7-39b6-48b5-92d6-d8834f4c7525': {
on: {
ANSWER: { target: 'd2af9ab2-a155-4c20-9a71-24829d09a2dd', actions: 'incrementCurrentStep' },
},
},
'3df8fe41-eff8-4402-9b7a-86c8b3925131': {
on: {
ANSWER: { target: '285d03ef-0e67-42a1-87a5-de2afbea3e73', actions: 'incrementFollowUpQuestionStep' },
},
},
'a8efc3b9-dffc-49d9-920c-e7f3b6ae5053': {
on: {
ANSWER: [
{
target: 'd8ba8d8b-c7f6-41ff-8070-ad0722e95ad1',
cond: { type: 'eventValueIsCondValue', value: 'Yes' },
actions: 'incrementFollowUpQuestionStep',
},
{
target: '14b91394-d722-434c-9255-f87547d834eb',
actions: 'incrementCurrentStep',
},
],
},
},
'3df8fe41-eff8-4402-9b7a-86c8b3925131': {
on: {
ANSWER: { target: '8f031865-fb1c-4667-ace9-bb2e9fe24a62', actions: 'incrementCurrentStep' },
},
},
'4eebcbba-c8da-4508-b4c8-0f7ce7d1b73e': {
on: {
ANSWER: { target: 'complete', actions: 'incrementCurrentStep' },
},
},
// follow-up follow-up questions
'd0c96d0c-9adc-4884-843e-aa935d958df9': {
on: {
ANSWER: [
{
target: '53deda86-0a83-46d6-bd33-1392e4f69740',
cond: { type: 'eventValueIsCondValue', value: 'No' },
actions: 'incrementFollowUpQuestionStep',
},
{
target: 'c211e782-a9c9-461a-939a-f94dd26bd709',
actions: 'incrementCurrentStep',
actions: 'incrementFollowUpQuestionStep',
},
],
},
},
'5e223e4c-4647-4a51-8bc0-0c96c4a1f76d': {
on: {
ANSWER: { target: 'c211e782-a9c9-461a-939a-f94dd26bd709', actions: 'incrementCurrentStep' },
},
},
'6ff65f00-5619-422f-8208-11f81ffec456': {
on: {
ANSWER: { target: 'c211e782-a9c9-461a-939a-f94dd26bd709', actions: 'incrementCurrentStep' },
},
},
'd8ba8d8b-c7f6-41ff-8070-ad0722e95ad1': {
on: {
ANSWER: { target: '14b91394-d722-434c-9255-f87547d834eb', actions: 'incrementCurrentStep' },
},
},
'c9146afd-e439-4d0e-9688-3ac0c038a63e': {
on: {
ANSWER: { target: 'c211e782-a9c9-461a-939a-f94dd26bd709', actions: 'incrementCurrentStep' },
},
},
'285d03ef-0e67-42a1-87a5-de2afbea3e73': {
on: {
ANSWER: [
{
target: '18381789-efdd-4748-8484-998f3c190cd0',
cond: { type: 'eventValueIsCondValue', value: 'Yes' },
actions: 'incrementFollowUpQuestionStep',
},
{
target: '8f031865-fb1c-4667-ace9-bb2e9fe24a62',
actions: 'incrementCurrentStep',
actions: 'incrementFollowUpQuestionStep',
},
],
},
},
'c9146afd-e439-4d0e-9688-3ac0c038a63': {
ANSWER: { target: 'c211e782-a9c9-461a-939a-f94dd26bd709', actions: 'incrementCurrentStep' },
},
// follow-up follow-up follow-up questions
'53deda86-0a83-46d6-bd33-1392e4f69740': {
on: {
ANSWER: { target: 'c211e782-a9c9-461a-939a-f94dd26bd709', actions: 'incrementCurrentStep' },
},
},
'18381789-efdd-4748-8484-998f3c190cd0': {
on: {
ANSWER: { target: '8f031865-fb1c-4667-ace9-bb2e9fe24a62', actions: 'incrementCurrentStep' },
},
},
// complete
complete: {
type: 'final',
},
},
},
{
guards: {
eventValueIsCondValue: (_, event, { cond }) => event.value === cond.value,
},
actions: {
incrementFollowUpQuestionStep: assign({ followUpQuestionStep: (context) => context.followUpQuestionStep + 1 }),
incrementCurrentStep: assign({ currentStep: (context) => context.currentStep + 1, followUpQuestionStep: 0 }),
},
},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment