From 0130b9aa0de6762ca22d000d60fb87d3ce841c11 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: Mon, 2 Sep 2024 00:27:30 +0300 Subject: [PATCH] feat: new price count --- src/entities/product/index.ts | 2 +- .../product/model/use-product-price.ts | 94 ++++++++++--------- src/entities/product/ui/ProductCard.vue | 8 +- src/entities/product/ui/ProductPrice.vue | 31 ++++++ src/entities/product/ui/index.ts | 1 + src/pages/product/ui/ProductPage.vue | 34 ++----- 6 files changed, 91 insertions(+), 79 deletions(-) create mode 100644 src/entities/product/ui/ProductPrice.vue diff --git a/src/entities/product/index.ts b/src/entities/product/index.ts index 1db025d..d4e8840 100644 --- a/src/entities/product/index.ts +++ b/src/entities/product/index.ts @@ -1,3 +1,3 @@ -export { ProductCard, ProductImage } from './ui' +export { ProductCard, ProductImage, ProductPrice } from './ui' export * as productModel from './model' diff --git a/src/entities/product/model/use-product-price.ts b/src/entities/product/model/use-product-price.ts index 33ef53c..092c53e 100644 --- a/src/entities/product/model/use-product-price.ts +++ b/src/entities/product/model/use-product-price.ts @@ -1,65 +1,69 @@ -import { watchOnce } from '@vueuse/shared' +import { useStorage } from '@vueuse/core' import type { CrmVariant } from '~/src/shared/model' +import { useCRMGetCart } from '~/src/shared/api/crm/crm' -export function useProductPrice() { +export function useProductPrice(variants: CrmVariant[], id: string) { const amount = ref(0) const currentUnitPrice = ref(0) const prevUnitPrice = ref() - const variants = ref([]) + const { mutateAsync, data } = useCRMGetCart() + + const addedItems = useStorage<{ + id: string + count: number + }[]>('added-items', []) 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 - } - } + const setInitCurrentPrice = () => { + currentUnitPrice.value = Number(variants[0].price) / 100 } + watch(amount, async (value, oldValue) => { + await mutateAsync({ + data: [ + { + count: String(value), + productId: id, + }, + ], + }) + + const newCurrent = Number(data.value?.data.amount) / 100 || 0 + const newPrev = Number(data.value?.data.amountOld) / 100 || 0 + + currentUnitPrice.value = newCurrent + + if (newCurrent !== newPrev) { + prevUnitPrice.value = newPrev + } + else { + prevUnitPrice.value = undefined + } + + if (oldValue === 0 && value > 0) { + addedItems.value.push({ + id, + count: value, + }) + } + + if (oldValue > 0 && value === 0) { + addedItems.value = addedItems.value.filter(item => item.id !== id) + + setInitCurrentPrice() + } + }) + + setInitCurrentPrice() + return { amount, currentUnitPrice, prevUnitPrice, currentPriceForAll, prevUnitPriceForAll, - variants, } } diff --git a/src/entities/product/ui/ProductCard.vue b/src/entities/product/ui/ProductCard.vue index 57ffafa..cf76561 100644 --- a/src/entities/product/ui/ProductCard.vue +++ b/src/entities/product/ui/ProductCard.vue @@ -8,11 +8,7 @@ const props = defineProps<{ product: CrmProduct }>() -const { amount, currentUnitPrice, prevUnitPriceForAll, prevUnitPrice, variants } = useProductPrice() - -if (props.product.variants) { - variants.value = props.product.variants -} +const { amount, currentUnitPrice, prevUnitPriceForAll, prevUnitPrice } = useProductPrice(props.product.variants || [], props.product.id!) function onImageClick() { navigateTo( @@ -33,7 +29,7 @@ function onImageClick() {
{{ product.name }}
- {{ currentUnitPrice * (amount || 1) }} ₽ + {{ currentUnitPrice }} ₽
{{ prevUnitPriceForAll }} ₽ diff --git a/src/entities/product/ui/ProductPrice.vue b/src/entities/product/ui/ProductPrice.vue new file mode 100644 index 0000000..aff0798 --- /dev/null +++ b/src/entities/product/ui/ProductPrice.vue @@ -0,0 +1,31 @@ + + + diff --git a/src/entities/product/ui/index.ts b/src/entities/product/ui/index.ts index 800235b..51a7d63 100644 --- a/src/entities/product/ui/index.ts +++ b/src/entities/product/ui/index.ts @@ -1,2 +1,3 @@ export { default as ProductCard } from './ProductCard.vue' export { default as ProductImage } from './ProductImage.vue' +export { default as ProductPrice } from './ProductPrice.vue' diff --git a/src/pages/product/ui/ProductPage.vue b/src/pages/product/ui/ProductPage.vue index b0e97fe..5273604 100644 --- a/src/pages/product/ui/ProductPage.vue +++ b/src/pages/product/ui/ProductPage.vue @@ -1,22 +1,13 @@