Skip to content

Instantly share code, notes, and snippets.

@blorenz
Last active September 1, 2019 17:46
Show Gist options
  • Save blorenz/c025152c29f991300896d9e67501f55e to your computer and use it in GitHub Desktop.
Save blorenz/c025152c29f991300896d9e67501f55e to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const registrationMachine = Machine(
{
id: `registration`,
initial: `codeEntry`,
context: {
user: undefined,
},
states: {
codeEntry: {
on: { SUBMIT: `submitting` },
},
captainEntry: {
on: { SUBMIT: `submitting` },
},
teamEntry: {
on: { SUBMIT: `submitting` },
},
photoEntry: {
on: { SUBMIT: `submitting` },
},
submitting: {
entry: [`clearError`],
invoke: {
id: `submitForm`,
src: (context, event) => {
return event.data.mutate(event.data.mutateProps);
},
onDone: [
{
target: `captainEntry`,
cond: `userValid`,
},
{
target: `error`,
cond: `userNotValid`,
},
],
onError: {
target: `error`,
},
},
},
error: {
on: { SUBMIT: `submitting` },
entry: [`setError`],
},
completed: {
type: `final`,
entry: [`redirectToDashboard`],
},
},
},
{
actions: {
redirectToDashboard: (context, event) => {
window.FH.loggedin = true;
const user = event.data.person;
console.log(user);
if (user.isSuperuser) window.FH.ut = `superuser`;
else if (user.isManager) window.FH.ut = `manager`;
else if (user.isStaff) window.FH.ut = `staff`;
else window.FH.ut = `user`;
history.push(Routes.CURRENT_HUNTS);
},
setError: (context, event) => {
return setLoginError(event.data.error);
},
clearError: (context, event) => setLoginError(null),
},
guards: {
userValid: (context, event) => {
return event.data && event.data.person;
},
userNotValid: (context, event) => {
return !event.data || !event.data.person;
},
},
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment