Skip to content

Instantly share code, notes, and snippets.

@DaveWelling
Last active October 1, 2020 21:32
Show Gist options
  • Save DaveWelling/74a1d829ee71698d7f594f028cbe0734 to your computer and use it in GitHub Desktop.
Save DaveWelling/74a1d829ee71698d7f594f028cbe0734 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
/* eslint camelcase: 0 */
// Because of the fsm naming conventions, ignore camelcase in this file.
// To get a visualization of this finite state machine,
// go to https://xstate.js.org/viz/?gist=74a1d829ee71698d7f594f028cbe0734
// You may have to copy the code below and replace the
// 'DEFINITION' section in the link above.
// (HINT: It is easier to copy if you collapse all the top level stuff)
// ********************************************************
// TO COPY TO VISUALIZATION TOOL LINKED ABOVE, START COPY HERE
//
// Find the `loginMachine` const assignment at the bottom and uncomment
// that for the visualization tool to use.
// Do not include the module.export at the bottom in your copy.
// ********************************************************
// A default context for visualization tool - *overridden by app*.
// eslint-disable-next-line no-unused-vars
const context = {
errors: { field: {}, form: [] },
temporaryToken: 'imatemporarytoken'
};
const fsmGuards = {
c_needTenantSelection: context => context.tenant == null,
c_needLicenseAcceptance: context =>
!!(context.tenant && (context.tenant.needMlaAcceptance || context.tenant.needEulaAcceptance)),
c_hasTempToken: (context, event) => {
// Check on the event first
if (event.type === 'TEMP_TOKEN_FOUND') {
return true;
}
// then check the context
return context.temporaryToken != null;
}
};
const fsmConfig = {
id: 'loginFsm',
initial: 'needs_authentication',
context,
states: {
needs_authentication: {
// Check for a temporary token
// Go to needs_newPassword if one is found
// otherwise, wait for credentials to be submitted
invoke: {
id: 'checkTempToken',
src: 'svc_checkTempToken',
onDone: {
cond: 'c_hasTempToken',
target: 'needs_newPassword'
},
onError: {
internal: true, // Do not leave this state or cycle invoke
actions: 'do_setErrors'
}
},
on: {
CREDENTIALS_SUBMITTED: {
target: 'is_authenticating',
actions: 'do_storeCredentials'
}
}
},
needs_newPassword: {
on: {
CREDENTIALS_SUBMITTED: {
target: 'is_authenticating',
actions: 'do_storeCredentials'
}
}
},
is_authenticating: {
invoke: {
id: 'authenticate',
src: 'svc_authenticate',
onDone: {
target: 'is_authenticated',
actions: 'do_setAuthenticationResults'
},
onError: {
target: 'needs_authentication',
actions: 'do_setErrors'
}
}
},
is_authenticated: {
initial: 'unknown',
on: {
TENANT_SELECTED: {
target: 'is_authenticating',
actions: 'do_setTenant'
},
TENANT_SELECTION_CANCELLED: {
target: 'needs_authentication',
actions: 'do_resetLogin'
},
LICENSE_ACCEPTED: {
target: 'is_authenticating',
actions: 'do_setAcceptedLicense'
},
LICENSE_REJECTED: {
target: 'needs_authentication',
actions: 'do_resetLogin'
}
},
states: {
unknown: {
always: [
{
target: 'needs_tenantSelection',
cond: 'c_needTenantSelection'
},
{
target: 'needs_licenseAcceptance',
cond: 'c_needLicenseAcceptance'
},
{
target: 'is_readyForRedirect',
actions: 'do_redirectToIndex'
}
]
},
// unknown: {
// on: {
// '': [
// {
// target: 'needs_tenantSelection',
// cond: 'c_needTenantSelection'
// },
// {
// target: 'needs_licenseAcceptance',
// cond: 'c_needLicenseAcceptance'
// },
// {
// target: 'is_readyForRedirect',
// actions: 'do_redirectToIndex'
// }
// ]
// }
// },
needs_tenantSelection: {},
needs_licenseAcceptance: {},
is_readyForRedirect: {
final: true
}
}
}
}
};
// TO COPY TO VISUALIZATION (see instructions above) UNCOMMENT THIS
const loginMachine = Machine({ ...fsmConfig, context }, { guards: fsmGuards });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment