search.js 7.1 KB

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