MenuItem.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <div
  3. class="item"
  4. >
  5. <img
  6. :src="`${API_URL}/${item.title}.jpg`"
  7. class="image"
  8. />
  9. <div class="title">
  10. {{ item.title }}<span v-if="quantity>0">&nbsp;{{ item.price }} руб</span>
  11. </div>
  12. <button
  13. v-if="quantity==0"
  14. class="price-button"
  15. @click="addItemToCart"
  16. >
  17. {{ item.price }} руб
  18. </button>
  19. <div v-else class="quantity-wrapper">
  20. <button
  21. class="qty-minus qty-button"
  22. @click="removeItemFromCart"
  23. >
  24. -
  25. </button>
  26. <span class="qty-value">{{ quantity }}</span>
  27. <button
  28. class="qty-plus qty-button"
  29. @click="addItemToCart"
  30. >
  31. +
  32. </button>
  33. </div>
  34. </div>
  35. </template>
  36. <script setup>
  37. import { CartItem } from "@/helpers/common"
  38. import { storeToRefs } from "pinia"
  39. import { computed } from "vue"
  40. import { useItemsStore, API_URL } from '/src/stores/items.js'
  41. const props = defineProps({
  42. item: Object
  43. })
  44. const menuStore = useItemsStore()
  45. const { cartList } = storeToRefs(menuStore)
  46. function addItemToCart () {
  47. // console.log('Добавляю блюдо в корзину')
  48. const itemInCart = cartList.value.find(i => i.title == props.item.title)
  49. if (itemInCart) itemInCart.quantity++
  50. else cartList.value.push( new CartItem(props.item) )
  51. }
  52. function removeItemFromCart () {
  53. // console.log('Добавляю блюдо в корзину')
  54. const itemPos = cartList.value.findIndex(i => i.title == props.item.title)
  55. if (itemPos>=0) {
  56. cartList.value[itemPos].quantity--
  57. if (cartList.value[itemPos].quantity == 0) {
  58. cartList.value.splice(itemPos, 1)
  59. }
  60. }
  61. }
  62. const quantity = computed(() => {
  63. const itemInCart = cartList.value
  64. .find(i => i.title == props.item.title)
  65. if (itemInCart) return itemInCart.quantity
  66. else return ''
  67. })
  68. // defineExpose({ quantity })
  69. </script>
  70. <style lang="css" scoped>
  71. .item {
  72. display: inline-block;
  73. width: 300px;
  74. margin: 8px;
  75. background-color: bisque;
  76. border-radius: 16px;
  77. }
  78. .image {
  79. width: 100%;
  80. border-top-left-radius: 16px;
  81. border-top-right-radius: 16px;
  82. }
  83. .title {
  84. padding: 16px;
  85. }
  86. .price-button {
  87. width: calc(100% - 16px);
  88. margin: 8px;
  89. padding: 16px;
  90. border-radius: 12px;
  91. border: none;
  92. background-color: lightgray;
  93. cursor: pointer;
  94. }
  95. .quantity-wrapper {
  96. display: flex;
  97. width: 100%;
  98. padding: 8px;
  99. justify-content: space-between;
  100. }
  101. .qty-button {
  102. width: 50px;
  103. padding: 16px;
  104. border-radius: 12px;
  105. border: none;
  106. background-color: lightgray;
  107. cursor: pointer;
  108. }
  109. </style>