update price

This commit is contained in:
Samandar Turgunboyev
2026-03-04 10:14:49 +05:00
parent a0994df9bd
commit 3fcbf22b70
6 changed files with 40 additions and 29 deletions

View File

@@ -146,6 +146,12 @@ const CartPage = () => {
});
};
const price = cartItems?.map((item) =>
item.product.prices.find((p) => p.price_type.code === '1')
? item.product.prices.find((p) => p.price_type.code === '1')?.price
: Math.min(...item.product.prices.map((p) => Number(p.price))),
);
if (isLoading)
return (
<div className="min-h-screen flex items-center justify-center">
@@ -177,10 +183,15 @@ const CartPage = () => {
const subtotal =
cartItems.reduce((sum, item) => {
if (!item.product.prices.length) return sum;
const price = Math.min(
...item.product.prices.map((p) => Number(p.price)),
);
const price = item.product.prices.find((p) => p.price_type.code === '1')
? Number(
item.product.prices.find((p) => p.price_type.code === '1')?.price,
)
: Math.min(...item.product.prices.map((p) => Number(p.price)));
const qty = quantities[item.id] ?? item.quantity;
return sum + price * qty;
}, 0) || 0;
@@ -243,12 +254,7 @@ const CartPage = () => {
<div className="flex items-center gap-2 mb-3">
<span className="text-blue-600 font-bold text-xl">
{formatPrice(
Math.min(
...item.product.prices.map((p) => Number(p.price)),
),
true,
)}
{formatPrice(Number(price), true)}
</span>
<span className="text-sm text-gray-500">
/{measurementDisplay}

View File

@@ -139,11 +139,13 @@ const OrderPage = () => {
if (item.product.prices.length === 0) return sum; // narx yo'q bo'lsa qo'shmaymiz
// Eng yuqori narxni olish
const maxPrice = Math.min(
...item.product.prices.map((p) => Number(p.price)),
);
const maxPrice = item.product.prices.find(
(p) => p.price_type.code === '1',
)
? item.product.prices.find((p) => p.price_type.code === '1')?.price
: Math.min(...item.product.prices.map((p) => Number(p.price)));
return sum + maxPrice * item.quantity;
return sum + Number(maxPrice) * item.quantity;
}, 0) || 0; // cartItems bo'sh bo'lsa 0 qaytaradi
const [coords, setCoords] = useState({

View File

@@ -213,7 +213,9 @@ const ProductDetail = () => {
};
const subtotal = data?.prices?.length
? Math.min(...data.prices.map((p) => Number(p.price)))
? data.prices.find((p) => p.price_type.code === '1')
? data.prices.find((p) => p.price_type.code === '1')?.price
: Math.min(...data.prices.map((p) => Number(p.price)))
: 0;
/* ---------------- LOADING ---------------- */
@@ -250,7 +252,7 @@ const ProductDetail = () => {
<div className="flex items-baseline gap-2 mb-4">
<span className="text-4xl font-bold text-blue-600">
{formatPrice(subtotal, true)}
{formatPrice(Number(subtotal), true)}
</span>
<span className="text-xl text-gray-500">/{measurementDisplay}</span>
</div>
@@ -298,7 +300,7 @@ const ProductDetail = () => {
</div>
<div className="mb-6 text-xl font-semibold">
{t('Jami')}: {formatPrice(subtotal * numericQty, true)}
{t('Jami')}: {formatPrice(Number(subtotal) * numericQty, true)}
</div>
<div className="flex gap-3">

View File

@@ -40,6 +40,7 @@ export interface ProductListResult {
price_type: {
id: number;
name: string;
code: string;
};
}[];
balance: number;
@@ -76,6 +77,7 @@ export interface ProductDetail {
price_type: {
id: number;
name: string;
code: string;
};
}[];
}
@@ -131,6 +133,7 @@ export interface FavouriteProductRes {
price_type: {
id: number;
name: string;
code: string;
};
}[];
}

View File

@@ -184,6 +184,10 @@ export function ProductCard({
},
});
const price = product.prices.find((p) => p.price_type.code === '1')
? product.prices.find((p) => p.price_type.code === '1')?.price
: Math.min(...product.prices.map((p) => Number(p.price)));
/** ❌ Error */
if (error) {
return (
@@ -242,10 +246,7 @@ export function ProductCard({
<div className="p-3 sm:p-4 space-y-2 flex-1">
{product.prices.length > 0 && (
<p className="text-lg font-bold">
{formatPrice(
Math.min(...product.prices.map((p) => Number(p.price))),
true,
)}
{formatPrice(Number(price), true)}
<span className="text-sm text-slate-500 ml-1">
/{measurementDisplay}
</span>

View File

@@ -107,7 +107,9 @@ export const SearchResult = ({ query }: SearchResultProps) => {
? product.images[0].image
: BASE_URL + product.images[0].image
: LogosProduct;
const price = product.prices?.[0]?.price;
const price = product.prices.find((p) => p.price_type.code === '1')
? product.prices.find((p) => p.price_type.code === '1')?.price
: Math.min(...product.prices.map((p) => Number(p.price)));
return (
<div
@@ -128,14 +130,9 @@ export const SearchResult = ({ query }: SearchResultProps) => {
<p className="text-sm font-medium text-slate-900 line-clamp-2">
{product.name}
</p>
{price && (
<p className="text-sm font-semibold text-[#57A595] mt-1">
{formatPrice(
Math.min(...product.prices.map((p) => Number(p.price))),
true,
)}
</p>
)}
<p className="text-sm font-semibold text-[#57A595] mt-1">
{formatPrice(Number(price), true)}
</p>
</div>
</div>
);