Last active
December 12, 2019 15:19
-
-
Save andymantell/600375cf81fcfd65098d093abbb2fdab 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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const previousClearanceHeld = (context, event) => { | |
return context.previousClearanceHeld | |
}; | |
const formMachine = Machine( | |
{ | |
id: 'form', | |
initial: 'idle', | |
context: { | |
data: undefined, | |
error: undefined, | |
location: undefined, | |
status: undefined, | |
}, | |
states: { | |
idle: { | |
on: { | |
LOAD: { | |
target: 'loading', | |
actions: ['setLocation'], | |
}, | |
}, | |
}, | |
loading: { | |
invoke: { | |
src: 'fetchFormPageData', | |
onDone: { | |
target: 'loaded', | |
actions: ['setState'], | |
}, | |
onError: { | |
target: 'errorapi', | |
actions: ['setError'], | |
}, | |
}, | |
}, | |
loaded: { | |
on: { | |
CHANGE: { | |
target: 'loaded', | |
actions: ['setForm'], | |
}, | |
SAVE_UNFINISHED: { | |
target: 'saving', | |
actions: ['setForm'], | |
}, | |
SAVE_CONTINUE: { | |
target: 'saving', | |
actions: ['setForm'], | |
}, | |
}, | |
}, | |
validating: { | |
on: { | |
INVALID: 'errorvalidation', | |
VALID: 'saving', | |
}, | |
}, | |
errorvalidation: { | |
on: { | |
SAVE_UNFINISHED: 'saving', | |
SAVE_CONTINUE: 'validating', | |
}, | |
}, | |
saving: { | |
invoke: { | |
src: 'saveFormData', | |
onDone: { | |
target: 'saved', | |
actions: ['setState'], | |
}, | |
onError: { | |
target: 'errorapi', | |
actions: ['setError'], | |
}, | |
}, | |
}, | |
saved: { | |
type: 'final', | |
}, | |
errorapi: { | |
on: { | |
RETRY: 'loading', | |
}, | |
}, | |
}, | |
}, | |
{ | |
actions: { | |
setLocation: assign({ | |
location: (ctx, e) => e.location, | |
}), | |
setForm: assign({ | |
data: (ctx, e) => e.data, | |
}), | |
setState: assign({ | |
data: (ctx, e) => e.data.data, | |
status: (ctx, e) => e.data.status, | |
}), | |
setError: assign({ | |
error: (ctx, e) => e.data.message, | |
}), | |
}, | |
} | |
); | |
const previousClearancesMachine = Machine({ | |
id: 'previous-clearances', | |
initial: 'previous-clearances-held', | |
context: { | |
previousClearanceHeld: false | |
}, | |
invoke: { | |
id: 'form-machine', | |
src: formMachine | |
}, | |
states: { | |
'previous-clearances-held': { | |
entry: send('LOAD', { to: 'form-machine' }), | |
on: { | |
SAVE_CONTINUE: [ | |
{ | |
target: 'conditional-check', | |
actions: send('SAVE_CONTINUE', { | |
to: 'form-machine' | |
}) | |
}, | |
{ | |
target: 'conditional-check', | |
actions: send('SAVE_CONTINUE', { | |
to: 'form-machine' | |
}) | |
} | |
], | |
SAVE_UNFINISHED: [ | |
{ | |
target: 'conditional-check', | |
actions: send('SAVE_UNFINISHED', { | |
to: 'form-machine' | |
}) | |
}, | |
{ | |
target: 'conditional-check', | |
actions: send('SAVE_UNFINISHED', { | |
to: 'form-machine' | |
}) | |
} | |
] | |
} | |
}, | |
'conditional-check': { | |
on: { | |
'': [ | |
{ | |
target: 'most-recent-clearance', | |
cond: previousClearanceHeld, | |
}, | |
{ | |
target: 'clearance-withdrawn' | |
} | |
] | |
} | |
}, | |
'most-recent-clearance': { | |
on: { | |
SAVE_CONTINUE: { | |
target: 'clearance-withdrawn', | |
actions: send('SAVE_CONTINUE', { | |
to: 'form-machine' | |
}) | |
}, | |
SAVE_UNFINISHED: { | |
target: 'clearance-withdrawn', | |
actions: send('SAVE_UNFINISHED', { | |
to: 'form-machine' | |
}) | |
} | |
} | |
}, | |
'clearance-withdrawn': { | |
on: { | |
SAVE_CONTINUE: { | |
target: 'summary', | |
actions: send('SAVE_CONTINUE', { | |
to: 'form-machine' | |
}) | |
}, | |
SAVE_UNFINISHED: { | |
target: 'summary', | |
actions: send('SAVE_UNFINISHED', { | |
to: 'form-machine' | |
}) | |
} | |
} | |
}, | |
summary: { | |
type: 'final' | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment