NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import api from '../../utils/api'
  2. import { repackBySort } from './arrangements'
  3. const state = {
  4. all: [],
  5. loaded: false,
  6. singleShort: null,
  7. }
  8. const getters = {
  9. allShorts: state => state.all,
  10. allShortsBySlug: state =>
  11. Object.values(state.all).reduce((bySlug, short) => {
  12. bySlug[short.slug] = short
  13. return bySlug
  14. }, {}),
  15. allShortsLoaded: state => state.loaded,
  16. }
  17. const actions = {
  18. getAllShorts({ commit }, { sortType, params }) {
  19. commit('CLEAR_SHORTS')
  20. commit('SHORTS_LOADED', false)
  21. const storeFetch = (shorts => {
  22. let repacked = repackBySort(shorts, sortType)
  23. commit('STORE_FETCHED_SHORTS', { shorts: repacked })
  24. commit('SHORTS_LOADED', true)
  25. })
  26. return api.getByType({ type: 'short', sort: sortType, params, cb: storeFetch })
  27. },
  28. getMoreShorts({ commit }, { sortType, params }) {
  29. const storeFetch = (shorts => {
  30. let repacked = shorts
  31. commit('ADD_TO_FETCHED_SHORTS', { shorts: repacked })
  32. commit('SHORTS_LOADED', true)
  33. })
  34. return api.getByType({ type: 'short', sort: sortType, params, cb: storeFetch })
  35. },
  36. getSingleShort({ commit }, id) {
  37. commit('CLEAR_SINGLE_SHORTS')
  38. commit('SHORTS_LOADED', false)
  39. api.getSingleType('short', id, short => {
  40. commit('STORE_FETCHED_SINGLE_SHORT', short)
  41. commit('SHORTS_LOADED', true)
  42. })
  43. },
  44. }
  45. const mutations = {
  46. ADD_TO_FETCHED_SHORTS(state, { shorts }) {
  47. state.all = [...state.all, ...shorts]
  48. },
  49. STORE_FETCHED_SHORTS(state, { shorts }) {
  50. state.all = shorts
  51. },
  52. STORE_FETCHED_SINGLE_SHORT(state, short) {
  53. state.singleShort = short
  54. },
  55. CLEAR_SHORTS(state) {
  56. state.all = []
  57. },
  58. CLEAR_SINGLE_SHORTS(state) {
  59. state.singleShort = null
  60. },
  61. SHORTS_LOADED(state, val) {
  62. state.loaded = val
  63. },
  64. }
  65. export default { state, getters, actions, mutations }