search.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { JSONAPI } from 'vuejs/api/json-axios'
  2. import { MA } from 'vuejs/api/ma-axios'
  3. import qs from 'querystring'
  4. export default {
  5. namespaced: true,
  6. // initial state
  7. state : {
  8. keys: "",
  9. term: "",
  10. uuids: [],
  11. items: [],
  12. offset: 0,
  13. limit: 15,
  14. infos: null,
  15. count: 0,
  16. // infinteState will come from vue-infinite-loading plugin
  17. // implemented in vuejs/components/Content/Base.vue
  18. infiniteLoadingState: null
  19. },
  20. // getters
  21. getters : {},
  22. // mutations
  23. mutations : {
  24. setUuids (state, uuids) {
  25. state.uuids = state.uuids.concat(uuids)
  26. },
  27. resetUuids (state) {
  28. state.uuids = []
  29. },
  30. setItems (state, items) {
  31. state.items = state.items.concat(items)
  32. },
  33. resetItems (state) {
  34. state.items = []
  35. },
  36. setKeys (state, keys) {
  37. state.keys = keys
  38. },
  39. setTerm (state, term) {
  40. state.term = term
  41. },
  42. setInfos (state, infos) {
  43. state.infos = infos
  44. },
  45. setCount (state, count) {
  46. state.count = count
  47. },
  48. resetOffset(state) {
  49. state.offset = 0
  50. },
  51. incrementOffset(state) {
  52. state.offset += state.limit
  53. },
  54. setInfiniteState(state, infiniteLoadingstate){
  55. state.infiniteLoadingState = infiniteLoadingstate
  56. }
  57. },
  58. // actions
  59. actions : {
  60. newSearch({ dispatch, commit, state }) {
  61. console.log('Search newSearch');
  62. commit('resetUuids')
  63. commit('resetItems')
  64. commit('resetOffset')
  65. this.commit('Common/setPagetitle', state.keys)
  66. dispatch('getResults')
  67. },
  68. nextPage ({ dispatch, commit, state }, $infiniteLoadingstate) {
  69. console.log("Search nextPage", $infiniteLoadingstate);
  70. commit('incrementOffset')
  71. commit('setInfiniteState', $infiniteLoadingstate)
  72. dispatch('getResults')
  73. },
  74. getResults ({ dispatch, commit, state }) {
  75. let params = {
  76. keys: state.keys,
  77. term: state.term,
  78. offset:state.offset,
  79. limit: state.limit
  80. }
  81. // console.log('Search getResults params', params);
  82. let q = qs.stringify(params)
  83. return MA.get(`/materio_sapi/getresults?`+q)
  84. .then(({ data }) => {
  85. console.log('search MA getresults data', data)
  86. // commit('setItems', data.items)
  87. commit('setInfos', data.infos)
  88. commit('setCount', data.count)
  89. commit('setUuids', data.uuids)
  90. dispatch('getItems', data.uuids)
  91. })
  92. .catch(( error ) => {
  93. console.warn('Issue with getResults', error)
  94. Promise.reject(error)
  95. })
  96. },
  97. getItems({ dispatch, commit, state }, uuids) {
  98. let params = {
  99. // include: 'images', // no needs to include thanks to consumers_image_styles module
  100. 'filter[uuids-groupe][group][conjunction]': 'OR'
  101. };
  102. for (var i = 0; i < uuids.length; i++) {
  103. let uuid = uuids[i]
  104. params[`filter[${uuid}][condition][path]`] = 'id'
  105. params[`filter[${uuid}][condition][value]`] = uuid
  106. params[`filter[${uuid}][condition][operator]`] = '='
  107. params[`filter[${uuid}][condition][memberOf]`] = 'uuids-groupe'
  108. }
  109. // console.log('search JSONAPI params', params);
  110. let q = qs.stringify(params)
  111. return JSONAPI.get('node/materiau?'+q)
  112. .then(({ data }) => {
  113. console.log('search getItems data', data)
  114. dispatch('parseItems', {data:data.data, uuids:uuids})
  115. // commit('setItems', data.items)
  116. })
  117. .catch(( error ) => {
  118. console.warn('Issue with getItems', error)
  119. Promise.reject(error)
  120. })
  121. },
  122. parseItems({ dispatch, commit, state }, { data, uuids }) {
  123. // data comes from jsonapi query
  124. // uuids comes from solr search query (we loop from uuids to conserve the search results order)
  125. console.log('search parseItems data, uuids', data, uuids)
  126. let items = []
  127. // for (var i = 0; i < data.length; i++) {
  128. for (var i = 0; i < uuids.length; i++) {
  129. let uuid = uuids[i]
  130. // https://stackoverflow.com/questions/11258077/how-to-find-index-of-an-object-by-key-and-value-in-an-javascript-array
  131. let item_index = data.findIndex(p => p.id == uuid)
  132. let item_src = data[item_index]
  133. let attrs = item_src.attributes
  134. let relations = item_src.relationships
  135. // get field values
  136. let item = {
  137. uuid: uuid,
  138. title: attrs.title,
  139. description: attrs.field_short_description,
  140. body: attrs.body,
  141. reference: attrs.field_reference,
  142. }
  143. // get images included values
  144. let img_src = relations.images.data
  145. item.images = []
  146. for (var j = 0; j < img_src.length; j++) {
  147. item.images.push({
  148. title: img_src[j].meta.title,
  149. // meta.imageDerivatives.style.href link is provided by drupal consumers_image_styles module
  150. url: img_src[j].meta.imageDerivatives.links.card_medium.href
  151. })
  152. }
  153. items.push(item)
  154. }
  155. console.log('items', items)
  156. commit('setItems', items)
  157. if(state.infiniteLoadingState){
  158. if (state.offset + state.limit > state.count) {
  159. console.log('Search infinite completed');
  160. // tell to vue-infinite-loading plugin that there si no new page
  161. state.infiniteLoadingState.complete()
  162. }else{
  163. console.log('Search infinite loaded');
  164. // tell to vue-infinite-loading plugin that newpage is loaded
  165. state.infiniteLoadingState.loaded()
  166. }
  167. }
  168. }
  169. }
  170. }