blabla.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // import { JSONAPI } from 'vuejs/api/json-axios'
  2. import { REST } from 'vuejs/api/rest-axios'
  3. // import { MA } from 'vuejs/api/ma-axios'
  4. // import qs from 'querystring-es3'
  5. export default {
  6. namespaced: true,
  7. // initial state
  8. state: {
  9. contenttype: null,
  10. items: [],
  11. page: 0,
  12. // infinteState will come from vue-infinite-loading plugin
  13. // implemented in vuejs/components/Content/Base.vue
  14. infiniteLoadingState: null
  15. },
  16. // getters
  17. getters: {},
  18. // mutations
  19. mutations: {
  20. setItems (state, items) {
  21. state.items = state.items.concat(items)
  22. },
  23. incrementPage (state) {
  24. state.page += 1
  25. },
  26. setInfiniteState (state, infiniteLoadingstate) {
  27. state.infiniteLoadingState = infiniteLoadingstate
  28. }
  29. },
  30. // actions
  31. actions: {
  32. getItems ({ dispatch, commit, state }) {
  33. return REST.get(`/blabla_rest?_format=json&page=${state.page}`, {})
  34. .then(({ data }) => {
  35. console.log('blabla REST: data', data)
  36. if (data.length) {
  37. commit('setItems', data)
  38. // console.log('items.length', this.items.length)
  39. if (state.infiniteLoadingState) { state.infiniteLoadingState.loaded() }
  40. } else {
  41. if (state.infiniteLoadingState) { state.infiniteLoadingState.complete() }
  42. }
  43. })
  44. .catch((error) => {
  45. console.warn('Issue with blabla getitems', error)
  46. Promise.reject(error)
  47. })
  48. },
  49. nextPage ({ dispatch, commit, state }, $infiniteLoadingstate) {
  50. console.log('blabla nextPage', $infiniteLoadingstate)
  51. commit('incrementPage')
  52. commit('setInfiniteState', $infiniteLoadingstate)
  53. dispatch('getItems')
  54. },
  55. getItemIndex ({ dispatch, commit, state }, nid) {
  56. console.log('getItemIndex nid', nid)
  57. return state.items.findIndex((e) => {
  58. // console.log('findIndex', e.nid, nid)
  59. return parseInt(e.nid) === nid
  60. })
  61. },
  62. getPrevNextItems ({ dispatch, commit, state }, index) {
  63. // TODO: preload prev and next items
  64. return {
  65. prev: state.items[index - 1],
  66. next: state.items[index + 1]
  67. }
  68. }
  69. }
  70. }