Skip to content

Instantly share code, notes, and snippets.

@mtermoul
Created July 27, 2018 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtermoul/3a7f02fa0348a941f60af46c260dd8de to your computer and use it in GitHub Desktop.
Save mtermoul/3a7f02fa0348a941f60af46c260dd8de to your computer and use it in GitHub Desktop.
...
actions: {
...
fetchCities ({commit, dispatch, state}) {
// will populate the cities from cosmic js REST API
const params = {
type_slug: 'cities'
}
Cosmic.getObjectsByType(params)
.then(data => {
let locations = {}
let country = ''
data.objects.forEach(city => {
const cityKey = city.slug.toUpperCase()
const stateKey = city.metadata.state.slug.toUpperCase()
...
locations[stateKey][cityKey].city = city.title
locations[stateKey][cityKey].state = city.metadata.state.title
locations[stateKey][cityKey].postalCode = city.metadata.postal_code
locations[stateKey][cityKey].dataUrl = city.metadata.data_url
locations[stateKey][cityKey].country = country
locations[stateKey][cityKey].geoPoint = {
latitude: city.metadata.lat,
longitude: city.metadata.lng
}
})
commit('SET_AVAILABLE_LOCATIONS', locations)
// set the initial default location
dispatch('updateSelectedLocation', {state: 'FL', city: 'ORLANDO', postalCode: '32821'})
})
.catch(err => {
console.log(err)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment