search.js 6.3 KB

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