search.js 6.0 KB

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