Skip to content

Instantly share code, notes, and snippets.

@savioserra
Created February 12, 2021 02:29
Show Gist options
  • Save savioserra/c9bace3d6ba2e6d90301143aa0d53741 to your computer and use it in GitHub Desktop.
Save savioserra/c9bace3d6ba2e6d90301143aa0d53741 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({
initial: 'initial',
context: {
restaurants: [],
addressesPredictions: [],
selectedRestaurant: null,
selectedPrediction: null,
},
states: {
error: {},
initial: {
entry: ['focusInput'],
on: {SEARCH: 'loading_predictions'},
},
not_found: {},
loading_predictions: {
invoke: {
src: 'fetchPredictions',
onError: 'not_found',
onDone: {
target: 'addresses_predictions',
actions: assign({addressesPredictions: (_, {data}) => data}),
},
},
},
loading_restaurants: {
invoke: {
src: 'fetchRestaurants',
onDone: [
{
target: 'multiple_restaurants_result',
cond: 'hasMultipleRestaurantsResult',
actions: assign({restaurants: (_, {data}) => data}),
},
{
target: 'single_restaurant_result',
cond: 'hasSingleRestaurantsResult',
actions: assign({restaurants: (_, {data}) => data}),
},
],
onError: {
target: 'not_found',
},
},
},
multiple_restaurants_result: {
type: 'final',
},
addresses_predictions: {
on: {
SEARCH: 'loading_predictions',
NEXT: {
target: 'loading_restaurants',
cond: 'hasAddressPredictionSelected',
},
SELECT_ADDRESS: {
actions: assign({selectedPrediction: (_, {data}) => data}),
},
},
},
single_restaurant_result: {
type: 'final',
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment