made load more a component and added it to the newsfeed too

This commit is contained in:
Grzegorz Leoniec 2018-10-26 17:38:25 +02:00
parent 5561cda13e
commit 201b02ac1d
4 changed files with 80 additions and 28 deletions

21
components/LoadMore.vue Normal file
View File

@ -0,0 +1,21 @@
<template>
<ds-space
margin-top="large"
style="text-align: center">
<ds-button
:icon="loading ? 'spinner' : 'arrow-down'"
:disabled="loading"
ghost
@click="$emit('click')">
mehr laden
</ds-button>
</ds-space>
</template>
<script>
export default {
props: {
loading: { type: Boolean, default: false }
}
}
</script>

View File

@ -1,7 +1,7 @@
import gql from 'graphql-tag' import gql from 'graphql-tag'
export default gql(` export default gql(`
query User($slug: String!, $offset: Int, $first: Int) { query User($slug: String!, $first: Int, $offset: Int) {
User(slug: $slug) { User(slug: $slug) {
id id
name name

View File

@ -1,33 +1,46 @@
<template> <template>
<ds-flex <div>
:width="{ base: '100%' }" <ds-flex
gutter="base"> :width="{ base: '100%' }"
<ds-flex-item gutter="base">
v-for="post in Post" <ds-flex-item
:width="{ base: '100%', xs: '100%', md: '50%', xl: '33%' }" v-for="post in Post"
:key="post.id"> :width="{ base: '100%', xs: '100%', md: '50%', xl: '33%' }"
<hc-post-card :post="post" /> :key="post.id">
</ds-flex-item> <hc-post-card :post="post" />
</ds-flex> </ds-flex-item>
</ds-flex>
<hc-load-more
v-if="true"
:loading="$apollo.loading"
@click="showMoreContributions" />
</div>
</template> </template>
<script> <script>
import gql from 'graphql-tag' import gql from 'graphql-tag'
import HcPostCard from '~/components/PostCard.vue' import HcPostCard from '~/components/PostCard.vue'
import HcLoadMore from '~/components/LoadMore.vue'
export default { export default {
components: { components: {
HcPostCard HcPostCard,
HcLoadMore
}, },
data() { data() {
return { return {
// Initialize your apollo data // Initialize your apollo data
Post: [] Post: [],
page: 1,
pageSize: 10
} }
}, },
computed: { computed: {
tags() { tags() {
return this.Post ? this.Post[0].tags.map(tag => tag.name) : '-' return this.Post ? this.Post[0].tags.map(tag => tag.name) : '-'
},
offset() {
return (this.page - 1) * this.pageSize
} }
}, },
methods: { methods: {
@ -36,13 +49,31 @@ export default {
name: 'post-slug', name: 'post-slug',
params: { slug: post.slug } params: { slug: post.slug }
}).href }).href
},
showMoreContributions() {
// this.page++
// Fetch more data and transform the original result
this.page++
this.$apollo.queries.Post.fetchMore({
variables: {
first: this.pageSize,
offset: this.offset
},
// Transform the previous result with new data
updateQuery: (previousResult, { fetchMoreResult }) => {
let output = { Post: this.Post }
output.Post = [...previousResult.Post, ...fetchMoreResult.Post]
return output
},
fetchPolicy: 'cache-and-network'
})
} }
}, },
apollo: { apollo: {
Post: { Post: {
query: gql(` query: gql(`
query { query Post($first: Int, $offset: Int) {
Post(first:10) { Post(first: $first, offset: $offset) {
id id
title title
contentExcerpt contentExcerpt
@ -74,7 +105,13 @@ export default {
shoutedCount shoutedCount
} }
} }
`) `),
variables() {
return {
first: this.pageSize,
offset: 0
}
}
} }
} }
} }

View File

@ -187,18 +187,10 @@
:show-author-popover="false" /> :show-author-popover="false" />
</ds-flex-item> </ds-flex-item>
</ds-flex> </ds-flex>
<ds-space <hc-load-more
v-if="hasMore" v-if="hasMore"
margin-top="large" :loading="$apollo.loading"
style="text-align: center"> @click="showMoreContributions" />
<ds-button
:icon="$apollo.loading ? 'spinner' : 'arrow-down'"
:disabled="$apollo.loading"
ghost
@click="showMoreContributions">
mehr laden
</ds-button>
</ds-space>
</ds-flex-item> </ds-flex-item>
</ds-flex> </ds-flex>
</div> </div>
@ -212,6 +204,7 @@ import HcPostCard from '~/components/PostCard.vue'
import HcFollowButton from '~/components/FollowButton.vue' import HcFollowButton from '~/components/FollowButton.vue'
import HcCountTo from '~/components/CountTo.vue' import HcCountTo from '~/components/CountTo.vue'
import HcBadges from '~/components/Badges.vue' import HcBadges from '~/components/Badges.vue'
import HcLoadMore from '~/components/LoadMore.vue'
export default { export default {
components: { components: {
@ -219,7 +212,8 @@ export default {
HcPostCard, HcPostCard,
HcFollowButton, HcFollowButton,
HcCountTo, HcCountTo,
HcBadges HcBadges,
HcLoadMore
}, },
transition: { transition: {
name: 'slide-up', name: 'slide-up',