Skip to content

Instantly share code, notes, and snippets.

@jschaf
Created April 14, 2021 06:22
Show Gist options
  • Save jschaf/34684de93bbdf1151d866e6220d422f8 to your computer and use it in GitHub Desktop.
Save jschaf/34684de93bbdf1151d866e6220d422f8 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const machine = Machine({
id: 'activeProduct',
initial: 'pristine',
strict: true,
context: {
name: {}, // product resource name
product: {}, // current active product
dirtyProduct: {}, // locally modified fields not yet submitted
submittingProduct: {}, // fields currently being submitted
seqSubmitFails: 0, // number of sequential submit failures
},
states: {
// A pristine form matches the product in the DB.
pristine: {
on: {
MUTATE: {target: 'dirty', actions: ['mergeMutation']},
},
},
// A dirty form differs from the product in the DB.
dirty: {
on: {
MUTATE: {target: 'dirty', actions: ['mergeMutation']},
},
after: {100: {target: 'submitting', actions: ['prepareSubmit']}},
},
// A form that is currently submitting.
submitting: {
invoke: {
id: 'product/submitting',
src: 'submitMutations',
onDone: [
{cond: 'isPristine', target: 'pristine', actions: ['mergeSubmitted']},
{target: 'dirty', actions: ['mergeSubmitted']},
],
onError: {
target: 'dirty',
actions: ['handleSubmitFailure'],
},
},
on: {
MUTATE: {actions: ['mergeMutation']},
},
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment