Skip to content

Instantly share code, notes, and snippets.

@arcdev1
Created November 25, 2019 06:16
Show Gist options
  • Save arcdev1/8c4e567d1060aa26ed5b631b5d1f2323 to your computer and use it in GitHub Desktop.
Save arcdev1/8c4e567d1060aa26ed5b631b5d1f2323 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 isFormValid = ()=>true
const loginMachine = Machine({
id: 'login',
initial: 'awaitingCredentials',
context: {
emailAddress: null,
password: null,
rememberMe: false,
emailError: null,
passwordError: null,
formError: null,
shouldValidateEmail: false,
shouldValidatePassword: false,
},
states: {
awaitingCredentials: {
on: {
EMAIL_CHANGED: {
actions: 'validateEmail'
},
EMAIL_BLURRED: {
actions: 'allowEmailValidation'
},
PASSWORD_BLURRED: {
actions: 'allowPasswordValidation'
},
PASSWORD_CHANGED: {
actions: 'validatePassword'
},
REMEMBER_ME_TOGGLED: {
actions: 'toggleRememberMe'
},
SUBMIT: [{
actions: ['allowEmailValidation','allowPasswordValidation','validateEmail', 'validatePassword'],
cond: isFormValid,
target: 'validatingCredentials'
}]
}
},
validatingCredentials: {
invoke: {
id: 'authenticate',
src: (context, event) => async () => true,
onDone: 'credentialsValid',
onError: 'credentialsInvalid'
}
},
credentialsValid: {
type: 'final'
},
credentialsInvalid: {
on: {
RESET: {
actions: 'resetForm',
target:'awaitingCredentials'}
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment