Skip to content

Instantly share code, notes, and snippets.

@Xaraxia
Last active August 19, 2020 07:18
Show Gist options
  • Save Xaraxia/de4fbfc0e4003efff3f768cb918b3905 to your computer and use it in GitHub Desktop.
Save Xaraxia/de4fbfc0e4003efff3f768cb918b3905 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const initialContext = {
isUQ: false,
institution: undefined,
orgunit: undefined,
isStaff: false,
isStudent: false,
exceptionreason: undefined,
purpose: undefined,
cluster: undefined,
clusterxp: undefined,
linuxxp: undefined,
email: undefined,
fullname: undefined,
uqusername: undefined,
statestack: []
};
const surveyMachine = Machine({
id: 'clustersurvey',
initial: 'persontype',
context: initialContext,
states: {
// see https://xstate.js.org/docs/guides/history.html#history-state-configuration
'persontype': {
on: {
SELECT_UQSTAFF: {
actions: ['setUQ', 'setStaff'],
target: 'select_ou'
},
SELECT_UQSTUDENT: {
actions: ['setUQ', 'setStudent'],
target: 'select_ou'
},
SELECT_OTHSTAFF: {
actions: ['setNotUQ', 'setStaff'],
target: 'select_institution'
},
SELECT_OTHSTUDENT: {
actions: ['setNotUQ', 'setStudent'],
target: 'select_institution'
},
RESTART: 'reset'
},
exit: [assign({ statestack: (context) => context.statestack.concat('persontype') })]
},
'select_ou': {
on: {
NEXT: [
{
target: 'purpose',
cond: 'isStaff',
actions: [
'setOU',
assign({ statestack: (context) => context.statestack.concat('select_ou') })
]
},
{
target: 'exceptionreason',
cond: 'isStudent',
actions: [
'setOU',
assign({ statestack: (context) => context.statestack.concat('select_ou') })
]
}
],
PREV: 'goback',
RESTART: 'reset'
}
},
'select_institution': {
on: {
SELECT_INSTITUTION: [
{
target: 'purpose',
cond: 'isStaff',
actions: [
'setInstitution',
assign({ statestack: (context) => context.statestack.concat('select_institution') })
]
},
{
target: 'exceptionreason',
cond: 'isStudent',
actions: [
'setInstitution',
assign({ statestack: (context) => context.statestack.concat('select_institution') })
]
}
],
NOT_VALID_INSTITUTION: {
target: 'invalidinstitution', actions: ['setInstitution']
},
PREV: 'goback',
RESTART: 'reset'
}
},
'exceptionreason': {
on: {
NEXT: {
target: 'purpose',
actions: [assign({ statestack: (context) => context.statestack.concat('exceptionreason') })]
},
PREV: 'goback',
RESTART: 'reset'
}
},
'purpose': {
on: {
NEXT: {
target: 'whichcluster',
actions: [assign({ statestack: (context) => context.statestack.concat('purpose') })]
},
PREV: 'goback',
RESTART: 'reset'
}
},
'whichcluster': {
on: {
NEXT: {
target: 'linuxxp',
actions: [assign({ statestack: (context) => context.statestack.concat('whichcluster') })]
},
PREV: 'goback',
RESTART: 'reset'
},
},
'linuxxp': {
on: {
NEXT: {
target: 'clusterxp',
actions: [assign({ statestack: (context) => context.statestack.concat('linuxxp') })]
},
PREV: 'goback',
RESTART: 'reset'
},
},
'clusterxp': {
on: {
NEXT: {
target: 'accountdetails',
actions: [assign({ statestack: (context) => context.statestack.concat('clusterxp') })]
},
PREV: 'goback',
RESTART: 'reset'
}
},
'accountdetails': {
on: {
NEXT: [
{
target: 'checkneedsaccount',
actions: [
'setUQusername',
'setEmail',
'setFullName',
assign({ statestack: (context) => context.statestack.concat('accountdetails') })
]
},
],
PREV: 'goback',
RESTART: 'reset'
}
},
'checkneedsaccount': {
on: {
"": [
{
target: 'confirmrequest',
cond: 'hasAccount',
},
{
target: 'uqacctapplyinfo',
cond: 'needsAccount',
},
],
}
},
'uqacctapplyinfo': {
on: {
NEXT: {
target: 'confirmrequest',
actions: [assign({ statestack: (context) => context.statestack.concat('uqacctapplyinfo') })]
},
PREV: 'goback',
RESTART: 'reset'
}
},
'confirmrequest': {
on: {
NEXT: {
target: 'submitted',
actions: [assign({ statestack: (context) => context.statestack.concat('confirmrequest') })]
},
PREV: 'goback',
RESTART: 'reset'
}
},
'submitted': {
type: 'final'
},
'invalidinstitution': {
type: 'final'
},
'goback': {
on: {
"": [
{ target: 'persontype', cond: (ctx) => ctx.statestack[ctx.statestack.length - 1] == 'persontype' },
{ target: 'select_ou', cond: (ctx) => ctx.statestack[ctx.statestack.length - 1] == 'select_ou' },
{ target: 'select_institution', cond: (ctx) => ctx.statestack[ctx.statestack.length - 1] == 'select_institution' },
{ target: 'exceptionreason', cond: (ctx) => ctx.statestack[ctx.statestack.length - 1] == 'exceptionreason' },
{ target: 'purpose', cond: (ctx) => ctx.statestack[ctx.statestack.length - 1] == 'purpose' },
{ target: 'whichcluster', cond: (ctx) => ctx.statestack[ctx.statestack.length - 1] == 'whichcluster' },
{ target: 'linuxxp', cond: (ctx) => ctx.statestack[ctx.statestack.length - 1] == 'linuxxp' },
{ target: 'clusterxp', cond: (ctx) => ctx.statestack[ctx.statestack.length - 1] == 'clusterxp' },
{ target: 'accountdetails', cond: (ctx) => ctx.statestack[ctx.statestack.length - 1] == 'accountdetails' }
]
},
exit: ['popHistory'],
},
'reset': {
on: {
"": {
target: 'persontype',
actions: 'resetState'
}
}
}
}
}, {
actions: {
setUQ: assign({ isUQ: true }),
setNotUQ: assign({ isUQ: false }),
setStaff: assign({ isStaff: true, isStudent: false }),
setStudent: assign({ isStudent: true, isStaff: false }),
setInstitution: assign({ institution: (context, event) => { var _a; return ((_a = event.value) !== null && _a !== void 0 ? _a : '').trim() || context.institution; } }),
setOU: assign({ orgunit: (context, event) => { var _a; return ((_a = event.value) !== null && _a !== void 0 ? _a : '').trim() || context.orgunit; } }),
setUQusername: assign({ uqusername: (context, event) => { var _a; return ((_a = event.value) !== null && _a !== void 0 ? _a : { 'uqusername': '' }).uqusername.trim() || context.uqusername; } }),
setEmail: assign({ uqusername: (context, event) => { var _a; return ((_a = event.value) !== null && _a !== void 0 ? _a : { 'email': '' }).email.trim() || context.email; } }),
setFullName: assign({ uqusername: (context, event) => { var _a; return ((_a = event.value) !== null && _a !== void 0 ? _a : { 'fullname': '' }).fullname.trim() || context.fullname; } }),
popHistory: assign((context, event) => ({ statestack: context.statestack.slice(0, -1) })),
// pushHistory: assign<SurveyContext, SurveyEvent>({ statestack: (context: SurveyContext, event: SurveyEvent, meta) : Partial<SurveyContext> => context.statestack.concat(context.statestack, meta.state.value)}),
resetState: assign(Object.assign({}, initialContext))
},
guards: {
isExternal: (context) => !context.isUQ,
isInternal: (context) => context.isUQ,
isStudent: (context) => context.isStudent,
isStaff: (context) => context.isStaff,
hasAccount: (context) => typeof (context.uqusername) !== 'undefined' && context.uqusername != '',
needsAccount: (context) => typeof (context.uqusername) === 'undefined' || context.uqusername == '',
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment