| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- import api from '../../utils/api'
- import { sortTypes } from '../../utils/helpers'
-
- const state = {
- all: [],
- loaded: false,
- singlePost: null,
- }
-
- const getters = {
- allPosts: state => state.all,
- allPostsLoaded: state => state.loaded,
- }
-
- const _arrangeByType = postsList => {
- const byType = {}
- postsList.forEach(post => {
- const subtypes = post.categories
- subtypes.forEach(type => {
- if (!byType[type]) byType[type] = []
- byType[type].push(post)
- })
- })
- const flatPacked = []
- Object.keys(byType).forEach(type => {
- flatPacked.push({ slug: type, title: type, inbetween: true })
- byType[type].forEach(post => flatPacked.push(post))
- })
- return flatPacked
- }
-
- const actions = {
- async getAllPosts({ commit }, { sortType, params }) {
- commit('CLEAR_POSTS')
- commit('POSTS_LOADED', false)
- const storeFetch = posts => {
- let repacked = posts
- if (sortType == sortTypes.subtype) {
- repacked = _arrangeByType(posts)
- }
- commit('STORE_FETCHED_POSTS', { posts: repacked })
- commit('POSTS_LOADED', true)
- }
- return await api.getByType({
- type: 'post',
- sort: sortType,
- params,
- cb: storeFetch,
- })
- },
- getMorePosts({ commit }, { sortType, params }) {
- const storeFetch = posts => {
- commit('ADD_TO_FETCHED_POSTS', { posts })
- commit('POSTS_LOADED', true)
- }
- return api.getByType({
- type: 'post',
- sort: sortType,
- params,
- cb: storeFetch,
- })
- },
- getSinglePost({ commit }, id) {
- commit('CLEAR_SINGLE_POSTS')
- commit('POSTS_LOADED', false)
- return api.getSingleType('post', id, post => {
- commit('STORE_FETCHED_SINGLE_POST', post)
- commit('POSTS_LOADED', true)
- })
- },
- }
-
- const mutations = {
- ADD_TO_FETCHED_POSTS(state, { posts }) {
- state.all = [...state.all, ...posts]
- },
- STORE_FETCHED_POSTS(state, { posts }) {
- state.all = posts
- },
- STORE_FETCHED_SINGLE_POST(state, post) {
- state.singlePost = post
- },
- CLEAR_POSTS(state) {
- state.all = []
- },
- CLEAR_SINGLE_POSTS(state) {
- state.singlePost = null
- },
- POSTS_LOADED(state, val) {
- state.loaded = val
- },
- }
-
- export default { state, getters, actions, mutations }
|