-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Adding cart state into local storage - Quantity change now possible from cart page - Delete item from cart
- Loading branch information
Showing
11 changed files
with
177 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/backend/web/web.client/web-app-new/src/components/cart/CartItem.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import Image from "next/image"; | ||
import { TrashIcon } from "@heroicons/react/24/solid"; | ||
import { useCart } from "@/context/CartContext"; | ||
import { CartItemQuantity } from "./CartItemQuantity"; | ||
|
||
// Type for the cart item | ||
type CartItemProps = { | ||
id: number; | ||
name: string; | ||
description: string; | ||
quantity: number; | ||
price: number; | ||
image: string; | ||
}; | ||
|
||
// A single cart item component | ||
export const CartItem: React.FC<CartItemProps> = ({ | ||
id, | ||
name, | ||
description, | ||
quantity, | ||
price, | ||
image, | ||
}) => { | ||
const { removeItem, setQuantity } = useCart(); | ||
return ( | ||
<div className="flex items-center justify-between border-b border-gray-200 py-4"> | ||
<div className="flex items-center"> | ||
<Image | ||
src={image} | ||
alt={name} | ||
quality={75} | ||
width={60} | ||
height={60} | ||
className="rounded-full" | ||
/> | ||
<div className="ml-4"> | ||
<h3 className="text-lg font-bold">{name}</h3> | ||
<p className="text-sm text-gray-600">{description}</p> | ||
<p className="text-sm font-bold">Total: ${price.toFixed(2)}</p> | ||
</div> | ||
</div> | ||
<div className="flex items-center"> | ||
<CartItemQuantity | ||
className="px-4 relative" | ||
quantity={quantity} | ||
onQuantityChange={(newQuantity: number) => { | ||
setQuantity(id, newQuantity); | ||
}} | ||
/> | ||
<button | ||
className="text-gray-500 hover:text-red-500" | ||
onClick={() => removeItem(id)} | ||
> | ||
<TrashIcon className="h-6 w-6" /> | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
}; |
40 changes: 40 additions & 0 deletions
40
src/backend/web/web.client/web-app-new/src/components/cart/CartItemQuantity.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
type Props = { | ||
onQuantityChange: (newQuantity: number) => void; | ||
quantity: number; | ||
className?: string; | ||
}; | ||
|
||
export const CartItemQuantity: React.FC<Props> = ({ | ||
quantity, | ||
onQuantityChange, | ||
className, | ||
}) => { | ||
const handleQuantityChange = (newQuantity: number) => { | ||
if (newQuantity > 0) { | ||
onQuantityChange(newQuantity); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className={className}> | ||
<button | ||
onClick={() => handleQuantityChange(quantity - 1)} | ||
className="text-md bg-gray-200 text-gray-600 px-2 py-1 rounded-l" | ||
> | ||
- | ||
</button> | ||
<input | ||
type="text" | ||
className="w-12 text-center border-t border-b" | ||
value={quantity} | ||
onChange={(e) => handleQuantityChange(parseInt(e.target.value) || 0)} | ||
/> | ||
<button | ||
onClick={() => handleQuantityChange(quantity + 1)} | ||
className="text-md bg-gray-200 text-gray-600 px-2 py-1 rounded-r" | ||
> | ||
+ | ||
</button> | ||
</div> | ||
); | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.