Created
February 12, 2021 02:29
-
-
Save savioserra/c9bace3d6ba2e6d90301143aa0d53741 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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