浏览代码

파라메터 처리를 위한 전역 함수 추가

김보경 5 年之前
父节点
当前提交
5ff9b5e54d
共有 2 个文件被更改,包括 39 次插入0 次删除
  1. 1 0
      pages/blogs/index.vue
  2. 38 0
      store/blog.js

+ 1 - 0
pages/blogs/index.vue

@@ -59,6 +59,7 @@ export default {
   },
   async fetch({store}) {
     await store.dispatch('blog/fetchBlogs')
+    await store.dispatch('blog/fetchFeaturedBlogs', {'filter[featured]': true})
   }
 }
 </script>

+ 38 - 0
store/blog.js

@@ -1,3 +1,27 @@
+const applyParamsToUrl = (url, filter) => {
+  if (filter) {
+    let filteredEntities = ''
+    if (url.indexOf('?') === -1) {
+      url += '?'
+    } else {
+      url += '&'
+    }
+
+    Object.keys(filter).forEach(key => {
+      filteredEntities += `${key}=${filter[key]}&`
+    })
+
+    if (filteredEntities.slice(-1) === '&') {
+      filteredEntities = filteredEntities.slice(0, -1)
+    }
+
+    return url + filteredEntities
+  }
+
+  return url
+}
+
+
 export const state = () => ({
   item: {},
   items: {
@@ -17,6 +41,20 @@ export const actions = {
     commit('setBlogs', {resource: 'all', blogs})
     return state.items.all
   },
+
+  // /api/v1/blogs?filter[featured]=true
+  async fetchFeaturedBlogs({commit, state}, filter) {
+    const url = applyParamsToUrl('/api/v1/blogs', filter)
+    const data = await this.$axios.$get(url)
+    if(data.isAxiosError === true){
+      console.log(data.data)
+      return Error('')
+    }
+    const { blogs } = data
+    commit('setBlogs', {resource: 'featured', blogs})
+    return state.items.featured
+  },
+
   async fetchBlogBySlug({commit, state}, slug) {
     const blog = await this.$axios.$get(`/api/v1/blogs/s/${slug}`)
     if(blog.isAxiosError === true){