productsMixins.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. callbackargs: { variation_id: variation_id },
  41. // close: (variation_id) => { this.closeModal(variation_id) },
  42. onLogedInBack: (cba) => { this.onLogedIn(cba.variation_id) },
  43. onRegisteredBack: (cba) => { this.onRegistered(cba.variation_id) }
  44. // // not really an event
  45. // // more a callback function passed as prop to the component
  46. // addNoteId: (id) => {
  47. // // no needs to refresh the entire item via searchresults
  48. // // plus if we are in article, there is not searchresults
  49. // // this.refreshItem({id: this.item.id})
  50. // // instead create the note id directly
  51. // this.item.note = { id: id }
  52. // }
  53. },
  54. // settings
  55. {
  56. name: 'modal-loginregister',
  57. draggable: false,
  58. classes: 'vm--modale-loginregister',
  59. width: '500px',
  60. height: '350px'
  61. }
  62. )
  63. },
  64. // event bubbled from modal login form
  65. onLogedIn (variation_id) {
  66. console.log('Product: onLogedIn. variation_id', variation_id)
  67. this.addtocart(variation_id)
  68. },
  69. // event bubbled from modal register form
  70. onRegistered (variation_id) {
  71. console.log('Product: onRegistered. variation_id', variation_id)
  72. this.addtocart(variation_id)
  73. },
  74. getCarts () {
  75. // this is bugging on stage
  76. return REST.get('/cart?_format=json', {}, { 'X-CSRF-Token': this.csrftoken })
  77. // .then(({ data }) => {
  78. // console.log('current user carts: data', data)
  79. // })
  80. .catch((error) => {
  81. console.warn('Issue with get cart', error)
  82. Promise.reject(error)
  83. })
  84. },
  85. deleteCart (order_id) {
  86. console.log('deleting cart ', order_id)
  87. return REST.delete(`/cart/${order_id}/items?_format=json`)
  88. .then(({ data }) => {
  89. console.log(`product cart ${order_id} deleted: data`, data)
  90. })
  91. .catch((error) => {
  92. console.warn(`Issue with cart ${order_id} deleting`, error)
  93. Promise.reject(error)
  94. })
  95. },
  96. clearCarts (data) {
  97. const promises = []
  98. // clear each cart as a promise
  99. for (let i = 0; i < data.length; i++) {
  100. promises.push(this.deleteCart(data[i].order_id))
  101. }
  102. // return all the promises as one
  103. return Promise.all(promises)
  104. },
  105. addtocart (variation_id) {
  106. console.log('addtocart')
  107. // transition
  108. this.$modal.hide('modal-loginregister')
  109. document.getElementById('pricing').classList.add('loading')
  110. // handle the cart then redirect to checkout flow
  111. this.getCarts()
  112. .then(({ data }) => {
  113. console.log('current user carts: data', data)
  114. this.clearCarts(data)
  115. .then(() => {
  116. console.log('all carts cleared')
  117. // fill the cart with new product
  118. REST.post('/cart/add?_format=json', [{
  119. purchased_entity_type: 'commerce_product_variation',
  120. purchased_entity_id: variation_id,
  121. quantity: this.quantity
  122. }])
  123. .then(({ data }) => {
  124. console.log('product added to cart: data', data)
  125. this.closeModal()
  126. // redirect to /cart
  127. // window.location.href = "/cart"
  128. // TODO: redirect to checkout instead of cart
  129. window.location.href = `/checkout/${data[0].order_id}/order_information`
  130. })
  131. .catch((error) => {
  132. console.warn('Issue with product add to cart', error)
  133. Promise.reject(error)
  134. })
  135. })
  136. })
  137. }
  138. }
  139. }