Skip to content

Instantly share code, notes, and snippets.

@vojtaholik
Created October 20, 2020 08:05
Show Gist options
  • Save vojtaholik/8712f0c4e5643a706d5d9145225a8340 to your computer and use it in GitHub Desktop.
Save vojtaholik/8712f0c4e5643a706d5d9145225a8340 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const quizMachine = Machine({
id: 'question',
initial: 'idle',
context: {
questions: [{title: "question 1", title: "question 2", id: 'question-1'}],
currentQuestionId: 'question-1',
answers: []
},
states: {
idle: {
on: {
SUBMIT: {
target: "answering",
actions: assign({answers: (context, event) => {
const {answers} = context
return [...answers, event.answer]
}})
}
}
},
answering: {
// invoke: {
// id: 'postingAnswer',
// src: (context, _event) => {
// const {ansers, currentQuestionId} = context
// // find(answers, {questionId: currentQuestionId})
// // return axios.post()
// }
// onDone: {target: 'answered'},
// onError: {target: 'failure'}
// }
on: {'': [{target: 'answered'}]},
},
answered: {
on: {
NEXT_QUESTION: {target: 'idle', actions: assign({currentQuestionId: (context, event) => {
return event.currentQuestionId
}})}
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment