Product.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <article class="product">
  3. <header>
  4. <h1 v-html="product.title" />
  5. </header>
  6. <section class="content">
  7. <div class="description" v-html="product.body" />
  8. </section>
  9. <aside v-if="!isAdherent">
  10. <button
  11. v-for="variation in product.variations"
  12. type="button"
  13. name="addtocart"
  14. @click.stop="checkaddtocart"
  15. >
  16. Commander {{ variation.price.value }}
  17. </button>
  18. </aside>
  19. <Modal
  20. v-if="showLoginModal"
  21. @close="closeModal"
  22. :styles="{width:'500px', height:'100%'}"
  23. >
  24. <h2>Please login or register before continue.</h2>
  25. <LoginRegister @onLogedIn="onLogedIn" @onRegistered="onRegistered" />
  26. </Modal>
  27. </article>
  28. </template>
  29. <script>
  30. import { REST } from 'vuejs/api/rest-axios'
  31. import router from 'vuejs/route'
  32. import { mapState, mapActions } from 'vuex'
  33. import Modal from 'vuejs/components/Helper/Modal'
  34. import LoginRegister from 'vuejs/components/Helper/LoginRegister'
  35. let basePath = drupalSettings.path.baseUrl + drupalSettings.path.pathPrefix;
  36. export default {
  37. name: "Product",
  38. router,
  39. props: ['product'],
  40. data(){
  41. return {
  42. quantity: 1,
  43. showLoginModal:false
  44. }
  45. },
  46. computed: {
  47. ...mapState({
  48. isloggedin: state => state.User.isloggedin,
  49. isAdherent: state => state.User.isAdherent,
  50. csrftoken: state => state.User.csrftoken
  51. })
  52. },
  53. methods:{
  54. // ...mapActions({
  55. // userLogin: 'User/userLogin'
  56. // }),
  57. closeModal () {
  58. this.showLoginModal = false;
  59. },
  60. checkaddtocart() {
  61. console.log('checkaddtocart');
  62. if(!this.isloggedin){
  63. // show popup for login or register
  64. // login or register event will be catched by onLogedin or onRegistered
  65. this.showLoginModal = true
  66. }else{
  67. // if already logedin directly goes to cart operations
  68. this.addtocart()
  69. }
  70. },
  71. // event bubbled from modal login form
  72. onLogedIn () {
  73. console.log('Product: onLogedIn');
  74. this.addtocart()
  75. },
  76. // event bubbled from modal register form
  77. onRegistered () {
  78. console.log('Product: onRegistered');
  79. this.addtocart()
  80. },
  81. getCarts () {
  82. // this is bugging on stage
  83. return REST.get(`/cart?_format=json`, {}, {'X-CSRF-Token':this.csrftoken})
  84. // .then(({ data }) => {
  85. // console.log('current user carts: data', data)
  86. // })
  87. .catch(( error ) => {
  88. console.warn('Issue with get cart', error)
  89. Promise.reject(error)
  90. })
  91. },
  92. deleteCart (order_id) {
  93. console.log('deleting cart ', order_id);
  94. return REST.delete(`/cart/${order_id}/items?_format=json`)
  95. .then(({ data }) => {
  96. console.log(`product cart ${order_id} deleted: data`, data)
  97. })
  98. .catch(( error ) => {
  99. console.warn(`Issue with cart ${order_id} deleting`, error)
  100. Promise.reject(error)
  101. })
  102. },
  103. clearCarts (data) {
  104. let promises = [];
  105. // clear each cart as a promise
  106. for (var i = 0; i < data.length; i++) {
  107. promises.push(this.deleteCart(data[i].order_id))
  108. }
  109. // return all the promises as one
  110. return Promise.all(promises)
  111. },
  112. addtocart () {
  113. console.log("addtocart")
  114. this.getCarts()
  115. .then(({data}) => {
  116. console.log('current user carts: data', data)
  117. this.clearCarts(data)
  118. .then(() => {
  119. console.log('all carts cleared');
  120. // fill the cart with new product
  121. REST.post(`/cart/add?_format=json`, [{
  122. "purchased_entity_type": "commerce_product_variation",
  123. "purchased_entity_id": this.product.variation_id,
  124. "quantity": this.quantity
  125. }])
  126. .then(({ data }) => {
  127. console.log('product added to cart: data', data)
  128. this.closeModal()
  129. // redirect to /cart
  130. // window.location.href = "/cart"
  131. // TODO: redirect to checkout instead of cart
  132. window.location.href = `/checkout/${data[0].order_id}/order_information`
  133. })
  134. .catch(( error ) => {
  135. console.warn('Issue with product add to cart', error)
  136. Promise.reject(error)
  137. })
  138. })
  139. })
  140. }
  141. },
  142. components: {
  143. Modal,
  144. LoginRegister
  145. }
  146. }
  147. </script>
  148. <style lang="scss" scoped>
  149. </style>