Product.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. >
  31. <div>
  32. Please login or register before continue.
  33. </div>
  34. <LoginRegisterForm @onLogedIn="logedIn"/>
  35. </Modal>
  36. </article>
  37. </template>
  38. <script>
  39. import { REST } from 'vuejs/api/rest-axios'
  40. import router from 'vuejs/route'
  41. import { mapState, mapActions } from 'vuex'
  42. import Modal from 'vuejs/components/Helper/Modal'
  43. import LoginRegisterForm from 'vuejs/components/Helper/LoginRegisterForm'
  44. let basePath = drupalSettings.path.baseUrl + drupalSettings.path.pathPrefix;
  45. export default {
  46. name: "Product",
  47. router,
  48. props: ['product'],
  49. data(){
  50. return {
  51. quantity: 1,
  52. showLoginModal:false
  53. }
  54. },
  55. computed: {
  56. ...mapState({
  57. isloggedin: state => state.User.isloggedin,
  58. isAdherent: state => state.User.isAdherent
  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. // TODO: show popup login or register
  72. this.showLoginModal = true
  73. // TODO: rest login or register
  74. // TODO: rest login
  75. // TODO: this.addtocart()
  76. }else{
  77. this.addtocart()
  78. }
  79. },
  80. logedIn () {
  81. console.log('Product: logedIn');
  82. this.closeModal()
  83. },
  84. addtocart () {
  85. console.log("addtocart")
  86. REST.post(`/cart/add?_format=json`, [{
  87. "purchased_entity_type": "commerce_product_variation",
  88. "purchased_entity_id": this.product.variation_id,
  89. "quantity": this.quantity
  90. }])
  91. .then(({ data }) => {
  92. console.log('product add to cart REST: data', data)
  93. })
  94. .catch(( error ) => {
  95. console.warn('Issue with pricing', error)
  96. Promise.reject(error)
  97. })
  98. }
  99. },
  100. components: {
  101. Modal,
  102. LoginRegisterForm
  103. }
  104. }
  105. </script>
  106. <style lang="scss" scoped>
  107. </style>