| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- 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)
- 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 }
|