search.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. if(state.keys || state.term){
  83. this.commit('Common/setPagetitle', state.keys)
  84. }else{
  85. this.commit('Common/setPagetitle', 'Base')
  86. }
  87. dispatch('getResults')
  88. },
  89. nextPage ({ dispatch, commit, state }, $infiniteLoadingstate) {
  90. console.log('Search nextPage', $infiniteLoadingstate)
  91. commit('incrementOffset')
  92. commit('setInfiniteState', $infiniteLoadingstate)
  93. dispatch('getResults')
  94. },
  95. getResults ({ dispatch, commit, state }) {
  96. const params = {
  97. keys: state.keys,
  98. term: state.term,
  99. offset: state.offset,
  100. limit: state.limit
  101. }
  102. // console.log('Search getResults params', params);
  103. const q = qs.stringify(params)
  104. return MA.get('/materio_sapi/getresults?' + q)
  105. .then(({ data }) => {
  106. console.log('search MA getresults data', data)
  107. // commit('setItems', data.items)
  108. commit('setInfos', data.infos)
  109. commit('setCount', data.count)
  110. if (data.count) {
  111. commit('setUuids', data.uuids)
  112. // loadMaterials is on mixins
  113. // https://github.com/huybuidac/vuex-extensions
  114. // dispatch('loadMaterialsGQL', {
  115. // ids: data.nids,
  116. // gqlfragment: materiauGQL,
  117. // callBack: 'loadSearchResultsCallBack'
  118. // })
  119. let ast = gql`{
  120. searchresults(ids: [${data.nids}], lang: "${drupalDecoupled.lang_code}") {
  121. ...SearchResultFields
  122. }
  123. }
  124. ${ searchresultGQL }
  125. `
  126. MGQ.post('', { query: print(ast) })
  127. .then(( resp ) => {
  128. console.log('loadSearchResultsGQL resp', resp )
  129. dispatch("loadSearchResultsCallBack", { items: resp.data.data.searchresults})
  130. })
  131. .catch(error => {
  132. console.warn('Issue with loadSearchResults', error)
  133. Promise.reject(error)
  134. })
  135. } else {
  136. commit('setNoresults')
  137. }
  138. })
  139. .catch((error) => {
  140. console.warn('Issue with getResults', error)
  141. Promise.reject(error)
  142. })
  143. },
  144. loadSearchResultsCallBack ({ commit, state }, { items, callBackArgs }) {
  145. console.log('search loadSearchResultsCallBack', items)
  146. commit('setResults', items)
  147. if (state.infiniteLoadingState) {
  148. if (state.offset + state.limit > state.count) {
  149. console.log('Search infinite completed')
  150. // tell to vue-infinite-loading plugin that there si no new page
  151. state.infiniteLoadingState.complete()
  152. } else {
  153. console.log('Search infinite loaded')
  154. // tell to vue-infinite-loading plugin that newpage is loaded
  155. state.infiniteLoadingState.loaded()
  156. }
  157. }
  158. }
  159. }
  160. }