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 @@