NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import api from '../../utils/api'
  2. import { sortTypes } from '../../utils/helpers'
  3. const state = {
  4. all: [],
  5. loaded: false,
  6. singlePost: null,
  7. }
  8. const getters = {
  9. allPosts: state => state.all,
  10. allPostsLoaded: state => state.loaded,
  11. }
  12. const _arrangeByType = postsList => {
  13. const byType = {}
  14. postsList.forEach(post => {
  15. const subtypes = post.categories
  16. subtypes.forEach(type => {
  17. if(!byType[type]) byType[type] = []
  18. byType[type].push(post)
  19. })
  20. })
  21. const flatPacked = []
  22. Object.keys(byType).forEach(type => {
  23. flatPacked.push({ slug: type, title: type, inbetween: true })
  24. byType[type].forEach(post => flatPacked.push(post))
  25. })
  26. return flatPacked
  27. }
  28. const actions = {
  29. async getAllPosts({ commit }, { sortType, params }) {
  30. commit('CLEAR_POSTS')
  31. commit('POSTS_LOADED', false)
  32. const storeFetch = (posts => {
  33. let repacked = posts
  34. if(sortType == sortTypes.subtype) {
  35. repacked = _arrangeByType(posts)
  36. }
  37. commit('STORE_FETCHED_POSTS', { posts: repacked })
  38. commit('POSTS_LOADED', true)
  39. })
  40. return await api.getByType({ type: 'post', sort: sortType, params, cb: storeFetch })
  41. },
  42. getMorePosts({ commit }, { sortType, params }) {
  43. const storeFetch = (posts => {
  44. commit('ADD_TO_FETCHED_POSTS', { posts })
  45. commit('POSTS_LOADED', true)
  46. })
  47. return api.getByType({ type: 'post', sort: sortType, params, cb: storeFetch })
  48. },
  49. getSinglePost({ commit }, id) {
  50. commit('CLEAR_SINGLE_POSTS')
  51. commit('POSTS_LOADED', false)
  52. api.getSingleType('post', id, post => {
  53. commit('STORE_FETCHED_SINGLE_POST', post)
  54. commit('POSTS_LOADED', true)
  55. })
  56. },
  57. }
  58. const mutations = {
  59. ADD_TO_FETCHED_POSTS(state, { posts }) {
  60. state.all = [...state.all, ...posts]
  61. },
  62. STORE_FETCHED_POSTS(state, { posts }) {
  63. state.all = posts
  64. },
  65. STORE_FETCHED_SINGLE_POST(state, post) {
  66. state.singlePost = post
  67. },
  68. CLEAR_POSTS(state) {
  69. state.all = []
  70. },
  71. CLEAR_SINGLE_POSTS(state) {
  72. state.singlePost = null
  73. },
  74. POSTS_LOADED(state, val) {
  75. state.loaded = val
  76. },
  77. }
  78. export default { state, getters, actions, mutations }