search.js 4.6 KB

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