Product.vue 4.6 KB

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