search.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. // console.log('setResults, items', items);
  38. if(items){ // avoid bug like items = [null]
  39. state.items = state.items.concat(items)
  40. }
  41. // console.log('setResults, state.items', state.items);
  42. },
  43. resetItems (state) {
  44. state.items = []
  45. },
  46. setKeys (state, keys) {
  47. state.keys = keys
  48. },
  49. setTerm (state, term) {
  50. state.term = term
  51. },
  52. setInfos (state, infos) {
  53. state.infos = infos
  54. },
  55. setCount (state, count) {
  56. state.count = count
  57. },
  58. resetCount (state, count) {
  59. state.count = false
  60. },
  61. setNoresults (state) {
  62. state.noresults = true
  63. },
  64. resetNoresults (state) {
  65. state.noresults = false
  66. },
  67. resetOffset (state) {
  68. state.offset = 0
  69. },
  70. incrementOffset (state) {
  71. state.offset += state.limit
  72. },
  73. setInfiniteState (state, infiniteLoadingstate) {
  74. state.infiniteLoadingState = infiniteLoadingstate
  75. }
  76. },
  77. // actions
  78. actions: {
  79. newSearch ({ dispatch, commit, state }) {
  80. console.log('Search newSearch')
  81. commit('resetUuids')
  82. commit('resetItems')
  83. commit('resetCount')
  84. commit('resetNoresults')
  85. commit('resetOffset')
  86. if(state.keys || state.term){
  87. this.commit('Common/setPagetitle', state.keys)
  88. }else{
  89. this.commit('Common/setPagetitle', 'Base')
  90. }
  91. dispatch('getResults')
  92. },
  93. nextPage ({ dispatch, commit, state }, $infiniteLoadingstate) {
  94. console.log('Search nextPage', $infiniteLoadingstate)
  95. commit('incrementOffset')
  96. commit('setInfiniteState', $infiniteLoadingstate)
  97. dispatch('getResults')
  98. },
  99. getResults ({ dispatch, commit, state }) {
  100. const params = {
  101. keys: state.keys,
  102. term: state.term,
  103. offset: state.offset,
  104. limit: state.limit
  105. }
  106. // console.log('Search getResults params', params);
  107. const q = qs.stringify(params)
  108. return MA.get('/materio_sapi/getresults?' + q)
  109. .then(({ data }) => {
  110. console.log('search MA getresults data', data)
  111. // commit('setItems', data.items)
  112. commit('setInfos', data.infos)
  113. commit('setCount', data.count)
  114. if (data.count) {
  115. commit('setUuids', data.uuids)
  116. // loadMaterials is on mixins
  117. // https://github.com/huybuidac/vuex-extensions
  118. // dispatch('loadMaterialsGQL', {
  119. // ids: data.nids,
  120. // gqlfragment: materiauGQL,
  121. // callBack: 'loadSearchResultsCallBack'
  122. // })
  123. let ast = gql`{
  124. searchresults(ids: [${data.nids}], lang: "${drupalDecoupled.lang_code}") {
  125. ...SearchResultFields
  126. }
  127. }
  128. ${ searchresultGQL }
  129. `
  130. MGQ.post('', { query: print(ast) })
  131. .then(resp => {
  132. console.log('loadSearchResultsGQL resp', resp )
  133. dispatch("loadSearchResultsCallBack", { items: resp.data.data.searchresults})
  134. })
  135. .catch(error => {
  136. console.warn('Issue with loadSearchResults', error)
  137. Promise.reject(error)
  138. })
  139. } else {
  140. commit('setNoresults')
  141. }
  142. })
  143. .catch((error) => {
  144. console.warn('Issue with getResults', error)
  145. Promise.reject(error)
  146. })
  147. },
  148. loadSearchResultsCallBack ({ commit, state }, { items, callBackArgs }) {
  149. console.log('search loadSearchResultsCallBack', items)
  150. commit('setResults', items)
  151. if (state.infiniteLoadingState) {
  152. if (state.offset + state.limit > state.count) {
  153. console.log('Search infinite completed')
  154. // tell to vue-infinite-loading plugin that there si no new page
  155. state.infiniteLoadingState.complete()
  156. } else {
  157. console.log('Search infinite loaded')
  158. // tell to vue-infinite-loading plugin that newpage is loaded
  159. state.infiniteLoadingState.loaded()
  160. }
  161. }
  162. }
  163. }
  164. }