pages.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { REST } from 'vuejs/api/rest-axios'
  2. import { MGQ } from 'vuejs/api/graphql-axios'
  3. import { print } from 'graphql/language/printer'
  4. import gql from 'graphql-tag'
  5. import productsGQL from 'vuejs/api/gql/products.fragment.gql'
  6. export default {
  7. namespaced: true,
  8. // initial state
  9. state: {
  10. products_ids: [],
  11. products: []
  12. },
  13. // getters
  14. getters: {},
  15. // mutations
  16. mutations: {
  17. setProductsIds (state, ids) {
  18. state.products_ids = ids
  19. },
  20. setProducts (state, p) {
  21. state.products = p
  22. }
  23. },
  24. // actions
  25. actions: {
  26. getProducts ({ dispatch, commit, state }) {
  27. dispatch('loadProductsIds')
  28. },
  29. loadProductsIds ({ dispatch, commit, state }) {
  30. REST.get('/pricing_rest?_format=json', {})
  31. .then(({ data }) => {
  32. console.log('getProducts REST: data', data)
  33. const ids = []
  34. for (let i = 0; i < data.length; i++) {
  35. ids.push(data[i].product_id)
  36. }
  37. commit('setProductsIds', ids)
  38. dispatch('loadProducts')
  39. })
  40. .catch((error) => {
  41. console.warn('Issue with pricing', error)
  42. Promise.reject(error)
  43. })
  44. },
  45. loadProducts ({ dispatch, commit, state }) {
  46. const ast = gql`{
  47. products(ids: [${state.products_ids}]) {
  48. ...ProductsFields
  49. }
  50. }
  51. ${productsGQL}
  52. `
  53. MGQ.post('', { query: print(ast) })
  54. .then((resp) => {
  55. console.log('loadProductsGQL resp', resp)
  56. commit('setProducts', resp.data.data.products)
  57. })
  58. .catch(error => {
  59. console.warn('Issue with loadProducts', error)
  60. Promise.reject(error)
  61. })
  62. }
  63. }
  64. }