Product.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. })
  59. },
  60. methods:{
  61. // ...mapActions({
  62. // userLogin: 'User/userLogin'
  63. // }),
  64. closeModal () {
  65. this.showLoginModal = false;
  66. },
  67. checkaddtocart() {
  68. console.log('checkaddtocart');
  69. if(!this.isloggedin){
  70. // show popup for login or register
  71. // login or register event will be catched by onLogedin or onRegistered
  72. this.showLoginModal = true
  73. }else{
  74. // if already logedin directly goes to cart operations
  75. this.addtocart()
  76. }
  77. },
  78. // event bubbled from modal login form
  79. onLogedIn () {
  80. console.log('Product: onLogedIn');
  81. this.addtocart()
  82. },
  83. // event bubbled from modal register form
  84. onRegistered () {
  85. console.log('Product: onRegistered');
  86. this.addtocart()
  87. },
  88. getCarts () {
  89. return REST.get(`/cart?_format=json`)
  90. // .then(({ data }) => {
  91. // console.log('current user carts: data', data)
  92. // })
  93. .catch(( error ) => {
  94. console.warn('Issue with get cart', error)
  95. Promise.reject(error)
  96. })
  97. },
  98. deleteCart (order_id) {
  99. console.log('deleting cart ', order_id);
  100. return REST.delete(`/cart/${order_id}/items?_format=json`)
  101. .then(({ data }) => {
  102. console.log(`product cart ${order_id} deleted: data`, data)
  103. })
  104. .catch(( error ) => {
  105. console.warn(`Issue with cart ${order_id} deleting`, error)
  106. Promise.reject(error)
  107. })
  108. },
  109. clearCarts (data) {
  110. let promises = [];
  111. // clear each cart as a promise
  112. for (var i = 0; i < data.length; i++) {
  113. promises.push(this.deleteCart(data[i].order_id))
  114. }
  115. // return all the promises as one
  116. return Promise.all(promises)
  117. },
  118. addtocart () {
  119. console.log("addtocart")
  120. this.getCarts()
  121. .then(({data}) => {
  122. console.log('current user carts: data', data)
  123. this.clearCarts(data)
  124. .then(() => {
  125. console.log('all carts cleared');
  126. // fill the cart with new product
  127. REST.post(`/cart/add?_format=json`, [{
  128. "purchased_entity_type": "commerce_product_variation",
  129. "purchased_entity_id": this.product.variation_id,
  130. "quantity": this.quantity
  131. }])
  132. .then(({ data }) => {
  133. console.log('product added to cart: data', data)
  134. this.closeModal()
  135. // redirect to /cart
  136. // window.location.href = "/cart"
  137. // TODO: redirect to checkout instead of cart
  138. window.location.href = `/checkout/${data[0].order_id}/order_information`
  139. })
  140. .catch(( error ) => {
  141. console.warn('Issue with product add to cart', error)
  142. Promise.reject(error)
  143. })
  144. })
  145. })
  146. }
  147. },
  148. components: {
  149. Modal,
  150. LoginRegister
  151. }
  152. }
  153. </script>
  154. <style lang="scss" scoped>
  155. </style>