search.js 6.0 KB

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