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