Files
hedena_gold/apps/user-app/src/composables/useProduct.ts
T
2026-09-09 06:46:27 +03:30

56 lines
2.7 KiB
TypeScript

// Adapts our actual ProductSummary (apps/user-app/src/api/client.ts) into the
// same output shape legacy's Mixins/product/index.ts `useProduct()` produced,
// so the ported product-card components (ProductHorizontal/ProductVertical/
// ProductVerticalFull) can render unchanged.
//
// Honest gaps vs. legacy (no backend equivalent exists yet — apps/api was not
// touched to add these):
// - promotions/discounts: legacy matched a customer's order against admin
// promotion + ignore-list rules (getCandidPromotions/getPromotion). No such
// data exists in the new API, so `hasDiscountWage` is always null here —
// the discount badge and "before/after" price never render, which is
// correct given there is genuinely nothing to show, not a bug.
// - `wage`: legacy added the logged-in customer's own `percent` (a
// commission/markup field on their account) on top of the variant's
// making-charge %. No login exists in this port yet, so wage is just the
// selected variant's defaultWagePercent on its own.
// - `is_exist` (stock) and `has_warranty`: no such fields exist on our
// ProductVariant model yet — always true / false respectively.
import { computed, ref, unref, type Ref } from 'vue'
import type { ProductSummary, ProductVariantSummary } from '../api/client'
function getCheapestVariant(variants: ProductVariantSummary[] | undefined): ProductVariantSummary | null {
if (!variants || variants.length === 0) return null
return variants.reduce((cheapest, v) =>
parseFloat(v.defaultWagePercent) < parseFloat(cheapest.defaultWagePercent) ? v : cheapest,
)
}
export function useProduct(productItem: ProductSummary, initialVariant?: ProductVariantSummary) {
const product = ref<ProductSummary>(productItem)
const image = computed(() => product.value.images?.[0]?.url)
const id = computed(() => product.value.id)
const title = computed(() => product.value.title)
const variants = computed(() => product.value.variants)
const selectedVariant: Ref<ProductVariantSummary | null> = ref(initialVariant ?? getCheapestVariant(unref(variants)))
const min_gram = computed(() => selectedVariant.value ? parseFloat(selectedVariant.value.rashWeightGrams) : 0)
const wage = computed(() => (selectedVariant.value ? parseFloat(selectedVariant.value.defaultWagePercent) : null))
const hasDiscountWage = ref<number | null>(null) // no promotions backend yet — see file header
const is_exist = ref(true) // no stock field on ProductVariant yet
const has_warranty = ref(false) // no warranty field on ProductVariant yet
return {
product,
image,
id,
title,
variants,
selectedVariant,
min_gram,
wage,
hasDiscountWage,
is_exist,
has_warranty,
}
}