Skip to content

Instantly share code, notes, and snippets.

@fdjones
Last active April 12, 2020 11:27
Show Gist options
  • Save fdjones/d00e26bd32c0231a9f1fcab898c0d148 to your computer and use it in GitHub Desktop.
Save fdjones/d00e26bd32c0231a9f1fcab898c0d148 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
function fetchQuestions() {
return Promise.resolve([
{ name: 'bob' },
{ name: 'bill' },
{ name: 'mary' }
]);
}
const test = Machine({
id: 'test',
initial: 'idle',
context: {
questions: [],
error: ''
},
states: {
idle: {
on: {
START_TEST: 'fetchingQuestions'
}
},
fetchingQuestions: {
on: {
EXIT_TEST: 'exit'
},
invoke: {
id: 'fetchQuestions',
src: () => fetchQuestions,
onDone: {
target: 'answeringQuestion',
actions: assign({ questions: (ctx, event) => event.data})
},
onError: {
target: 'error',
actions: assign({ questions: (ctx, event) => event.data})
}
}
},
answeringQuestion: {
on: {
SUBMIT_ANSWER: 'viewingAnwer',
EXIT_TEST: 'exit'
},
},
viewingAnwer: {
on: {
NEXT_QUESTION: 'answeringQuestion',
FINISH_TEST: 'reviewingTest',
EXIT_TEST: 'exit'
}
},
reviewingTest: {
on: {
EXIT_TEST: 'exit'
}
},
error: {},
exit: {
type: 'final'
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment