search.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // import { REST } from 'vuejs/api/rest-axios'
  2. // import { JSONAPI } from 'vuejs/api/json-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. keys: '',
  10. term: '',
  11. uuids: [],
  12. items: [],
  13. offset: 0,
  14. limit: 15,
  15. infos: null,
  16. count: 0,
  17. noresults: false,
  18. // infinteState will come from vue-infinite-loading plugin
  19. // implemented in vuejs/components/Content/Base.vue
  20. infiniteLoadingState: null
  21. },
  22. // getters
  23. getters: {},
  24. // mutations
  25. mutations: {
  26. setUuids (state, uuids) {
  27. state.uuids = state.uuids.concat(uuids)
  28. },
  29. resetUuids (state) {
  30. state.uuids = []
  31. },
  32. setMaterials (state, items) {
  33. state.items = state.items.concat(items)
  34. },
  35. resetItems (state) {
  36. state.items = []
  37. },
  38. setKeys (state, keys) {
  39. state.keys = keys
  40. },
  41. setTerm (state, term) {
  42. state.term = term
  43. },
  44. setInfos (state, infos) {
  45. state.infos = infos
  46. },
  47. setCount (state, count) {
  48. state.count = count
  49. },
  50. resetCount (state, count) {
  51. state.count = false
  52. },
  53. setNoresults (state) {
  54. state.noresults = true
  55. },
  56. resetNoresults (state) {
  57. state.noresults = false
  58. },
  59. resetOffset (state) {
  60. state.offset = 0
  61. },
  62. incrementOffset (state) {
  63. state.offset += state.limit
  64. },
  65. setInfiniteState (state, infiniteLoadingstate) {
  66. state.infiniteLoadingState = infiniteLoadingstate
  67. }
  68. },
  69. // actions
  70. actions: {
  71. newSearch ({ dispatch, commit, state }) {
  72. console.log('Search newSearch')
  73. commit('resetUuids')
  74. commit('resetItems')
  75. commit('resetCount')
  76. commit('resetNoresults')
  77. commit('resetOffset')
  78. this.commit('Common/setPagetitle', state.keys)
  79. dispatch('getResults')
  80. },
  81. nextPage ({ dispatch, commit, state }, $infiniteLoadingstate) {
  82. console.log('Search nextPage', $infiniteLoadingstate)
  83. commit('incrementOffset')
  84. commit('setInfiniteState', $infiniteLoadingstate)
  85. dispatch('getResults')
  86. },
  87. getResults ({ dispatch, commit, state }) {
  88. const params = {
  89. keys: state.keys,
  90. term: state.term,
  91. offset: state.offset,
  92. limit: state.limit
  93. }
  94. // console.log('Search getResults params', params);
  95. const q = qs.stringify(params)
  96. return MA.get('/materio_sapi/getresults?' + q)
  97. .then(({ data }) => {
  98. console.log('search MA getresults data', data)
  99. // commit('setItems', data.items)
  100. commit('setInfos', data.infos)
  101. commit('setCount', data.count)
  102. if (data.count) {
  103. commit('setUuids', data.uuids)
  104. // loadMaterials is on mixins
  105. // https://github.com/huybuidac/vuex-extensions
  106. dispatch('loadMaterials', {
  107. uuids: data.uuids,
  108. imgStyle: ['card_medium', 'card_full'],
  109. callBack: 'loadMaterialsCallBack'
  110. })
  111. } else {
  112. commit('setNoresults')
  113. }
  114. })
  115. .catch((error) => {
  116. console.warn('Issue with getResults', error)
  117. Promise.reject(error)
  118. })
  119. },
  120. loadMaterialsCallBack ({ commit, state }, { items, callBackArgs }) {
  121. console.log('search loadMaterialsCallBack', items)
  122. commit('setMaterials', items)
  123. if (state.infiniteLoadingState) {
  124. if (state.offset + state.limit > state.count) {
  125. console.log('Search infinite completed')
  126. // tell to vue-infinite-loading plugin that there si no new page
  127. state.infiniteLoadingState.complete()
  128. } else {
  129. console.log('Search infinite loaded')
  130. // tell to vue-infinite-loading plugin that newpage is loaded
  131. state.infiniteLoadingState.loaded()
  132. }
  133. }
  134. }
  135. }
  136. }