search.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 Vue from 'vue'
  6. import MGQ from 'vuejs/api/graphql-axios'
  7. import { print } from 'graphql/language/printer'
  8. import gql from 'graphql-tag'
  9. import searchresultGQL from 'vuejs/api/gql/searchresults.fragment.gql'
  10. export default {
  11. namespaced: true,
  12. // initial state
  13. state: {
  14. keys: '',
  15. terms: [],
  16. filters: [],
  17. uuids: [],
  18. items: [],
  19. offset: 0,
  20. limit: 15,
  21. infos: null,
  22. count: 0,
  23. noresults: false,
  24. // infinteState will come from vue-infinite-loading plugin
  25. // implemented in vuejs/components/Content/Base.vue
  26. infiniteLoadingState: null
  27. },
  28. // getters
  29. getters: {},
  30. // mutations
  31. mutations: {
  32. setUuids (state, uuids) {
  33. state.uuids = state.uuids.concat(uuids)
  34. },
  35. resetUuids (state) {
  36. state.uuids = []
  37. },
  38. setResults (state, items) {
  39. // console.log('setResults, items', items)
  40. if (items) { // avoid bug like items = [null]
  41. state.items = state.items.concat(items)
  42. }
  43. // console.log('setResults, state.items', state.items)
  44. },
  45. updateItem (state, item) {
  46. // state.items[data.index] = data.item // this is not triggering vuejs refresh
  47. // find the right index
  48. const index = state.items.findIndex(i => i.id === item.id)
  49. // update the array
  50. if (index !== -1) {
  51. Vue.set(state.items, index, item)
  52. }
  53. // console.log("mutation updateItem state.items", state.items)
  54. },
  55. resetItems (state) {
  56. state.items = []
  57. },
  58. setKeys (state, keys) {
  59. state.keys = keys
  60. },
  61. setTerms (state, terms) {
  62. state.terms = terms
  63. console.log('search store setTerms', terms)
  64. },
  65. setFilters (state, filters) {
  66. console.log('store search setFilters', filters)
  67. state.filters = typeof filters === 'string' ? filters.split(',') : filters
  68. },
  69. setInfos (state, infos) {
  70. state.infos = infos
  71. },
  72. setCount (state, count) {
  73. state.count = count
  74. },
  75. resetCount (state, count) {
  76. state.count = false
  77. },
  78. setNoresults (state) {
  79. state.noresults = true
  80. },
  81. resetNoresults (state) {
  82. state.noresults = false
  83. },
  84. resetOffset (state) {
  85. state.offset = 0
  86. },
  87. resetInfos (state) {
  88. state.infos = false
  89. },
  90. incrementOffset (state) {
  91. state.offset += state.limit
  92. },
  93. setInfiniteState (state, infiniteLoadingstate) {
  94. state.infiniteLoadingState = infiniteLoadingstate
  95. }
  96. },
  97. // actions
  98. actions: {
  99. newSearch ({ dispatch, commit, state }) {
  100. console.log('Search newSearch, state.keys', state.keys)
  101. commit('resetUuids')
  102. commit('resetItems')
  103. commit('resetCount')
  104. commit('resetNoresults')
  105. commit('resetOffset')
  106. commit('resetInfos')
  107. if (state.keys || state.terms.length) {
  108. this.commit('Common/setPagetitle', state.keys.join(', '))
  109. } else {
  110. this.commit('Common/setPagetitle', 'Base')
  111. }
  112. dispatch('getResults')
  113. },
  114. nextPage ({ dispatch, commit, state }, $infiniteLoadingstate) {
  115. console.log('Search nextPage', $infiniteLoadingstate)
  116. commit('incrementOffset')
  117. commit('setInfiniteState', $infiniteLoadingstate)
  118. dispatch('getResults')
  119. },
  120. getResults ({ dispatch, commit, state }) {
  121. const params = {
  122. keys: state.keys,
  123. terms: JSON.stringify(state.terms),
  124. offset: state.offset,
  125. limit: state.limit
  126. }
  127. console.log('search store getResults, params', params)
  128. if (state.filters) {
  129. console.log('getResults filters', state.filters)
  130. params.filters = state.filters.join(',')
  131. }
  132. // console.log('Search getResults params', params)
  133. const q = qs.stringify(params)
  134. return MA.get('/materio_sapi/getresults?' + q)
  135. .then(({ data }) => {
  136. console.log('search MA getresults data', data, state.terms)
  137. // commit('setItems', data.items)
  138. commit('setInfos', data.infos)
  139. commit('setCount', data.count)
  140. if (data.count) {
  141. commit('setUuids', data.uuids)
  142. // loadMaterials is on mixins
  143. // https://github.com/huybuidac/vuex-extensions
  144. // dispatch('loadMaterialsGQL', {
  145. // ids: data.nids,
  146. // gqlfragment: materiauGQL,
  147. // callBack: 'loadSearchResultsCallBack'
  148. // })
  149. const ast = gql`{
  150. searchresults(ids: [${data.nids}], lang: "${drupalDecoupled.lang_code}") {
  151. ...SearchResultFields
  152. }
  153. }
  154. ${searchresultGQL}
  155. `
  156. MGQ.post('', { query: print(ast) })
  157. .then(resp => {
  158. console.log('loadSearchResultsGQL resp', resp)
  159. dispatch('loadSearchResultsCallBack', { items: resp.data.data.searchresults })
  160. })
  161. .catch(error => {
  162. console.warn('Issue with loadSearchResults', error)
  163. Promise.reject(error)
  164. })
  165. } else {
  166. commit('setNoresults')
  167. }
  168. })
  169. .catch((error) => {
  170. console.warn('Issue with getResults', error)
  171. // window.location.reload()
  172. Promise.reject(error)
  173. })
  174. },
  175. loadSearchResultsCallBack ({ commit, state }, { items, callBackArgs }) {
  176. console.log('search loadSearchResultsCallBack', items)
  177. commit('setResults', items)
  178. if (state.infiniteLoadingState) {
  179. if (state.offset + state.limit > state.count) {
  180. console.log('Search infinite completed')
  181. // tell to vue-infinite-loading plugin that there si no new page
  182. state.infiniteLoadingState.complete()
  183. } else {
  184. console.log('Search infinite loaded')
  185. // tell to vue-infinite-loading plugin that newpage is loaded
  186. state.infiniteLoadingState.loaded()
  187. }
  188. }
  189. },
  190. refreshItem ({ commit, state }, { id }) {
  191. console.log('Search refreshItem: id', id)
  192. const ast = gql`{
  193. searchresult(id: ${id}, lang: "${drupalDecoupled.lang_code}") {
  194. ...SearchResultFields
  195. }
  196. }
  197. ${searchresultGQL}
  198. `
  199. MGQ.post('', { query: print(ast) })
  200. .then(resp => {
  201. console.log('refreshItem searchresult resp', resp)
  202. // dispatch("loadSearchResultsCallBack", { items: resp.data.data.searchresults})
  203. commit('updateItem', resp.data.data.searchresult)
  204. })
  205. .catch(error => {
  206. console.warn('Issue with refreshItem', error)
  207. Promise.reject(error)
  208. })
  209. }
  210. }
  211. }