order status update

This commit is contained in:
Samandar Turgunboyev
2026-02-13 15:50:53 +05:00
parent f6231229da
commit d5148aaf06
6 changed files with 331 additions and 261 deletions

View File

@@ -39,6 +39,12 @@ const CartPage = () => {
const [quantities, setQuantities] = useState<Record<string, string>>({});
const debounceRef = useRef<Record<string, NodeJS.Timeout | null>>({});
// O'lchov birligini formatlash uchun yordamchi funksiya
const getQuantityMessage = (qty: number, measurement: string | null) => {
if (!measurement) return `${qty} dona`;
return `${qty} ${measurement}`;
};
useEffect(() => {
if (!cartItems) return;
const initialQuantities: Record<string, string> = {};
@@ -57,8 +63,24 @@ const CartPage = () => {
body: { quantity: number };
cart_item_id: string;
}) => cart_api.update_cart_item({ body, cart_item_id }),
onSuccess: () =>
queryClient.invalidateQueries({ queryKey: ['cart_items', cart_id] }),
onSuccess: (_, variables) => {
queryClient.invalidateQueries({ queryKey: ['cart_items', cart_id] });
// Qaysi mahsulot yangilanganini topish
const item = cartItems?.find(
(i) => String(i.id) === variables.cart_item_id,
);
if (item) {
const measurementName = item.product.meansurement?.name || null;
toast.success(
`${t('Miqdor')} ${getQuantityMessage(variables.body.quantity, measurementName)} ${t('ga yangilandi')}`,
{
richColors: true,
position: 'top-center',
},
);
}
},
onError: (err: AxiosError) =>
toast.error(err.message, { richColors: true, position: 'top-center' }),
});
@@ -66,8 +88,13 @@ const CartPage = () => {
const { mutate: deleteCartItem } = useMutation({
mutationFn: ({ cart_item_id }: { cart_item_id: string }) =>
cart_api.delete_cart_item(cart_item_id),
onSuccess: () =>
queryClient.invalidateQueries({ queryKey: ['cart_items', cart_id] }),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['cart_items', cart_id] });
toast.success(t("Savatdan o'chirildi"), {
richColors: true,
position: 'top-center',
});
},
onError: (err: AxiosError) =>
toast.error(err.message, { richColors: true, position: 'top-center' }),
});
@@ -104,15 +131,14 @@ const CartPage = () => {
const subtotal =
cartItems?.reduce((sum, item) => {
if (item.product.prices.length === 0) return sum; // narx yo'q bo'lsa qo'shmaymiz
if (item.product.prices.length === 0) return sum;
// Eng yuqori narxni olish
const maxPrice = Math.max(
...item.product.prices.map((p) => Number(p.price)),
);
return sum + maxPrice * item.quantity;
}, 0) || 0; // cartItems bo'sh bo'lsa 0 qaytaradi
}, 0) || 0;
const handleQuantityChange = (itemId: string, value: number) => {
setQuantities((prev) => ({
@@ -143,107 +169,125 @@ const CartPage = () => {
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
<div className="lg:col-span-2">
<div className="bg-white rounded-lg shadow-md overflow-hidden">
{cartItems.map((item, index) => (
<div
key={item.id}
className={`p-6 flex relative gap-4 ${
index !== cartItems.length - 1 ? 'border-b' : ''
}`}
>
<Button
variant="destructive"
size="icon"
onClick={() =>
deleteCartItem({ cart_item_id: String(item.id) })
}
className="absolute right-2 w-7 h-7 top-2 cursor-pointer"
{cartItems.map((item, index) => {
const measurementDisplay =
item.product.meansurement?.name || 'шт.';
return (
<div
key={item.id}
className={`p-6 flex relative gap-4 ${
index !== cartItems.length - 1 ? 'border-b' : ''
}`}
>
<Trash className="size-4" />
</Button>
<div className="w-24 h-40 bg-gray-100 rounded-lg flex-shrink-0 overflow-hidden">
<Image
src={
item.product.images.length > 0
? item.product.images[0].image.includes(BASE_URL)
? item.product.images[0].image
: BASE_URL + item.product.images[0].image
: ProductBanner
<Button
variant="destructive"
size="icon"
onClick={() =>
deleteCartItem({ cart_item_id: String(item.id) })
}
alt={item.product.name}
width={500}
height={500}
unoptimized
className="object-cover"
style={{ width: '100%', height: '100%' }}
/>
</div>
className="absolute right-2 w-7 h-7 top-2 cursor-pointer"
>
<Trash className="size-4" />
</Button>
<div className="flex-1">
<h3 className="font-semibold text-lg mb-1">
{item.product.name}
</h3>
<div className="flex items-center gap-2 mb-3 max-lg:flex-col max-lg:items-start max-lg:gap-1">
<span className="text-blue-600 font-bold text-xl">
{formatPrice(
item.product.prices.length !== 0
? Math.max(
...item.product.prices.map((e) =>
Number(e.price),
),
)
: 0,
true,
)}
</span>
</div>
<div className="flex items-center border border-gray-300 rounded-lg w-max">
<button
onClick={() =>
handleQuantityChange(
String(item.id),
Number(quantities[item.id]) - 1,
)
<div className="w-24 h-40 bg-gray-100 rounded-lg flex-shrink-0 overflow-hidden">
<Image
src={
item.product.images.length > 0
? item.product.images[0].image.includes(BASE_URL)
? item.product.images[0].image
: BASE_URL + item.product.images[0].image
: ProductBanner
}
className="p-2 cursor-pointer transition rounded-lg"
>
<Minus className="w-4 h-4" />
</button>
<Input
value={quantities[item.id]}
onChange={(e) => {
const val = e.target.value.replace(/\D/g, ''); // faqat raqam
setQuantities((prev) => ({
...prev,
[item.id]: val,
}));
// Debounce bilan update
const valNum = Number(val);
if (!isNaN(valNum))
handleQuantityChange(String(item.id), valNum);
}}
type="text"
className="w-16 text-center"
alt={item.product.name}
width={500}
height={500}
unoptimized
className="object-cover"
style={{ width: '100%', height: '100%' }}
/>
</div>
<button
onClick={() =>
handleQuantityChange(
String(item.id),
Number(quantities[item.id]) + 1,
)
}
className="p-2 cursor-pointer transition rounded-lg"
>
<Plus className="w-4 h-4" />
</button>
<div className="flex-1">
<h3 className="font-semibold text-lg mb-1">
{item.product.name}
</h3>
<div className="flex items-center gap-2 mb-3 max-lg:flex-col max-lg:items-start max-lg:gap-1">
<span className="text-blue-600 font-bold text-xl">
{formatPrice(
item.product.prices.length !== 0
? Math.max(
...item.product.prices.map((e) =>
Number(e.price),
),
)
: 0,
true,
)}
</span>
<span className="text-sm text-gray-500">
/{measurementDisplay}
</span>
</div>
{/* O'lchov ko'rsatkichi */}
<p className="text-sm text-gray-500 mb-2">
{t('Miqdor')}: {quantities[item.id]} {measurementDisplay}
</p>
<div className="flex items-center border border-gray-300 rounded-lg w-max">
<button
onClick={() =>
handleQuantityChange(
String(item.id),
Number(quantities[item.id]) - 1,
)
}
className="p-2 cursor-pointer transition rounded-lg hover:bg-gray-50"
>
<Minus className="w-4 h-4" />
</button>
<div className="flex items-center gap-1 px-2">
<Input
value={quantities[item.id]}
onChange={(e) => {
const val = e.target.value.replace(/\D/g, '');
const valNum = Number(val);
setQuantities((prev) => ({
...prev,
[item.id]: val,
}));
if (!isNaN(valNum))
handleQuantityChange(String(item.id), valNum);
}}
type="text"
className="w-14 text-center border-none p-0"
/>
<span className="text-xs text-gray-500 whitespace-nowrap">
{measurementDisplay}
</span>
</div>
<button
onClick={() =>
handleQuantityChange(
String(item.id),
Number(quantities[item.id]) + 1,
)
}
className="p-2 cursor-pointer transition rounded-lg hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed"
>
<Plus className="w-4 h-4" />
</button>
</div>
</div>
</div>
</div>
))}
);
})}
</div>
</div>