search.js 4.1 KB

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