Skip to content

Instantly share code, notes, and snippets.

@ThaddeusJiang
Last active May 29, 2020 07:41
Show Gist options
  • Save ThaddeusJiang/e143d0ff8755cf5069732cff247bb1e2 to your computer and use it in GitHub Desktop.
Save ThaddeusJiang/e143d0ff8755cf5069732cff247bb1e2 to your computer and use it in GitHub Desktop.
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const formMachine = Machine({
initial: 'idle',
context: {
data: {
name: '',
age: '',
address: '',
},
error: '',
},
states: {
idle: {
on: {
fetch: 'fetching',
},
},
fetching: {
invoke: {
id: 'fetchData',
src: 'fetchData',
onDone: {
target: 'normal',
actions: assign({
// TODO: Can I directly set response to Vue date?
data: (context, event) => event.data,
}),
},
onError: {
target: 'fetchFailed',
actions: assign({
error: (context, event) => event.data,
}),
},
},
},
fetchFailed: {},
normal: {
on: {
onSubmit: 'updating',
},
},
updating: {
invoke: {
id: 'updateData',
src: 'updateData',
onDone: {
target: 'normal',
},
onError: {
target: 'updateFailed',
actions: assign({
error: (context, event) => event.data,
}),
},
},
},
updateFailed: {
on: {
ok: 'normal',
},
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment