mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
Add functionality to filter posts by category
- only filters if you send two requests :'(
This commit is contained in:
parent
bb93052b88
commit
65f11d6f98
@ -22,8 +22,7 @@
|
|||||||
slot-scope="item"
|
slot-scope="item"
|
||||||
class="locale-menu-item"
|
class="locale-menu-item"
|
||||||
:route="item.route"
|
:route="item.route"
|
||||||
:parents="item.parents"
|
@click.stop.prevent="filterPosts(item.route.name, toggleMenu)"
|
||||||
@click.stop.prevent="filterPostsByCategory(item.route.name, toggleMenu)"
|
|
||||||
>
|
>
|
||||||
<ds-icon :name="item.route.icon" />
|
<ds-icon :name="item.route.icon" />
|
||||||
{{ item.route.name }}
|
{{ item.route.name }}
|
||||||
@ -33,8 +32,8 @@
|
|||||||
</dropdown>
|
</dropdown>
|
||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
import gql from 'graphql-tag'
|
|
||||||
import Dropdown from '~/components/Dropdown'
|
import Dropdown from '~/components/Dropdown'
|
||||||
|
import { filterPosts } from '~/graphql/PostQuery.js'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
components: {
|
components: {
|
||||||
@ -45,6 +44,12 @@ export default {
|
|||||||
offset: { type: [String, Number], default: '16' },
|
offset: { type: [String, Number], default: '16' },
|
||||||
categories: { type: Array, default: () => [] },
|
categories: { type: Array, default: () => [] },
|
||||||
},
|
},
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
filter: {},
|
||||||
|
pageSize: 12,
|
||||||
|
}
|
||||||
|
},
|
||||||
computed: {
|
computed: {
|
||||||
routes() {
|
routes() {
|
||||||
let routes = this.categories.map(category => {
|
let routes = this.categories.map(category => {
|
||||||
@ -58,28 +63,33 @@ export default {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
filterPostsByCategory(name) {
|
filterPosts(name) {
|
||||||
const filterPostsByCategoryMutation = gql`
|
this.filter = { categories_some: { name } }
|
||||||
query($name: String) {
|
this.$apollo.mutate({
|
||||||
Post(filter: { categories_some: { name: $name } }) {
|
mutation: filterPosts(this.$i18n),
|
||||||
title
|
variables: {
|
||||||
id
|
filter: this.filter,
|
||||||
categories {
|
first: this.pageSize,
|
||||||
name
|
offset: 0,
|
||||||
}
|
},
|
||||||
|
update: (store, { data: { Post } }) => {
|
||||||
|
const data = store.readQuery({
|
||||||
|
query: filterPosts(this.$i18n),
|
||||||
|
variables: {
|
||||||
|
filter: {},
|
||||||
|
first: this.pageSize,
|
||||||
|
offset: 0,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
data.Post = Post
|
||||||
|
data.Post.push(Post)
|
||||||
|
const index = data.Post.findIndex(old => old.id === Post.id)
|
||||||
|
if (index !== -1) {
|
||||||
|
data.Post.splice(index, 1)
|
||||||
}
|
}
|
||||||
}
|
store.writeQuery({ query: filterPosts(this.$i18n), data })
|
||||||
`
|
},
|
||||||
setTimeout(() => {
|
})
|
||||||
this.$apollo
|
|
||||||
.mutate({
|
|
||||||
mutation: filterPostsByCategoryMutation,
|
|
||||||
variables: { name },
|
|
||||||
})
|
|
||||||
.then(res => {
|
|
||||||
// update the Post query
|
|
||||||
})
|
|
||||||
}, 500)
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@ -77,3 +77,49 @@ export default i18n => {
|
|||||||
}
|
}
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const filterPosts = i18n => {
|
||||||
|
const lang = i18n.locale().toUpperCase()
|
||||||
|
return gql(`
|
||||||
|
query Post($filter: _PostFilter, $first: Int, $offset: Int) {
|
||||||
|
Post(filter: $filter, first: $first, offset: $offset) {
|
||||||
|
id
|
||||||
|
title
|
||||||
|
contentExcerpt
|
||||||
|
createdAt
|
||||||
|
disabled
|
||||||
|
deleted
|
||||||
|
slug
|
||||||
|
image
|
||||||
|
author {
|
||||||
|
id
|
||||||
|
avatar
|
||||||
|
slug
|
||||||
|
name
|
||||||
|
disabled
|
||||||
|
deleted
|
||||||
|
contributionsCount
|
||||||
|
shoutedCount
|
||||||
|
commentsCount
|
||||||
|
followedByCount
|
||||||
|
followedByCurrentUser
|
||||||
|
location {
|
||||||
|
name: name${lang}
|
||||||
|
}
|
||||||
|
badges {
|
||||||
|
id
|
||||||
|
key
|
||||||
|
icon
|
||||||
|
}
|
||||||
|
}
|
||||||
|
commentsCount
|
||||||
|
categories {
|
||||||
|
id
|
||||||
|
name
|
||||||
|
icon
|
||||||
|
}
|
||||||
|
shoutedCount
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|||||||
@ -28,11 +28,11 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import FilterMenu from '~/components/FilterMenu/FilterMenu.vue'
|
import FilterMenu from '~/components/FilterMenu/FilterMenu.vue'
|
||||||
import gql from 'graphql-tag'
|
|
||||||
import uniqBy from 'lodash/uniqBy'
|
import uniqBy from 'lodash/uniqBy'
|
||||||
import HcPostCard from '~/components/PostCard'
|
import HcPostCard from '~/components/PostCard'
|
||||||
import HcLoadMore from '~/components/LoadMore.vue'
|
import HcLoadMore from '~/components/LoadMore.vue'
|
||||||
import { mapGetters } from 'vuex'
|
import { mapGetters } from 'vuex'
|
||||||
|
import { filterPosts } from '~/graphql/PostQuery.js'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
components: {
|
components: {
|
||||||
@ -105,48 +105,7 @@ export default {
|
|||||||
apollo: {
|
apollo: {
|
||||||
Post: {
|
Post: {
|
||||||
query() {
|
query() {
|
||||||
return gql(`
|
return filterPosts(this.$i18n)
|
||||||
query Post($filter: _PostFilter, $first: Int, $offset: Int) {
|
|
||||||
Post(filter: $filter, first: $first, offset: $offset) {
|
|
||||||
id
|
|
||||||
title
|
|
||||||
contentExcerpt
|
|
||||||
createdAt
|
|
||||||
disabled
|
|
||||||
deleted
|
|
||||||
slug
|
|
||||||
image
|
|
||||||
author {
|
|
||||||
id
|
|
||||||
avatar
|
|
||||||
slug
|
|
||||||
name
|
|
||||||
disabled
|
|
||||||
deleted
|
|
||||||
contributionsCount
|
|
||||||
shoutedCount
|
|
||||||
commentsCount
|
|
||||||
followedByCount
|
|
||||||
followedByCurrentUser
|
|
||||||
location {
|
|
||||||
name: name${this.$i18n.locale().toUpperCase()}
|
|
||||||
}
|
|
||||||
badges {
|
|
||||||
id
|
|
||||||
key
|
|
||||||
icon
|
|
||||||
}
|
|
||||||
}
|
|
||||||
commentsCount
|
|
||||||
categories {
|
|
||||||
id
|
|
||||||
name
|
|
||||||
icon
|
|
||||||
}
|
|
||||||
shoutedCount
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`)
|
|
||||||
},
|
},
|
||||||
variables() {
|
variables() {
|
||||||
return {
|
return {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user