Skip to content

Instantly share code, notes, and snippets.

@aumeunier
Created June 22, 2021 11:56
Show Gist options
  • Save aumeunier/06e494ff8983177816ce836e85f77cd7 to your computer and use it in GitHub Desktop.
Save aumeunier/06e494ff8983177816ce836e85f77cd7 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const onFormUpdate = { target: "Editing", actions: "onUpdate" };
/**
* This is the definition of the StateMachine.
* This machine defines a generic behavior for a form feature:
* - events (update, submitting, result of the submit)
* - transitions
* - guards/actions/services to call WITHOUT defining them
*
* Note that the "context" is set as 'any' because the machine itself does not
* deal with the context within the form.
*/
const FormMachineConfig = Machine({
id: 'formMachine',
initial: "Editing",
// State definitions
states: {
"Editing": {
always: [{
target: "Editing", cond: 'isFormComplete',
}],
on: {
"UpdateForm": onFormUpdate,
"UpdateFormComplete": "EditingComplete",
}
},
"EditingComplete": {
// always: [
// { target: "InvalidForm, cond: 'isFormIncomplete', actions: 'updateError' },
// ],
on: {
"UpdateForm": onFormUpdate,
"Validate": "Submitting",
}
},
"Submitting": {
invoke: {
src: "submitAsync",
onError: [{
target: "Blocked",
cond: "shouldBlock",
actions: "onBlock"
}],
onDone: [{
target: "Validated",
cond: "isFormValidated",
actions: "onValidated"
}, {
target: "ValidationFailed",
actions: "onFormError"
}]
}
},
"InvalidForm": {
entry: ['updateIncomplete'],
exit: ['updateIncomplete'],
on: {
"UpdateForm": onFormUpdate,
}
},
"ValidationFailed": {
on: {
"UpdateForm": onFormUpdate,
"Validate": "Submitting",
}
},
"Validated": {
// type: 'final'
},
"Blocked": {
// type: 'parallel',
},
},
});
const AuthMachine = Machine({
id: 'auth',
initial: "SignIn",
states: {
"Register": {
on: {
"SignIn": "SignIn",
"Forgot": "Forgot",
}
},
"SignIn": {
on: {
"Register": "Register",
"Forgot": "Forgot",
},
...FormMachineConfig
},
"Forgot": {
on: {
"Register": "Register",
"Forgot": "Forgot",
}
},
"Authenticated": { type: "final" },
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment