about summary refs log tree commit diff
path: root/components
diff options
context:
space:
mode:
Diffstat (limited to 'components')
-rw-r--r--components/Posts/PostList/PostList.vue45
-rw-r--r--components/Posts/PostList/README.md42
-rw-r--r--components/Posts/PostList/index.js19
-rw-r--r--components/Posts/PostList/parentMixin.js9
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()
+    }
+  }
+}