showrooms.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // import JSONAPI from 'vuejs/api/json-axios'
  2. // import REST from 'vuejs/api/rest-axios'
  3. // import MA from 'vuejs/api/ma-axios'
  4. // import qs from 'querystring-es3'
  5. import MGQ from 'vuejs/api/graphql-axios'
  6. import { print } from 'graphql/language/printer'
  7. import gql from 'graphql-tag'
  8. import ShowroomFields from 'vuejs/api/gql/showroom.fragment.gql'
  9. export default {
  10. namespaced: true,
  11. // initial state
  12. state: {
  13. items: [],
  14. showrooms_by_tid: {}
  15. },
  16. // getters
  17. getters: {},
  18. // mutations
  19. mutations: {
  20. setItems (state, items) {
  21. state.items = items
  22. items.forEach((item, i) => {
  23. state.showrooms_by_tid[item.id] = item
  24. })
  25. // console.log('Showroom setitems', state.showrooms_by_tid)
  26. }
  27. },
  28. // actions
  29. actions: {
  30. getShowrooms ({ dispatch, commit, state }) {
  31. // REST.get('/showrooms_rest?_format=json', {})
  32. // .then(({ data }) => {
  33. // console.log('showrooms REST: data', data)
  34. // commit('setItems', data)
  35. // })
  36. // .catch((error) => {
  37. // console.warn('Issue with showrooms', error)
  38. // Promise.reject(error)
  39. // })
  40. const ast = gql`{
  41. allshowrooms(lang: "${drupalDecoupled.lang_code}") {
  42. ...ShowroomFields
  43. }
  44. }
  45. ${ShowroomFields}
  46. `
  47. MGQ.post('', { query: print(ast) })
  48. .then(({ data: { data: { allshowrooms } } }) => {
  49. console.log('loadshowrooms showrooms loaded', allshowrooms)
  50. commit('setItems', allshowrooms)
  51. })
  52. .catch(error => {
  53. console.warn('Issue with getShowrooms', error)
  54. Promise.reject(error)
  55. })
  56. }
  57. }
  58. }