diff options
author | Patryk Niedźwiedziński <pniedzwiedzinski19@gmail.com> | 2019-12-05 17:34:44 +0100 |
---|---|---|
committer | Patryk Niedźwiedziński <pniedzwiedzinski19@gmail.com> | 2019-12-05 17:34:44 +0100 |
commit | 29a4e2488151861c10abeb0a05622e4d06e07c60 (patch) | |
tree | c237820ce1f7588d44820f64163bc0caaa693d7f /components/Posts | |
parent | 6a532274bf7d69cefe9e640274f30c3fdcc17c92 (diff) | |
download | puszcza-29a4e2488151861c10abeb0a05622e4d06e07c60.tar.gz puszcza-29a4e2488151861c10abeb0a05622e4d06e07c60.zip |
Fix SSR on PostList
Diffstat (limited to 'components/Posts')
-rw-r--r-- | components/Posts/PostList/PostList.vue | 45 | ||||
-rw-r--r-- | components/Posts/PostList/README.md | 42 | ||||
-rw-r--r-- | components/Posts/PostList/index.js | 19 | ||||
-rw-r--r-- | components/Posts/PostList/parentMixin.js | 9 |
4 files changed, 82 insertions, 33 deletions
diff --git a/components/Posts/PostList/PostList.vue b/components/Posts/PostList/PostList.vue index 781099f..80369a8 100644 --- a/components/Posts/PostList/PostList.vue +++ b/components/Posts/PostList/PostList.vue @@ -1,9 +1,8 @@ <template> - <pure-post-list v-if="rawPosts" :posts="posts" :loading="loading" /> + <pure-post-list v-if="posts" :posts="parsedPosts" :loading="loading" /> </template> <script> -import k from '~/api' import PurePostList from './PurePostList' export default { @@ -12,43 +11,23 @@ export default { props: { max: { type: Number, + required: false, + default: () => 4 + }, + posts: { + type: Array, required: false } }, - data() { - return { - loading: true, - rawPosts: undefined - } - }, - mounted() { - if (process.client && this.rawPosts === undefined) { - this.loadPostsClient() - } else { - this.rawPosts = k.getPosts() - this.loading = false - } - }, computed: { - posts() { + parsedPosts() { if (this.max) { - return this.rawPosts.slice(0, this.max) + return this.posts.slice(0, this.max) } - return this.rawPosts - } - }, - methods: { - async loadPostsClient() { - let posts = await this.$axios.get( - `${window.location.origin}/api/posts.json` - ) - this.rawPosts = posts.data - this.rawPosts = this.rawPosts.map(post => ({ - title: post.content.meta.title, - description: post.content.description, - route: post.route - })) - this.loading = false + return this.posts + }, + loading() { + return this.posts === undefined } } } diff --git a/components/Posts/PostList/README.md b/components/Posts/PostList/README.md new file mode 100644 index 0000000..839f9c2 --- /dev/null +++ b/components/Posts/PostList/README.md @@ -0,0 +1,42 @@ +## PostList + +This component creates a list of posts + +### Usage + +```html +<template> + <post-list :posts="posts"> +</template> + +<script> +import PostList, { getPosts } from '~/components/Posts/PostList'; + +export default { + components: { PostList }, + async asyncData() { + return { + posts: await getPosts() + } + } +} +</script> +``` + +Or by using the mixin + +```html +<template> + <post-list :posts="posts"> +</template> + +<script> +import PostList from '~/components/Posts/PostList'; +import postListParentMixin from '~/components/Posts/PostList/parentMixin'; + +export default { + components: { PostList }, + mixins: [postListParentMixin] +} +</script> +``` diff --git a/components/Posts/PostList/index.js b/components/Posts/PostList/index.js index dbb515e..11d44ff 100644 --- a/components/Posts/PostList/index.js +++ b/components/Posts/PostList/index.js @@ -1,3 +1,22 @@ +import axios from 'axios' + +import k from '~/api' import PostList from './PostList' +export const getPosts = async () => { + if (process.client) { + let posts = await axios.get(`${window.location.origin}/api/posts.json`) + return parsePosts(posts.data) + } else { + return parsePosts(k.getPosts()) + } +} + +export const parsePosts = posts => + posts.map(post => ({ + title: post.content.meta.title, + description: post.content.description, + route: post.route + })) + export default PostList diff --git a/components/Posts/PostList/parentMixin.js b/components/Posts/PostList/parentMixin.js new file mode 100644 index 0000000..e5b06ed --- /dev/null +++ b/components/Posts/PostList/parentMixin.js @@ -0,0 +1,9 @@ +import { getPosts } from './index' + +export default { + async asyncData() { + return { + posts: await getPosts() + } + } +} |