NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

page.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import api from '../../utils/api'
  2. // initial state
  3. const state = {
  4. all: {},
  5. loaded: false,
  6. page: null,
  7. }
  8. // getters
  9. const getters = {
  10. allPages: state => state.all,
  11. allPagesLoaded: state => state.loaded,
  12. page: state => id => {
  13. let field = typeof id === 'number' ? 'id' : 'slug'
  14. let page = state.all.filter(page => page[field] === id)
  15. return (page[0]) ? page[0] : false
  16. },
  17. pageContent: state => id => {
  18. let field = typeof id === 'number' ? 'id' : 'slug'
  19. let page = state.all.filter(page => page[field] === id)
  20. return (page[0]) ? page[0].content.rendered : false
  21. },
  22. somePages: state => limit => {
  23. if(state.all.length < 1) return false
  24. let all = [...state.all]
  25. return all.splice(0, Math.min(limit, state.all.length))
  26. },
  27. }
  28. // actions
  29. const actions = {
  30. getAllPages({ commit }, sortType) {
  31. api.getByType('pages', sortType, pages => {
  32. commit('STORE_FETCHED_PAGES', { pages })
  33. commit('PAGES_LOADED', true)
  34. })
  35. }
  36. }
  37. // mutations
  38. const mutations = {
  39. STORE_FETCHED_PAGES(state, { pages }) {
  40. state.all = pages
  41. },
  42. PAGES_LOADED(state, val) { state.loaded = val },
  43. }
  44. export default { state, getters, actions, mutations }