-
-
Save mtermoul/91d143764e0e25e9f5ab549937efb244 to your computer and use it in GitHub Desktop.
garage-sale/Vuex Store
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
// 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