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

View File

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

View File

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

View File

@@ -40,6 +40,7 @@ export interface ProductListResult {
price_type: { price_type: {
id: number; id: number;
name: string; name: string;
code: string;
}; };
}[]; }[];
balance: number; balance: number;
@@ -76,6 +77,7 @@ export interface ProductDetail {
price_type: { price_type: {
id: number; id: number;
name: string; name: string;
code: string;
}; };
}[]; }[];
} }
@@ -131,6 +133,7 @@ export interface FavouriteProductRes {
price_type: { price_type: {
id: number; id: number;
name: string; 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 */ /** ❌ Error */
if (error) { if (error) {
return ( return (
@@ -242,10 +246,7 @@ export function ProductCard({
<div className="p-3 sm:p-4 space-y-2 flex-1"> <div className="p-3 sm:p-4 space-y-2 flex-1">
{product.prices.length > 0 && ( {product.prices.length > 0 && (
<p className="text-lg font-bold"> <p className="text-lg font-bold">
{formatPrice( {formatPrice(Number(price), true)}
Math.min(...product.prices.map((p) => Number(p.price))),
true,
)}
<span className="text-sm text-slate-500 ml-1"> <span className="text-sm text-slate-500 ml-1">
/{measurementDisplay} /{measurementDisplay}
</span> </span>

View File

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