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.

object.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import api from '../../utils/api'
  2. import { allBySlug, repackBySort } from './arrangements'
  3. const state = {
  4. all: [],
  5. loaded: false,
  6. singleObject: null,
  7. }
  8. const getters = {
  9. allObjects: state => state.all,
  10. allObjectsBySlug: state => allBySlug(state),
  11. allObjectsLoaded: state => state.loaded,
  12. }
  13. const actions = {
  14. getAllObjects({ commit }, { sortType, params }) {
  15. commit('CLEAR_OBJECTS')
  16. commit('OBJECTS_LOADED', false)
  17. const storeFetch = objects => {
  18. let repacked = repackBySort(objects, sortType)
  19. commit('STORE_FETCHED_OBJECTS', { objects: repacked })
  20. commit('OBJECTS_LOADED', true)
  21. }
  22. return api.getByType({
  23. type: 'object',
  24. sort: sortType,
  25. params,
  26. cb: storeFetch,
  27. })
  28. },
  29. getMoreObjects({ commit }, { sortType, params }) {
  30. const storeFetch = objects => {
  31. let repacked = objects
  32. commit('ADD_TO_FETCHED_OBJECTS', { objects: repacked })
  33. commit('OBJECTS_LOADED', true)
  34. }
  35. return api.getByType({
  36. type: 'object',
  37. sort: sortType,
  38. params,
  39. cb: storeFetch,
  40. })
  41. },
  42. getSingleObject({ commit }, id) {
  43. commit('CLEAR_SINGLE_OBJECTS')
  44. commit('OBJECTS_LOADED', false)
  45. return api.getSingleType('object', id, object => {
  46. commit('STORE_FETCHED_SINGLE_OBJECT', object)
  47. commit('OBJECTS_LOADED', true)
  48. })
  49. },
  50. }
  51. const mutations = {
  52. ADD_TO_FETCHED_OBJECTS(state, { objects }) {
  53. state.all = [...state.all, ...objects]
  54. },
  55. STORE_FETCHED_OBJECTS(state, { objects }) {
  56. state.all = objects
  57. },
  58. STORE_FETCHED_SINGLE_OBJECT(state, object) {
  59. state.singleObject = object
  60. },
  61. CLEAR_OBJECTS(state) {
  62. state.all = []
  63. },
  64. CLEAR_SINGLE_OBJECTS(state) {
  65. state.singleObject = null
  66. },
  67. OBJECTS_LOADED(state, val) {
  68. state.loaded = val
  69. },
  70. }
  71. export default { state, getters, actions, mutations }