Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 3x 3x 3x 3x 13x 13x 13x 1x 13x 13x 13x 1x 13x 13x 13x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x | import React from "react";
import { useDispatch } from "react-redux";
import { AppDispatch } from "@/redux/store";
import { TrashIcon } from "@/components";
import { ProductThumbnail, PriceDisplay, Button } from "@/components/ui";
import { ProductLink } from "@/components/ui/links";
import { updateCartItemQuantity } from "@/redux/features/cartSlice";
import { useConfirm } from "@/hooks/useConfirm";
import { formatCurrency } from "@/lib/core";
import type { CartItem } from "@/redux/features/cartSlice";
import type { ActionCreatorWithPayload } from "@reduxjs/toolkit";
interface SingleItemProps {
item: CartItem;
removeItemFromCart: ActionCreatorWithPayload<number>;
}
const SingleItem: React.FC<SingleItemProps> = ({ item, removeItemFromCart }) => {
const dispatch = useDispatch<AppDispatch>();
const { confirm, ConfirmDialog } = useConfirm();
const handleRemoveFromCart = async () => {
const confirmed = await confirm({
title: "Remove from Cart",
message: `Are you sure you want to remove ${item.quantity} ${item.quantity === 1 ? 'item' : 'items'} of "${item.title}" from your cart?`,
confirmText: "Remove",
cancelText: "Cancel",
variant: "danger"});
if (confirmed) {
dispatch(removeItemFromCart(item.id));
}
};
const handleQuantityChange = async (newQuantity: number) => {
if (newQuantity < 1) {
// Confirm removal via confirmation dialog
const confirmed = await confirm({
title: "Remove from Cart",
message: `Are you sure you want to remove ${item.quantity} ${item.quantity === 1 ? 'item' : 'items'} of "${item.title}" from your cart?`,
confirmText: "Remove",
cancelText: "Cancel",
variant: "danger"});
if (confirmed) {
dispatch(removeItemFromCart(item.id));
}
} else {
dispatch(updateCartItemQuantity({ id: item.id, quantity: newQuantity }));
}
};
const handleIncrement = () => {
handleQuantityChange(item.quantity + 1);
};
const handleDecrement = () => {
handleQuantityChange(item.quantity - 1);
};
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const value = parseInt(e.target.value);
if (!isNaN(value) && value >= 0) {
handleQuantityChange(value);
}
};
const lineTotal = item.discountedPrice * item.quantity;
return (
<div className="flex flex-col gap-3 border-b border-gray-3 dark:border-gray-700 pb-4">
<div className="flex items-start justify-between gap-5">
<div className="flex items-start gap-4">
<div className="flex items-center justify-center rounded-[10px] bg-gray-3 dark:bg-gray-700 min-w-[90px] max-w-[90px] h-22.5">
<ProductThumbnail
src={item.imgs?.thumbnails[0]}
alt={item.title}
size={90}
/>
</div>
<div className="flex-1">
<h3 className="font-medium text-dark dark:text-gray-100 mb-1">
<ProductLink product={{ id: item.id, title: item.title }}>
{item.title}
</ProductLink>
</h3>
<PriceDisplay
price={item.price}
discountedPrice={item.discountedPrice}
size="sm"
/>
</div>
</div>
<Button
onClick={handleRemoveFromCart}
aria-label="Remove item from cart"
variant="ghost"
size="sm"
className="flex items-center justify-center rounded-lg min-w-[38px] h-9.5 bg-gray-2 dark:bg-gray-700 border border-gray-3 dark:border-gray-600 text-dark dark:text-gray-300 ease-out duration-200 hover:bg-red-light-6 hover:border-red-light-4 hover:text-red"
>
<TrashIcon />
</Button>
</div>
<div className="flex items-center justify-between gap-4 ml-[106px]">
<div className="flex items-center gap-2">
<span className="text-sm text-dark-5 dark:text-gray-400">Qty:</span>
<div className="flex items-center border border-gray-3 dark:border-gray-600 rounded-md">
<Button
variant="ghost"
size="sm"
onClick={handleDecrement}
className="px-3 py-1 min-h-0 rounded-none rounded-l-md text-dark dark:text-gray-200 hover:bg-gray-1 dark:hover:bg-gray-700 border-r border-gray-3 dark:border-gray-600"
aria-label="Decrease quantity"
>
-
</Button>
<input
type="number"
value={item.quantity}
onChange={handleInputChange}
min="1"
className="w-14 text-center py-1 focus:outline-none bg-transparent dark:text-gray-200"
aria-label="Quantity"
/>
<Button
variant="ghost"
size="sm"
onClick={handleIncrement}
className="px-3 py-1 min-h-0 rounded-none rounded-r-md text-dark dark:text-gray-200 hover:bg-gray-1 dark:hover:bg-gray-700 border-l border-gray-3 dark:border-gray-600"
aria-label="Increase quantity"
>
+
</Button>
</div>
</div>
<div className="text-right">
<p className="text-xs text-dark-5 dark:text-gray-400 mb-0.5">Line Total:</p>
<p className="font-medium text-dark dark:text-gray-100">{formatCurrency(lineTotal)}</p>
</div>
</div>
<ConfirmDialog />
</div>
);
};
export default SingleItem;
|