Skip to content

Instantly share code, notes, and snippets.

@kayue
Last active April 17, 2021 10:09
Show Gist options
  • Save kayue/fb333131f98472bb95239793c20cb871 to your computer and use it in GitHub Desktop.
Save kayue/fb333131f98472bb95239793c20cb871 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const fetchMachine = Machine({
id: 'checkout',
initial: 'selectCountry',
type: 'parallel',
context: {
shippingCountry: 'US',
shippingMethod: null,
},
states: {
selectCountry: {
initial: 'fetching',
onDone: 'selectShippingMethod',
on: {
CHANGE: {
target: '.idle'
}
},
states: {
fetching: {
invoke: {
src: 'fetchCountryOptions',
onDone: {
target: 'idle',
}
}
},
idle: {
on: {
SAVE: {
target: 'complete'
}
}
},
complete: {
type: 'final'
},
}
},
selectShippingMethod: {
initial: 'fetching',
onDone: {
target: 'selectAddress',
},
states: {
fetching: {
invoke: {
src: 'fetchShippingMethodOptions',
onDone: {
target: 'idle',
}
}
},
idle: {
on: {
SAVE: {
target: 'complete'
}
}
},
complete: {
type: 'final'
},
}
},
selectAddress: {
initial: 'fetching',
states: {
fetching: {
invoke: {
src: 'fetchAddressOptions',
onDone: {
target: 'idle',
}
}
},
idle: {
on: {
SAVE: {
target: 'submitting'
}
}
},
submitting: {
invoke: {
src: 'validate',
onDone: {
target: 'complete',
},
onError: {
target: 'idle',
actions: 'assignErrorMessageToContext',
},
}
},
complete: {
type: 'final'
},
}
},
selectPaymentMethod: {
initial: 'idle',
states: {
idle: {}
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment