Skip to content

Instantly share code, notes, and snippets.

@mtermoul
Last active September 15, 2018 21:38
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/91d143764e0e25e9f5ab549937efb244 to your computer and use it in GitHub Desktop.
Save mtermoul/91d143764e0e25e9f5ab549937efb244 to your computer and use it in GitHub Desktop.
garage-sale/Vuex Store
// garage-sale/src/store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
import Cosmic from '../api/cosmic' // used for Rest API
import { ApolloClient, HttpLink, InMemoryCache } from 'apollo-boost' // used for GraphQL API
import gql from 'graphql-tag'
const client = new ApolloClient({
link: new HttpLink({uri: 'https://graphql.cosmicjs.com/v1'}),
cache: new InMemoryCache()
})
Vue.use(Vuex)
export default new Vuex.Store({
state: {
isDataReady: false,
posts: [],
...
searchTerm: ''
},
...
actions: {
...
loadInitialData ({commit, dispatch}, payload) {
dispatch('fetchPostsGQ', payload)
dispatch('fetchPostConditions')
dispatch('fetchPostCategories')
commit('SET_USER_LOCATION', {city: 'Orlando', state: 'FL', postalCode: '32821'})
},
fetchPostsGQ ({commit, state}, payload) {
if (state.isDataReady) {
commit('SET_IS_DATA_READY', false)
}
client
.query({
query: gql`query Posts($bucket: String, $type: String!) {
objectsByType(bucket_slug: $bucket, type_slug: $type) {
_id
title
slug
metadata
}
}`,
variables: {bucket: 'garage-sale', type: 'posts'}
})
.then(data => {
commit('SET_POSTS', data.data.objectsByType)
commit('SET_IS_DATA_READY', true)
})
.catch(error => {
// eslint-disable-next-line
console.log(error)
})
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment