Implement refresh posts, fix duplicate posts bug

- on logo click
- scrollToTop
- remove duplicate records

Co-authored-by: kachulio1 <jngugi88@gmail.com>
This commit is contained in:
mattwr18 2019-11-05 14:57:54 +01:00
parent f64b71e746
commit 2ea0364242
5 changed files with 66 additions and 14 deletions

View File

@ -0,0 +1,2 @@
export const first = 12
export const offset = 0

View File

@ -6,7 +6,11 @@
<ds-flex class="main-navigation-flex"> <ds-flex class="main-navigation-flex">
<ds-flex-item :width="{ lg: '3.5%' }" /> <ds-flex-item :width="{ lg: '3.5%' }" />
<ds-flex-item :width="{ base: '80%', sm: '80%', md: '80%', lg: '15%' }"> <ds-flex-item :width="{ base: '80%', sm: '80%', md: '80%', lg: '15%' }">
<nuxt-link :to="{ name: 'index' }"> <nuxt-link
:to="{ name: 'index' }"
@click.native="refreshPosts({ i18n: $i18n })"
v-scroll-to="'.main-navigation'"
>
<ds-logo /> <ds-logo />
</nuxt-link> </nuxt-link>
</ds-flex-item> </ds-flex-item>
@ -225,6 +229,7 @@ export default {
...mapActions({ ...mapActions({
quickSearchClear: 'search/quickClear', quickSearchClear: 'search/quickClear',
quickSearch: 'search/quickSearch', quickSearch: 'search/quickSearch',
refreshPosts: 'posts/refreshPosts',
}), }),
goToPost(item) { goToPost(item) {
this.$nextTick(() => { this.$nextTick(() => {

View File

@ -15,7 +15,7 @@
</div> </div>
</ds-grid-item> </ds-grid-item>
<template v-if="hasResults"> <template v-if="hasResults">
<masonry-grid-item v-for="post in posts" :key="post.id"> <masonry-grid-item v-for="post in currentPosts" :key="post.id">
<hc-post-card <hc-post-card
:post="post" :post="post"
:width="{ base: '100%', xs: '100%', md: '50%', xl: '33%' }" :width="{ base: '100%', xs: '100%', md: '50%', xl: '33%' }"
@ -94,6 +94,7 @@ export default {
orderBy: 'posts/orderBy', orderBy: 'posts/orderBy',
selectedOrder: 'posts/selectedOrder', selectedOrder: 'posts/selectedOrder',
sortingIcon: 'posts/orderIcon', sortingIcon: 'posts/orderIcon',
currentPosts: 'posts/currentPosts',
}), }),
selected: { selected: {
get() { get() {
@ -101,7 +102,7 @@ export default {
}, },
set({ value }) { set({ value }) {
this.offset = 0 this.offset = 0
this.posts = [] this.setCurrentPosts([])
this.selectOrder(value) this.selectOrder(value)
}, },
}, },
@ -119,12 +120,13 @@ export default {
return filter return filter
}, },
hasResults() { hasResults() {
return this.$apollo.loading || (this.posts && this.posts.length > 0) return this.$apollo.loading || (this.currentPosts && this.currentPosts.length > 0)
}, },
}, },
methods: { methods: {
...mapMutations({ ...mapMutations({
selectOrder: 'posts/SELECT_ORDER', selectOrder: 'posts/SELECT_ORDER',
setCurrentPosts: 'posts/SET_CURRENT_POSTS',
}), }),
clearSearch() { clearSearch() {
this.$router.push({ path: '/' }) this.$router.push({ path: '/' })
@ -152,21 +154,30 @@ export default {
if (!fetchMoreResult || fetchMoreResult.Post.length < this.pageSize) { if (!fetchMoreResult || fetchMoreResult.Post.length < this.pageSize) {
this.hasMore = false this.hasMore = false
} }
const result = Object.assign({}, previousResult, { const result = {
Post: [...previousResult.Post, ...fetchMoreResult.Post], ...previousResult,
}) Post: [
return result ...previousResult.Post.filter(prevPost => {
return (
fetchMoreResult.Post.filter(newPost => newPost.id === prevPost.id).length === 0
)
}),
...fetchMoreResult.Post,
],
}
this.setCurrentPosts(result.Post)
}, },
}) })
}, },
deletePost(deletedPost) { deletePost(deletedPost) {
this.posts = this.posts.filter(post => { const posts = this.currentPosts.filter(post => {
return post.id !== deletedPost.id return post.id !== deletedPost.id
}) })
this.setCurrentPosts(posts)
}, },
resetPostList() { resetPostList() {
this.offset = 0 this.offset = 0
this.posts = [] this.setCurrentPosts([])
this.hasMore = true this.hasMore = true
}, },
pinPost(post) { pinPost(post) {
@ -210,7 +221,7 @@ export default {
} }
}, },
update({ Post }) { update({ Post }) {
this.posts = Post this.setCurrentPosts(Post)
}, },
fetchPolicy: 'cache-and-network', fetchPolicy: 'cache-and-network',
}, },

View File

@ -394,12 +394,18 @@ export default {
if (!fetchMoreResult || fetchMoreResult.profilePagePosts.length < this.pageSize) { if (!fetchMoreResult || fetchMoreResult.profilePagePosts.length < this.pageSize) {
this.hasMore = false this.hasMore = false
} }
const result = Object.assign({}, previousResult, { const result = {
...previousResult,
profilePagePosts: [ profilePagePosts: [
...previousResult.profilePagePosts, ...previousResult.profilePagePosts.filter(prevPost => {
return (
fetchMoreResult.profilePagePosts.filter(newPost => newPost.id === prevPost.id)
.length === 0
)
}),
...fetchMoreResult.profilePagePosts, ...fetchMoreResult.profilePagePosts,
], ],
}) }
return result return result
}, },
}) })

View File

@ -4,6 +4,8 @@ import xor from 'lodash/xor'
import isEmpty from 'lodash/isEmpty' import isEmpty from 'lodash/isEmpty'
import isEqual from 'lodash/isEqual' import isEqual from 'lodash/isEqual'
import clone from 'lodash/clone' import clone from 'lodash/clone'
import { filterPosts } from '~/graphql/PostQuery'
import { first, offset } from '~/constants/posts'
const defaultFilter = {} const defaultFilter = {}
@ -26,6 +28,7 @@ export const state = () => {
...defaultFilter, ...defaultFilter,
}, },
order: orderOptions['createdAt_desc'], order: orderOptions['createdAt_desc'],
currentPosts: [],
} }
} }
@ -74,6 +77,9 @@ export const mutations = {
SELECT_ORDER(state, value) { SELECT_ORDER(state, value) {
state.order = orderOptions[value] state.order = orderOptions[value]
}, },
SET_CURRENT_POSTS(state, posts) {
state.currentPosts = posts
},
} }
export const getters = { export const getters = {
@ -114,4 +120,26 @@ export const getters = {
orderIcon(state) { orderIcon(state) {
return state.order.icon return state.order.icon
}, },
currentPosts(state) {
return state.currentPosts || []
},
}
export const actions = {
async refreshPosts({ commit, getters }, { i18n }) {
const client = this.app.apolloProvider.defaultClient
const {
data: { Post },
} = await client.query({
query: filterPosts(i18n),
variables: {
filter: getters.filter,
first,
orderBy: ['pinned_asc', getters.orderBy],
offset,
},
})
commit('SET_CURRENT_POSTS', Post)
return Post
},
} }