search.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. term: '',
  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. setTerm (state, term) {
  62. state.term = term
  63. },
  64. setFilters (state, filters) {
  65. console.log('store search setFilters', filters)
  66. state.filters = typeof filters === 'string' ? filters.split(',') : filters
  67. },
  68. setInfos (state, infos) {
  69. state.infos = infos
  70. },
  71. setCount (state, count) {
  72. state.count = count
  73. },
  74. resetCount (state, count) {
  75. state.count = false
  76. },
  77. setNoresults (state) {
  78. state.noresults = true
  79. },
  80. resetNoresults (state) {
  81. state.noresults = false
  82. },
  83. resetOffset (state) {
  84. state.offset = 0
  85. },
  86. resetInfos (state) {
  87. state.infos = false
  88. },
  89. incrementOffset (state) {
  90. state.offset += state.limit
  91. },
  92. setInfiniteState (state, infiniteLoadingstate) {
  93. state.infiniteLoadingState = infiniteLoadingstate
  94. }
  95. },
  96. // actions
  97. actions: {
  98. newSearch ({ dispatch, commit, state }) {
  99. console.log('Search newSearch')
  100. commit('resetUuids')
  101. commit('resetItems')
  102. commit('resetCount')
  103. commit('resetNoresults')
  104. commit('resetOffset')
  105. commit('resetInfos')
  106. if (state.keys || state.term) {
  107. this.commit('Common/setPagetitle', state.keys)
  108. } else {
  109. this.commit('Common/setPagetitle', 'Base')
  110. }
  111. dispatch('getResults')
  112. },
  113. nextPage ({ dispatch, commit, state }, $infiniteLoadingstate) {
  114. console.log('Search nextPage', $infiniteLoadingstate)
  115. commit('incrementOffset')
  116. commit('setInfiniteState', $infiniteLoadingstate)
  117. dispatch('getResults')
  118. },
  119. getResults ({ dispatch, commit, state }) {
  120. const params = {
  121. keys: state.keys,
  122. term: state.term,
  123. offset: state.offset,
  124. limit: state.limit
  125. }
  126. if (state.filters) {
  127. console.log('getResults filters', state.filters)
  128. params.filters = state.filters.join(',')
  129. }
  130. // console.log('Search getResults params', params)
  131. const q = qs.stringify(params)
  132. return MA.get('/materio_sapi/getresults?' + q)
  133. .then(({ data }) => {
  134. console.log('search MA getresults data', data)
  135. // commit('setItems', data.items)
  136. commit('setInfos', data.infos)
  137. commit('setCount', data.count)
  138. if (data.count) {
  139. commit('setUuids', data.uuids)
  140. // loadMaterials is on mixins
  141. // https://github.com/huybuidac/vuex-extensions
  142. // dispatch('loadMaterialsGQL', {
  143. // ids: data.nids,
  144. // gqlfragment: materiauGQL,
  145. // callBack: 'loadSearchResultsCallBack'
  146. // })
  147. const ast = gql`{
  148. searchresults(ids: [${data.nids}], lang: "${drupalDecoupled.lang_code}") {
  149. ...SearchResultFields
  150. }
  151. }
  152. ${searchresultGQL}
  153. `
  154. MGQ.post('', { query: print(ast) })
  155. .then(resp => {
  156. console.log('loadSearchResultsGQL resp', resp)
  157. dispatch('loadSearchResultsCallBack', { items: resp.data.data.searchresults })
  158. })
  159. .catch(error => {
  160. console.warn('Issue with loadSearchResults', error)
  161. Promise.reject(error)
  162. })
  163. } else {
  164. commit('setNoresults')
  165. }
  166. })
  167. .catch((error) => {
  168. console.warn('Issue with getResults', error)
  169. // window.location.reload()
  170. Promise.reject(error)
  171. })
  172. },
  173. loadSearchResultsCallBack ({ commit, state }, { items, callBackArgs }) {
  174. console.log('search loadSearchResultsCallBack', items)
  175. commit('setResults', items)
  176. if (state.infiniteLoadingState) {
  177. if (state.offset + state.limit > state.count) {
  178. console.log('Search infinite completed')
  179. // tell to vue-infinite-loading plugin that there si no new page
  180. state.infiniteLoadingState.complete()
  181. } else {
  182. console.log('Search infinite loaded')
  183. // tell to vue-infinite-loading plugin that newpage is loaded
  184. state.infiniteLoadingState.loaded()
  185. }
  186. }
  187. },
  188. refreshItem ({ commit, state }, { id }) {
  189. console.log('Search refreshItem: id', id)
  190. const ast = gql`{
  191. searchresult(id: ${id}, lang: "${drupalDecoupled.lang_code}") {
  192. ...SearchResultFields
  193. }
  194. }
  195. ${searchresultGQL}
  196. `
  197. MGQ.post('', { query: print(ast) })
  198. .then(resp => {
  199. console.log('refreshItem searchresult resp', resp)
  200. // dispatch("loadSearchResultsCallBack", { items: resp.data.data.searchresults})
  201. commit('updateItem', resp.data.data.searchresult)
  202. })
  203. .catch(error => {
  204. console.warn('Issue with refreshItem', error)
  205. Promise.reject(error)
  206. })
  207. }
  208. }
  209. }