I have a blogs page that has includes two different components filter and card
<template>
<div>
<div v-if='$apollo.loading'>Fetching data...</div>
<div v-else>
<FilterComponent :categories='categories' />
<Card :blogs='blogs' />
</div>
</div>
</template>
<script>
import blogsGQL from '../graphql/blog/blog.graphql'
import categoriesGQL from '../graphql/category/category.graphql'
import Card from '~/components/Blog/Card'
import FilterComponent from '~/components/Blog/FilterComponent'
export default {
name: 'Blogs',
components: { FilterComponent, Card },
layout: 'blog',
apollo: {
blogs: {
query: blogsGQL,
prefetch: true
},
categories: {
query: categoriesGQL,
prefetch: true
}
}
}
</script>
Inside of my filterComponent I have a select dropdown filled by categories coming from the database, when I select on of these categories a graphql query fires that fetches new blog posts filted by category_id
<template>
<div class='w-full bg-gray-200 dark:bg-gray-900 py-10'>
<div class='container mx-auto px-6 flex items-start justify-center'>
<form id='filter' @submit.prevent>
<div class='mt-16 flex flex-col w-full'>
<select id='category'
v-model.number='selected'
class='dark:border-gray-700 pl-3 py-3 shadow-sm rounded text-sm focus:outline-none focus:border-indigo-700 bg-transparent placeholder-gray-500 text-gray-500 dark:text-gray-400'
>
<option value='0' selected>Choose category</option>
<option v-for='category in categories' :key='category.id' :value='category.id'>
{{ category.category }}
</option>
</select>
</div>
</form>
</div>
</div>
</template>
<script>
import blogPostByCategoryId from '~/graphql/blog/blogByCategoryId.graphql'
export default {
name: 'FilterComponent',
props: {
categories: {
type: Array,
required: true
}
},
data() {
return {
selected: 0
}
},
apollo: {
blogPostByCategoryId: {
query: blogPostByCategoryId,
variables() {
return {
category_id: this.selected
}
},
skip() {
return !this.selected
}
}
}
}
</script>
But how do I update the card component with the new data I received from my filter component? is it possible to just pass the blogPostByCategoryId result back into the card component somehow ?
EDIT:
I managed to get some pieces working, I currently have a index.js file inside of my store directory of nuxt this file contains the following code:
import blogsGQL from '~/graphql/blog/blog.graphql'
export const state = () => ({
blogs: []
})
export const actions = {
async nuxtServerInit({ commit }, context) {
const client = context.app.apolloProvider.defaultClient
const response = await client.query({ query: blogsGQL })
commit('setBlogs', response.data.blogs)
}
}
export const mutations = {
setBlogs(state, blogs) {
state.blogs.push(blogs)
},
filter(state, categoryId) {
state.blogs.map(blog => blog.category_id === categoryId)
}
}
I query my graphql api and put all posts inside of my state, then in my parent component I pass down the data to my card component like so:
<Card :blogs='blogs' />
computed: {
blogs() {
return this.$store.state.blogs
}
},
Now, I'm not sure if I'm having a brainfart but when trying to loop over the array inside of my card component like this:
<div v-for='blog in blogs'
All I get is blank space? I console logged the blogs array and the data is inside of it.
EDIT 2 :
So I fixed my previous problem, had to use state.blogs.push(...blogs) to get it working.
I also changed my file a bit after reading some docs I now use a getter to get the filterblogs :
export const getters = {
filterdBlogs: state => (categoryId) => {
return state.blogs.filter(blog => blog.category_id === categoryId)
}
}
I call this method on the apollo update method inside of my filterComponent :
update(data) {
const id = data.blogByCategoryId[0].category_id
this.$store.getters.filterdBlogs(id)
}
This seems to work, console logging the filterdblogs method returns correct items but my card component is not being updated.
EDIT 3
so I changed the card component instead of passing it through props I do this now
// card component
<template>
<div class='w-full bg-gray-200 dark:bg-gray-900 py-10'>
<div class='container mx-auto px-6 flex justify-center'>
<div class='w-full'>
<div v-for='blog in blogs'
:key='blog.id'
class='flex flex-row mx-auto bg-white dark:bg-gray-800 justify-center shadow rounded'>
<div class='w-full lg:w-1/3 px-12 flex flex-col items-center py-10'>
<div class='w-24 h-24 mb-3 p-2 rounded-full bg-gray-200 dark:bg-gray-700 flex items-center justify-center'>
<img class='w-full h-full overflow-hidden object-cover rounded-full'
:src='`https://tuk-cdn.s3.amazonaws.com/assets/components/grid_cards/gc_${blog.id}.png`'
alt='avatar' />
</div>
<h2 class='text-gray-800 dark:text-gray-100 text-xl tracking-normal font-medium mb-1'>{{ blog.title }}</h2>
<p class='text-gray-600 dark:text-gray-100 text-sm tracking-normal font-normal mb-8 text-center w-10/12'>
{{ blog.big_text }}</p>
<p class='text-gray-600 dark:text-gray-100 text-sm tracking-normal font-normal mb-8 text-center w-10/12'>
{{ blog.small_text }}</p>
<div class='flex items-start'>
<div class='mx-6 border-l border-r'>
<NuxtLink :to='`blog/${blog.slug}`'>
<button type='submit'
class='group relative w-full flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500'>
Read more
</button>
</NuxtLink>
</div>
<div class='mx-4 border-l border-r'>
<h2
class='group relative w-full flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white text-black'>
Posted on {{ blog.created_at | formatDate }}
</h2>
</div>
<div class='mx-4 border-l border-r'>
<h2
class='group relative w-full flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white text-black'>
Updated on {{ blog.updated_at | formatDate }}
</h2>
</div>
</div>
</div>
</div>
<!-- Card code block end -->
</div>
</div>
</div>
</template>
<script>
export default {
name: 'Card',
filters: {
formatDate: (dateStr) =>
Intl.DateTimeFormat('us-EN').format(new Date(dateStr))
},
computed: {
blogs() {
return this.$store.state.blogs
}
}
}
</script>
Now when I select a value on my dropdown menu the state is not being updated;
// dropdown component
<template>
<div class='w-full bg-gray-200 dark:bg-gray-900 py-10'>
<div class='container mx-auto px-6 flex items-start justify-center'>
<form id='filter' @submit.prevent>
<div class='mt-16 flex flex-col w-full'>
<select id='category'
v-model.number='selected'
class='dark:border-gray-700 pl-3 py-3 shadow-sm rounded text-sm focus:outline-none focus:border-indigo-700 bg-transparent placeholder-gray-500 text-gray-500 dark:text-gray-400'
>
<option value='0' selected>Choose category</option>
<option v-for='category in categories' :key='category.id' :value='category.id'>
{{ category.category }}
</option>
</select>
</div>
</form>
</div>
</div>
</template>
<script>
import blogPostByCategoryId from '~/graphql/blog/blogByCategoryId.graphql'
export default {
name: 'FilterComponent',
props: {
categories: {
type: Array,
required: true
}
},
data() {
return {
selected: 0
}
},
apollo: {
blogPostByCategoryId: {
query: blogPostByCategoryId,
variables() {
return {
category_id: this.selected
}
},
skip() {
return !this.selected
},
update(data) {
const id = data.blogByCategoryId[0].category_id
this.$store.commit('filter', {
categoryId: id
})
}
}
}
}
</script>
<style scoped>
</style>
my filter method looks like this :
export const mutations = {
filter(state, payload) {
return state.blogs.filter(blog => blog.category_id === payload.categoryId)
}
}