Skip to content

Instantly share code, notes, and snippets.

@LevelbossMike
Last active July 4, 2020 14:39
Show Gist options
  • Select an option

  • Save LevelbossMike/cb5fcec61b248200db1c192988fb5f04 to your computer and use it in GitHub Desktop.

Select an option

Save LevelbossMike/cb5fcec61b248200db1c192988fb5f04 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
function notifyActionName(context, event) {
window.alert(`Triggered action \`${event.type}\``)
}
const fetchMachine = Machine( {
id: 'quickstart-button-step-8',
context: {
disabled: false
},
type: 'parallel',
states: {
interactivity: {
initial: 'unknown',
states: {
unknown: {
on: {
'': [{ target: 'enabled', cond: 'isEnabled' }, { target: 'disabled' }],
},
},
enabled: {
on: {
DISABLE: 'disabled',
},
},
disabled: {
on: {
ENABLE: 'enabled',
},
},
},
},
activity: {
initial: 'idle',
states: {
idle: {
on: {
SUBMIT: {
target: 'busy',
cond: 'isEnabled',
},
},
},
busy: {
entry: ['handleSubmit'],
on: {
SUCCESS: 'success',
ERROR: 'error',
},
},
success: {
entry: ['handleSuccess'],
on: {
SUBMIT: {
target: 'busy',
cond: 'isEnabled',
},
},
},
error: {
entry: ['handleError'],
on: {
SUBMIT: {
target: 'busy',
cond: 'isEnabled',
},
},
},
},
},
},
},
{
actions: {
handleSubmit: notifyActionName,
handleSuccess: notifyActionName,
handleError: notifyActionName,
},
guards: {
isEnabled(context) {
return !context.disabled;
},
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment