index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import { createStore } from 'vuex-extensions'
  4. // import { JSONAPI } from 'vuejs/api/json-axios'
  5. // import qs from 'querystring-es3'
  6. import { MGQ } from 'vuejs/api/graphql-axios'
  7. import { print } from 'graphql/language/printer'
  8. import gql from 'graphql-tag'
  9. // import materiauFields from 'vuejs/api/gql/materiau.fragment.gql'
  10. import Common from './modules/common'
  11. import User from './modules/user'
  12. import Search from './modules/search'
  13. import Blabla from './modules/blabla'
  14. import Showrooms from './modules/showrooms'
  15. import Pages from './modules/pages'
  16. // https://github.com/vuejs/vuex/tree/dev/examples/shopping-cart
  17. Vue.use(Vuex)
  18. // export default new Vuex.Store({
  19. export default createStore(Vuex.Store, {
  20. modules: {
  21. Common,
  22. User,
  23. Search,
  24. Blabla,
  25. Showrooms,
  26. Pages
  27. },
  28. // https://github.com/huybuidac/vuex-extensions
  29. mixins: {
  30. actions: {
  31. loadMaterialsGQL ({ dispatch }, { ids, gqlfragment, callBack, callBackArgs }) {
  32. console.log('loadMaterialsGQL ids', ids)
  33. let ast = gql`{
  34. materiaux(ids: [${ids}]) {
  35. ...MateriauFields
  36. }
  37. }
  38. ${ gqlfragment }
  39. `
  40. MGQ.post('', { query: print(ast) })
  41. .then(( resp ) => {
  42. console.log('loadMaterialsGQL resp', resp )
  43. // dispatch('parseMaterialsGQL', {
  44. // items: resp.data.data.materiaux,
  45. // callBack: callBack,
  46. // callBackArgs: callBackArgs
  47. // })
  48. dispatch(callBack, {
  49. items: resp.data.data.materiaux,
  50. callBackArgs: callBackArgs
  51. })
  52. })
  53. .catch(error => {
  54. console.warn('Issue with loadMaterials', error)
  55. Promise.reject(error)
  56. })
  57. }
  58. // parseMaterialsGQL ({ dispatch }, { items, callBack, callBackArgs }) {
  59. // dispatch(callBack, { items: items, callBackArgs: callBackArgs })
  60. // }
  61. // loadMaterials ({ dispatch }, { uuids, imgStyle, callBack, callBackArgs }) {
  62. // const params = {
  63. // // include: 'images', // no needs to include thanks to consumers_image_styles module
  64. // // include: 'drupal_internal__nid',
  65. // 'filter[uuids-groupe][group][conjunction]': 'OR'
  66. // }
  67. // for (let i = 0; i < uuids.length; i++) {
  68. // const uuid = uuids[i]
  69. // params[`filter[${uuid}][condition][path]`] = 'id'
  70. // params[`filter[${uuid}][condition][value]`] = uuid
  71. // params[`filter[${uuid}][condition][operator]`] = '='
  72. // params[`filter[${uuid}][condition][memberOf]`] = 'uuids-groupe'
  73. // }
  74. // // console.log('search JSONAPI params', params);
  75. // const q = qs.stringify(params)
  76. // return JSONAPI.get('node/materiau?' + q)
  77. // .then(({ data }) => {
  78. // console.log('mixin getMaterials data', data)
  79. // dispatch('parseMaterials', { data: data.data, uuids: uuids, imgStyle: imgStyle, callBack: callBack, callBackArgs: callBackArgs })
  80. // // commit('setItems', data.items)
  81. // })
  82. // .catch((error) => {
  83. // console.warn('Issue with getItems', error)
  84. // Promise.reject(error)
  85. // })
  86. // },
  87. // parseMaterials ({ dispatch }, { data, uuids, imgStyle, callBack, callBackArgs }) {
  88. // // data comes from jsonapi query
  89. // // uuids comes from original query (solr, FlagCollection, etc)
  90. // // so we loop from uuids to conserve the original order
  91. // console.log('mixin parseMaterials', data, uuids, callBack, callBackArgs)
  92. // const items = []
  93. // // for (var i = 0; i < data.length; i++) {
  94. // for (let i = 0; i < uuids.length; i++) {
  95. // const uuid = uuids[i]
  96. // // https://stackoverflow.com/questions/11258077/how-to-find-index-of-an-object-by-key-and-value-in-an-javascript-array
  97. // const item_index = data.findIndex(p => p.id === uuid)
  98. // // console.log('item_index', item_index);
  99. // if (item_index === -1) continue
  100. //
  101. // const item_src = data[item_index]
  102. // const attrs = item_src.attributes
  103. // const relations = item_src.relationships
  104. //
  105. // // get field values
  106. // const item = {
  107. // uuid: uuid,
  108. // title: attrs.title,
  109. // field_short_description: attrs.field_short_description,
  110. // body: attrs.body,
  111. // field_reference: attrs.field_reference
  112. // }
  113. //
  114. // // get images included values
  115. // const img_src = relations.images.data
  116. // // console.log('img_src', img_src);
  117. // // this is a temporary deactivation of images
  118. // // img_src = [];
  119. // item.images = []
  120. // for (let j = 0; j < img_src.length; j++) {
  121. // if (img_src[j].meta.imageDerivatives) {
  122. // let img_styles = {}
  123. // for (var k = 0; k < imgStyle.length; k++) {
  124. // img_styles[imgStyle[k]] = img_src[j].meta.imageDerivatives.links.[imgStyle[k]].href
  125. // }
  126. // item.images.push({
  127. // title: img_src[j].meta.title,
  128. // src: img_src[j].meta.imageDerivatives.links.hd.href,
  129. // // meta.imageDerivatives.style.href link is provided by drupal consumers_image_styles module
  130. // // BUG: missing all image derivative but first
  131. // img_styles: img_styles
  132. // })
  133. // } else {
  134. // console.warn('missing image derivative ' + j + '/' + img_src.length + ' for ' + attrs.title)
  135. // }
  136. // }
  137. //
  138. // items.push(item)
  139. // }
  140. // console.log('items', items)
  141. // dispatch(callBack, { items: items, callBackArgs: callBackArgs })
  142. // }
  143. }
  144. }
  145. })