Skip to content

Instantly share code, notes, and snippets.

@mwangaben
Last active August 7, 2021 11:54
Show Gist options
  • Save mwangaben/fe978e80a56fab40ddfe41d75fc95e79 to your computer and use it in GitHub Desktop.
Save mwangaben/fe978e80a56fab40ddfe41d75fc95e79 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const createUser = function() {
alert('Makng User');
}
const fetchMachine = Machine({
id: 'register',
initial: 'idle',
context: {
form: {
name: '',
email: '',
password: ''
},
users: [],
error: '',
retries: 0
},
states: {
idle: {
on: {
SUBMIT: 'submitting'
},
},
submitting: {
// actions: ['registerUser'],
invoke: {
id: 'saveUser',
src: (context, event) => createUser(),
onDone:{
target: 'success',
actions: assign({ users: (context, event) => context.users.push(event.data) })
},
onError: {
target: 'failure',
actions: assign({ error: (context, event) => event.data })
}
}
// on: {
// RESOLVE: 'success',
// REJECT: 'failure'
// }
},
success: {
on:{
SUCCESS:'inform'
}
},
inform:{
on: {
OK: 'idle'
}
},
failure: {
on: {
RETRY: {
target: 'idle',
actions: assign({
retries: (context, event) => context.retries + 1
})
}
}
},
actions: {
// registerUser
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment