From 63d31fbe53388116f53b82fe9575b9676c9f2288 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BE=D0=BD=D1=81=D1=82=D0=B0=D0=BD=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=20=D0=A3=D0=BA=D0=BE=D0=BB=D0=BE=D0=B2?= <“ukolov.konst@gmail.com”> Date: Sat, 31 Aug 2024 14:04:49 +0300 Subject: [PATCH] feat: add description --- orval.config.ts | 2 +- src/entities/product/index.ts | 2 + src/entities/product/model/index.ts | 1 + .../product/model/use-product-price.ts | 65 +++++++++++++++++++ src/entities/product/ui/ProductCard.vue | 50 +++----------- src/pages/product/ui/ProductPage.vue | 35 +++++++++- src/shared/api/crm/crm.ts | 28 ++++---- 7 files changed, 123 insertions(+), 60 deletions(-) create mode 100644 src/entities/product/model/index.ts create mode 100644 src/entities/product/model/use-product-price.ts diff --git a/orval.config.ts b/orval.config.ts index 203b4b1..6e564ed 100644 --- a/orval.config.ts +++ b/orval.config.ts @@ -7,7 +7,7 @@ export default defineConfig({ target: 'src/shared/api', schemas: 'src/shared/model', client: 'vue-query', - baseUrl: 'https://cake-crm.3crabs.ru', + baseUrl: 'https://cake-api.3crabs.ru', // mock: true, }, input: './cakes.json', diff --git a/src/entities/product/index.ts b/src/entities/product/index.ts index cb851da..1db025d 100644 --- a/src/entities/product/index.ts +++ b/src/entities/product/index.ts @@ -1 +1,3 @@ export { ProductCard, ProductImage } from './ui' + +export * as productModel from './model' diff --git a/src/entities/product/model/index.ts b/src/entities/product/model/index.ts new file mode 100644 index 0000000..ddc4f56 --- /dev/null +++ b/src/entities/product/model/index.ts @@ -0,0 +1 @@ +export { useProductPrice } from './use-product-price' diff --git a/src/entities/product/model/use-product-price.ts b/src/entities/product/model/use-product-price.ts new file mode 100644 index 0000000..33ef53c --- /dev/null +++ b/src/entities/product/model/use-product-price.ts @@ -0,0 +1,65 @@ +import { watchOnce } from '@vueuse/shared' +import type { CrmVariant } from '~/src/shared/model' + +export function useProductPrice() { + const amount = ref(0) + const currentUnitPrice = ref(0) + const prevUnitPrice = ref() + const variants = ref([]) + + const currentPriceForAll = computed(() => currentUnitPrice.value * amount.value) + + const prevUnitPriceForAll = computed(() => prevUnitPrice.value * amount.value) + + watch(amount, () => { + updatePrices() + }) + + watchOnce(variants, () => { + updatePrices() + }) + + function updatePrices() { + if (!variants.value.length) { + return + } + + for (let i = 0; i < variants.value.length; i++) { + const variant = variants.value[i] + + let max = Number.POSITIVE_INFINITY + let min = 0 + + variant.properties?.forEach((property) => { + if (property.name === 'min') { + min = Number(property.value) + } + + if (property.name === 'max') + max = Number(property.value) + }) + + if (amount.value >= min && amount.value <= max) { + currentUnitPrice.value = Number(variant.price) / 100 + + if (i !== 0) { + prevUnitPrice.value = Number(variants.value[i - 1].price) / 100 + } + else { + prevUnitPrice.value = undefined + } + + return + } + } + } + + return { + amount, + currentUnitPrice, + prevUnitPrice, + currentPriceForAll, + prevUnitPriceForAll, + variants, + } +} diff --git a/src/entities/product/ui/ProductCard.vue b/src/entities/product/ui/ProductCard.vue index 10000f9..57ffafa 100644 --- a/src/entities/product/ui/ProductCard.vue +++ b/src/entities/product/ui/ProductCard.vue @@ -1,4 +1,5 @@