Last active
May 28, 2021 23:01
-
-
Save nucleogenesis/a231ee2eababe343c226dabbb3c3026e to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const store = { | |
getters: { | |
isUserLoggedIn: true, | |
facilities: ['a','b'], | |
facilityId: 1, | |
selectedFacility: 1, | |
} | |
} | |
const hasMultipleFacilities = context => context.facilities.length > 1; | |
// DON"T DELETE ABOVE | |
const initializing = { | |
// Note that these are executed in order and the order matters logically here | |
entry: ['goToProfileIfSignedIn', 'goToSignInWhenOneFacility', 'goToWhenMultipleFacilities'], | |
on: { | |
USER_INITIALLY_LOGGED_IN: 'profile', | |
SIGN_INTO_KNOWN_FACILITY: 'signin', | |
START_FROM_THE_BEGINNING: 'signinOrSignup', | |
}, | |
}; | |
const profile = { | |
entry: ['initializeIfNotSignedIn'], | |
on: { | |
NOT_SIGNED_IN_ON_ENTRY: 'initializing', | |
EDIT_PROFILE: 'editProfile', | |
}, | |
}; | |
const editProfile = { | |
on: { | |
PROFILE: 'profile', | |
}, | |
}; | |
const signin = { | |
on: { | |
USERNAME_SUBMITTED: 'usernameSubmitted', | |
GO_BACK: [{ | |
target: 'selectFacilityForSignIn', cond: hasMultipleFacilities | |
}], | |
} | |
}; | |
const usernameSubmitted = { | |
invoke: { | |
id: 'validateUsername', | |
src: (context) => getUsernameExists( | |
{ | |
username: context.signInUsername, | |
facilityId: context.faclityId, | |
} | |
), | |
onDone: { | |
target: 'signinPassword', | |
}, | |
onError: { | |
target: 'signin', | |
// Actually assign error messages here | |
actions: assign({ signInErrors: (context, event) => event.data }) | |
}, | |
} | |
} | |
const signinPassword = { | |
entry: ['initializeIfNoUsername'], | |
on: { | |
NO_USERNAME_IN_STATE: 'initializing', | |
SUBMIT_AUTHENTICATION: 'validateSigninWithPassword', | |
GO_BACK: 'signin', | |
}, | |
}; | |
const validateSigninWithPassword = { | |
invoke: { | |
id: "validateSignInAndRedirectIfValid", | |
src: () => new Promise.resolve(1), | |
onDone: { | |
target: '', | |
actions: ['redirectToLearn'], | |
}, | |
}, | |
} | |
const signup = { | |
on: { | |
SIGN_IN_INSTEAD: 'signin', | |
} | |
} | |
const selectFacilityForSignIn = { | |
on: { | |
FACILITY_SELECTED: 'signin', | |
GO_BACK: 'signinOrSignup', | |
}, | |
}; | |
const selectFacilityForSignUp = { | |
on: { | |
FACILITY_SELECTED: 'signup', | |
GO_BACK: 'signinOrSignup', | |
}, | |
}; | |
const signinOrSignup = { | |
on: { | |
SIGNIN: 'selectFacilityForSignIn', | |
SIGNUP: 'selectFacilityForSignUp', | |
}, | |
}; | |
function initializeIfNotSignedIn(context) { | |
if (!context.isUserLoggedIn) { | |
return send({ type: 'NOT_SIGNED_IN_ON_ENTRY' }); | |
} | |
} | |
function initializeIfNoUsername(context) { | |
if (context.signInUsername) { | |
return send({ type: 'NO_USERNAME_IN_STATE' }); | |
} | |
} | |
const redirectToLearn = () => window.location.href = "google.com" | |
/** | |
* Uses the in-scope vuex store to determine if the user | |
* is currently logged in and sends the appropriate event | |
* which will be heard by the implementing state machine. | |
* | |
* Intended for use in `initializing` state's `entry` actions | |
*/ | |
function goToProfileIfSignedIn() { | |
if (store.getters.isUserLoggedIn) { | |
return send({ type: 'USER_INITIALLY_LOGGED_IN' }); | |
} | |
} | |
/** | |
* Presumes that the user is not signed in as previous action | |
* should be taken elsewhere to ensure that is true. | |
*/ | |
function goToSignInWhenOneFacility() { | |
if (store.getters.facilities.length === 1) { | |
return send({ type: 'SIGN_INTO_KNOWN_FACILITY' }); | |
} | |
} | |
/** | |
* Presumes multiple facilities | |
* Presumes user is not signed in | |
*/ | |
function goToWhenMultipleFacilities() { | |
if (store.getters.facilityId) { | |
return send({ type: 'SIGN_INTO_KNOWN_FACILITY' }); | |
} else { | |
return send({ type: 'START_FROM_THE_BEGINNING' }); | |
} | |
} | |
/** Routes */ | |
/** State Machine(s) */ | |
const defaultContext = { | |
isUserLoggedIn: store.getters.isUserLoggedIn, | |
facilityId: store.getters.facilityId, | |
facilities: store.getters.facilities, | |
selectedFacility: store.getters.selectedFacility, | |
} | |
// DON"T DELETE BELOW | |
Machine({ | |
id: 'AuthRouterStateMachine', | |
initial: 'initializing', | |
context: defaultContext, | |
states: { | |
initializing, | |
profile, | |
editProfile, | |
selectFacilityForSignIn, | |
selectFacilityForSignUp, | |
signin, | |
signup, | |
usernameSubmitted, | |
validateSigninWithPassword, | |
signinPassword, | |
signinOrSignup, | |
}, | |
actions: { | |
initializeIfNotSignedIn, | |
initializeIfNoUsername, | |
goToProfileIfSignedIn, | |
goToSignInWhenOneFacility, | |
goToWhenMultipleFacilities, | |
redirectToLearn, | |
}, | |
guards: { | |
hasMultipleFacilities: (context) => true, | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment