productsMixins.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // https://forum.vuejs.org/t/how-to-use-helper-functions-for-imported-modules-in-vuejs-vue-template/6266/5
  2. import { REST } from 'vuejs/api/rest-axios'
  3. import Modal from 'vuejs/components/Helper/Modal'
  4. import LoginRegister from 'vuejs/components/Helper/LoginRegister'
  5. import { mapState } from 'vuex'
  6. export default {
  7. components: {
  8. Modal,
  9. LoginRegister
  10. },
  11. computed: {
  12. ...mapState({
  13. isloggedin: state => state.User.isloggedin,
  14. isAdherent: state => state.User.isAdherent,
  15. csrftoken: state => state.User.csrftoken
  16. })
  17. },
  18. methods: {
  19. closeModal () {
  20. this.showLoginModal = false
  21. },
  22. checkaddtocart (e, variation_id) {
  23. console.log('checkaddtocart', e, variation_id, this.isloggedin)
  24. if (!this.isloggedin) {
  25. // show popup for login or register
  26. // login or register event will be catched by onLogedin or onRegistered
  27. // this.showLoginModal = variation_id
  28. this.showLoginModal(variation_id)
  29. } else {
  30. // if already logedin directly goes to cart operations
  31. this.addtocart(variation_id)
  32. }
  33. },
  34. showLoginModal (variation_id) {
  35. this.$modal.show(
  36. LoginRegister,
  37. // props
  38. {
  39. // item: this.item,
  40. header: 'materio.In order to be able to place your order, please log in or create your account',
  41. callbackargs: { variation_id: variation_id },
  42. // close: (variation_id) => { this.closeModal(variation_id) },
  43. onLogedInBack: (cba) => { this.onLogedIn(cba.variation_id) },
  44. onRegisteredBack: (cba) => { this.onRegistered(cba.variation_id) }
  45. // // not really an event
  46. // // more a callback function passed as prop to the component
  47. // addNoteId: (id) => {
  48. // // no needs to refresh the entire item via searchresults
  49. // // plus if we are in article, there is not searchresults
  50. // // this.refreshItem({id: this.item.id})
  51. // // instead create the note id directly
  52. // this.item.note = { id: id }
  53. // }
  54. },
  55. // settings
  56. {
  57. name: 'modal-loginregister',
  58. draggable: false,
  59. classes: 'vm--modale-loginregister',
  60. width: '550px',
  61. height: '400px'
  62. }
  63. )
  64. },
  65. // event bubbled from modal login form
  66. onLogedIn (variation_id) {
  67. console.log('Product: onLogedIn. variation_id', variation_id)
  68. this.addtocart(variation_id)
  69. },
  70. // event bubbled from modal register form
  71. onRegistered (variation_id) {
  72. console.log('Product: onRegistered. variation_id', variation_id)
  73. this.addtocart(variation_id)
  74. },
  75. getCarts () {
  76. // this is bugging on stage
  77. return REST.get('/cart?_format=json', {}, { 'X-CSRF-Token': this.csrftoken })
  78. // .then(({ data }) => {
  79. // console.log('current user carts: data', data)
  80. // })
  81. .catch((error) => {
  82. console.warn('Issue with get cart', error)
  83. Promise.reject(error)
  84. })
  85. },
  86. deleteCart (order_id) {
  87. console.log('deleting cart ', order_id)
  88. return REST.delete(`/cart/${order_id}/items?_format=json`)
  89. .then(({ data }) => {
  90. console.log(`product cart ${order_id} deleted: data`, data)
  91. })
  92. .catch((error) => {
  93. console.warn(`Issue with cart ${order_id} deleting`, error)
  94. Promise.reject(error)
  95. })
  96. },
  97. clearCarts (data) {
  98. const promises = []
  99. // clear each cart as a promise
  100. for (let i = 0; i < data.length; i++) {
  101. promises.push(this.deleteCart(data[i].order_id))
  102. }
  103. // return all the promises as one
  104. return Promise.all(promises)
  105. },
  106. addtocart (variation_id) {
  107. console.log('addtocart')
  108. // transition
  109. this.$modal.hide('modal-loginregister')
  110. document.getElementById('pricing').classList.add('loading')
  111. // handle the cart then redirect to checkout flow
  112. this.getCarts()
  113. .then(({ data }) => {
  114. console.log('current user carts: data', data)
  115. this.clearCarts(data)
  116. .then(() => {
  117. console.log('all carts cleared')
  118. // fill the cart with new product
  119. REST.post('/cart/add?_format=json', [{
  120. purchased_entity_type: 'commerce_product_variation',
  121. purchased_entity_id: variation_id,
  122. quantity: this.quantity
  123. }])
  124. .then(({ data }) => {
  125. console.log('product added to cart: data', data)
  126. this.closeModal()
  127. // redirect to /cart
  128. // window.location.href = "/cart"
  129. // TODO: redirect to checkout instead of cart
  130. window.location.href = `/checkout/${data[0].order_id}/order_information`
  131. })
  132. .catch((error) => {
  133. console.warn('Issue with product add to cart', error)
  134. Promise.reject(error)
  135. })
  136. })
  137. })
  138. }
  139. }
  140. }