Skip to content

Instantly share code, notes, and snippets.

@derek-duncan
Created August 27, 2019 19:17
Show Gist options
  • Save derek-duncan/4aff383017be9b08877a86b8289006ca to your computer and use it in GitHub Desktop.
Save derek-duncan/4aff383017be9b08877a86b8289006ca to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const instinctMachine = Machine(
{
id: 'instinct',
context: {
offline: false,
user: null,
practice: null,
visit: null,
},
type: 'parallel',
states: {
view: {
initial: 'unauthenticated',
states: {
unauthenticated: {
onDone: 'authenticated',
initial: 'unknown',
states: {
unknown: {
on: {
'': [
{cond: 'practiceLoggedOut', target: 'practice'},
{cond: 'userLoggedOut', target: 'user'},
{target: 'completed'},
],
},
},
practice: {
entry: 'showPracticeForm',
invoke: {
id: 'practiceLogin',
src: 'practiceLogin',
onDone: {target: 'user', actions: 'assignPracticeInfo'},
},
},
user: {
entry: 'showUserForm',
invoke: {
id: 'userLogin',
src: 'userLogin',
onDone: {target: 'completed', actions: 'assignUserInfo'},
},
},
completed: {
type: 'done',
},
},
},
authenticated: {
type: 'parallel',
states: {
screen: {
on: {
GOTO_VISIT: {target: '.visitScreen', actions: 'assignVisit'},
GOTO_BACK: '.previousScreen',
},
initial: 'unknownScreen',
states: {
unknownScreen: {
on: {
'': [
{cond: 'hasRecentVisit', target: 'visitScreen'},
{target: 'statusBoardScreen'},
],
},
},
previousScreen: {
type: 'history',
history: 'deep',
},
statusBoardScreen: {},
visitScreen: {
type: 'parallel',
states: {
screen: {
on: {
GOTO_BACK: '.previousScreen',
GOTO_SHEET: '.sheetScreen',
GOTO_VITALS: '.vitalsScreen',
GOTO_INVOICE: '.invoiceScreen',
GOTO_TREATMENTS: '.treatmentsScreen',
},
initial: 'sheetScreen',
states: {
previousScreen: {
type: 'history',
},
sheetScreen: {
initial: 'loading',
states: {
loading: {
invoke: {
src: 'loadSheet',
onDone: {
target: 'loaded',
actions: 'spawnSheetOrders',
},
},
},
loaded: {
on: {
EXPORT_SHEET_PDF: {
actions: 'sendExportSheetPdf',
},
COPY_SHEET: {actions: 'copySheet'},
NAVIGATE_SHEET: {
target: 'loading',
actions: 'setSheetTime',
},
},
},
},
},
vitalsScreen: {
type: 'parallel',
states: {
current: {
initial: 'loading',
states: {
loading: {
invoke: {
src: 'loadVitals',
onDone: {
target: 'loaded',
actions: 'spawnVitalRows',
},
},
},
loaded: {
on: {
COPY_VITALS: {actions: 'copyVitals'},
},
initial: 'fullList',
states: {
fullList: {
on: {
TOGGLE_ACTIVE: 'activeList',
},
},
activeList: {
on: {
TOGGLE_ACTIVE: 'fullList',
},
},
},
},
},
},
historical: {},
},
},
invoiceScreen: {},
treatmentsScreen: {},
},
},
infoBar: {},
updateVisit: {
initial: 'idle',
states: {
idle: {
on: {
SET_ASSIGN: 'saving',
SET_COMPLAINT: 'saving',
SET_CRITICAL: 'saving',
SET_NAME: 'saving',
SET_NOTES: 'saving',
SET_STATUS: 'saving',
SET_WEIGHT: 'saving',
},
},
saving: {
invoke: {
id: 'updateVisitSaving',
src: 'saveVisit',
onDone: 'idle',
},
},
},
},
},
},
patientAdminScreen: {},
clientAdminScreen: {},
productAdminScreen: {},
groupAdminScreen: {},
categoryAdminScreen: {},
workflowAdminScreen: {},
staffAdminScreen: {},
alertAdminScreen: {},
serviceAdminScreen: {},
wardAdminScreen: {},
codeStatusAdminScreen: {},
},
},
patientSearch: {
invoke: {
src: 'patientSearchForm',
},
},
checkInVisit: {},
pdfDownload: {},
mainNav: {},
logout: {
onDone: '#instinct.view.unauthenticated',
initial: 'idle',
states: {
idle: {
on: {
HARD_USER_LOGOUT: {
target: 'completed',
actions: 'clearUserAuth',
},
USER_LOGOUT: 'userLogoutPending',
PRACTICE_LOGOUT: 'practiceLogoutPending',
},
},
userLogoutPending: {
exit: 'clearUserAuth',
invoke: {
src: 'saveUserContext',
onDone: 'completed',
},
},
practiceLogoutPending: {
exit: ['clearUserAuth', 'clearPracticeAuth'],
invoke: {
src: 'saveUserContext',
onDone: 'completed',
},
},
completed: {
type: 'done',
},
},
},
refreshUserToken: {
onDone: {
target: '#instinct.view.unauthenticated',
actions: 'clearUserAuth',
},
initial: 'delay',
states: {
delay: {
after: {
5000: 'refreshPending',
},
},
refreshPending: {
invoke: {
src: 'fetchRefreshedTokens',
onDone: 'delay',
onError: 'failed',
},
},
failed: {
type: 'done',
},
},
},
},
},
},
},
offlineStatus: {
initial: 'unknown',
states: {
unknown: {
on: {
'': [{cond: 'isOffline', target: 'offline'}, {target: 'idle'}],
},
},
idle: {
on: {
OFFLINE: 'offline',
},
},
offline: {
after: {
5000: 'unknown',
},
},
},
},
appUpdater: {},
notifications: {},
},
},
{
guards: {
hasRecentVisit: () => false,
practiceLoggedOut: () => false,
userLoggedOut: () => false,
isOffline: () => false,
},
actions: {
assignPracticeInfo: () => {},
assignUserInfo: () => {},
assignVisit: () => {},
clearPracticeAuth: () => {},
clearUserAuth: () => {},
copySheet: () => {},
saveUserContext: () => {},
sendExportSheetPdf: () => {},
spawnSheetOrders: () => {}, // spawn sheet order machine
setSheetTime: () => {},
spawnVitalRows: () => {},
},
services: {
saveUserContext: () => Promise.resolve(),
fetchRefreshedTokens: () => Promise.resolve(),
patientSearchForm: () => Machine({}),
practiceLogin: () => Machine({}),
userLogin: () => Machine({}),
saveVisit: () => Promise.resolve(),
loadSheet: () => Promise.resolve(),
loadVitals: () => Promise.resolve(),
},
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment