Skip to content

Instantly share code, notes, and snippets.

@njdancer
Last active October 8, 2019 01:58
Show Gist options
  • Save njdancer/f8ae8c17112d978670d1ee573a7df2f1 to your computer and use it in GitHub Desktop.
Save njdancer/f8ae8c17112d978670d1ee573a7df2f1 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const loadApplication = () => {
return new Promise((resolve,reject) => {
setTimeout(() => {resolve({authData: null})}, 1000)
})
}
const isCredentialsSet = (context, event) => event.username != null && event.password != null
const onboardingMachine = Machine({
initial: "check",
context: {
desiredTermsAndConditions: 1,
desiredInstructions: 1
},
states: {
check: {
on: {
"": [
{target: "termsAndConditions", cond: "isTermsAndConditionsNeeded"},
{target: "instructions", cond: "isInstructionsNeeded"},
{target: "locationServices"}
]
}
},
instructions: {
entry: "navigateToInstructions",
on: {
"AGREE": {
actions: ["assignInstructions"],
target: "check"
},
},
},
termsAndConditions: {
entry: "navigateToTermsAndConditions",
on: {
"AGREE": {
actions: ["assignTermsAndConditions"],
target: "check",
},
},
},
locationServices: {
invoke: {
id: "requestLocationServices",
onDone: "pushNotifications",
onError: {
actions: ["handleLocationServicesPermissionDenied", sendParent("AUTH.RESET")]
},
src: "requestLocationServices"
}
},
pushNotifications: {
invoke: {
id: "requestPushNotifications",
onDone: "complete",
onError: {
actions: ["handlePushNotificationsPermissionDenied", sendParent("AUTH.RESET")]
},
src: "requestPushNotifications"
}
},
complete: {
type: "final",
data: {
termsAndConditions: (context, event) => context.termsAndConditions,
instructions: (context, event) => context.instructions
}
}
},
},{
guards: {
isTermsAndConditionsNeeded: (context, event) => context.termsAndConditions == null || context.termsAndConditions < context.desiredTermsAndConditions,
isInstructionsNeeded: (context, event) => context.instructions == null || context.instructions < context.desiredInstructions,
},
actions: {
assignTermsAndConditions: assign({
termsAndConditions: (context, event) => context.desiredTermsAndConditions
}),
assignInstructions: assign({
instructions: (context, event) => context.desiredInstructions
}),
}
})
const sessionMachine = Machine(
{
context: {
defaults: {
organisationId: "sos2us",
},
},
id: "session",
initial: "start",
on: {
"AUTH.RESET": {
target: "credentials",
actions: ["resetSession", "persistSession"]
},
},
states: {
auth: {
invoke: {
id: "authUser",
onDone: {
actions: ["assignAuthData", "persistSession"],
target: "onboarding",
},
onError: {
actions: ["handleAuthError", "resetSession"],
target: "credentials",
},
src: "authUser",
},
},
credentials: {
entry: ["navigateToCredentials"],
on: {
"AUTH.ORG_USER": {
actions: "assignOrganisationId",
cond: "isOrganisationIdProvided",
target: "auth",
},
"AUTH.SINGLE_USER": {
actions: "assignDefaultOrganisationId",
target: "auth",
},
},
},
loaded: {
on: {
"": [
{ cond: "isAuthDataSet", target: "onboarding" },
{ target: "credentials" },
],
},
},
loading: {
invoke: {
id: "loadApplication",
onDone: {
actions: "assignPersistedSession",
target: "loaded",
},
src: "loadApplication",
},
},
main: {
entry: ["navigateToMain"],
// exit: ["resetSession", "persistSession"],
initial: "running",
on: {
"AUTH.REFRESH": ".refreshingAuthData"
},
onDone: {
actions: send("AUTH.RESET")
},
states: {
refreshingAuthData: {
invoke: {
id: "refreshAuthData",
onDone: {
actions: ["assignAuthData", "persistSession"],
target: "running",
},
onError: {
target: "sessionExpired",
},
src: "refreshAuthData",
},
},
running: {
},
sessionExpired: {
type: "final",
},
},
},
onboarding: {
invoke: {
id: "onboarding",
src: "onboarding",
onDone: {
target: "main",
actions: "assignOnboarding"
}
}
},
start: {
invoke: {
id: "presentSplashScreen",
onDone: {
target: "loading",
},
src: "presentSplashScreen",
},
},
},
},
{
actions: {
assignAuthData: assign({ authData: (context, event) => event.data }),
assignDefaultOrganisationId: assign({
organisationId: (context, event) => context.defaults.organisationId,
}),
assignOnboarding: assign({
onboarding: (context, event) => event.data,
}),
assignOrganisationId: assign({
organisationId: (context, event) => event.organisationId,
}),
assignPersistedSession: assign((context, event) => ({
...context,
...event.data,
})),
assignTermsAndConditions: assign({
termsAndConditions: (context, event) => event.version,
}),
resetSession: assign({
authData: () => null,
organisationId: () => null,
onboarding: () => null,
}),
},
guards: {
isAuthDataSet: (context, event) => context.authData != null,
isOnboardingNeeded: (context, event) =>
context.onboarding == null || context.onboarding < 1,
isOrganisationIdProvided: (context, event) =>
event.organisationId != null,
isTermsAndConditionsNeeded: (context, event) =>
context.termsAndConditions == null || context.termsAndConditions < 1,
isVersionProvided: (context, event) => event.version != null,
},
services: {
onboarding: onboardingMachine
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment