Skip to content

Instantly share code, notes, and snippets.

@ananthhh
Last active April 2, 2020 12:28
Show Gist options
  • Select an option

  • Save ananthhh/f0768df1d084922f937382265aae2b1d to your computer and use it in GitHub Desktop.

Select an option

Save ananthhh/f0768df1d084922f937382265aae2b1d to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const constants = {
ACTIONS: { changeAction: 'changeAction', onSuccess: 'onSuccess' },
GUARDS: { isFormValid: 'isFormValid' },
SERVICES: { doSubmit: 'doSubmit' }
};
const {ACTIONS, GUARDS, SERVICES} = constants;
const formMachine = Machine({
id: 'form',
initial: 'idle',
context: {
formData: {},
submitResult: undefined,
submitError: undefined,
},
states: {
idle: {
on: {
SUBMIT: [
{ target: 'loading', cond: GUARDS.isFormValid },
{ target: 'invalid' }
],
CHANGE: {
actions: ACTIONS.changeAction
}
}
},
loading: {
invoke: {
src: SERVICES.doSubmit,
onDone: {
target: 'success',
actions: assign({ submitResult: (_, event) => event.data })
},
onError: {
target: 'failure',
actions: assign({ submitError: (_, event) => event.data })
}
}
},
invalid: {
on: {
CHANGE: [
{ target: 'idle', cond: GUARDS.isFormValid },
{ actions: ACTIONS.changeAction }
]
}
},
success: {
type: 'final',
entry: [ACTIONS.onSuccess]
},
failure: {
on: {
SUBMIT: [
{ target: 'loading', cond: GUARDS.isFormValid },
'invalid'
],
CHANGE: {
target: 'idle',
actions: ACTIONS.changeAction
}
}
}
}
}, {
guards: {
[GUARDS.isFormValid]: (context, _event) => !Object.values(context.formData).filter(r => !r).length
},
actions: {
[ACTIONS.changeAction]: assign({
formData: (context, event) => ({
...context.formData,
[event.data.key]: event.data.value
})
})
},
services: {
[SERVICES.doSubmit]: async (context, _event) => new Promise(resolve => setTimeout(() => resolve(context.formData), 5000))
}
});
const constants = {
ACTIONS: { changeAction: 'changeAction', onSuccess: 'onSuccess' },
GUARDS: { isFormValid: 'isFormValid' },
SERVICES: { doSubmit: 'doSubmit' }
};
const {ACTIONS, GUARDS, SERVICES} = constants;
const formMachine = Machine({
id: 'form',
initial: 'idle',
context: {
formData: {},
submitResult: undefined,
submitError: undefined,
},
states: {
idle: {
on: {
SUBMIT: [
{ target: 'loading', cond: GUARDS.isFormValid },
{ target: 'invalid' }
],
CHANGE: {
actions: ACTIONS.changeAction
}
}
},
loading: {
invoke: {
src: SERVICES.doSubmit,
onDone: {
target: 'success',
actions: assign({ submitResult: (_, event) => event.data })
},
onError: {
target: 'failure',
actions: assign({ submitError: (_, event) => event.data })
}
}
},
invalid: {
on: {
CHANGE: [
{ target: 'idle', cond: GUARDS.isFormValid },
{ actions: ACTIONS.changeAction }
]
}
},
success: {
type: 'final',
entry: [ACTIONS.onSuccess]
},
failure: {
on: {
SUBMIT: [
{ target: 'loading', cond: GUARDS.isFormValid },
'invalid'
],
CHANGE: {
target: 'idle',
actions: ACTIONS.changeAction
}
}
}
}
}, {
guards: {
[GUARDS.isFormValid]: (context, _event) => !Object.values(context.formData).filter(r => !r).length
},
actions: {
[ACTIONS.changeAction]: assign({
formData: (context, event) => ({
...context.formData,
[event.data.key]: event.data.value
})
})
},
services: {
[SERVICES.doSubmit]: async (context, _event) => new Promise(resolve => setTimeout(() => resolve(context.formData), 5000))
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment