-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
188 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//* IMPORT | ||
import Border from '@/src/components/Border'; | ||
import BackButton from '@/src/components/buttons/BackButton'; | ||
import CartSection from '@/src/components/sections/CartSection'; | ||
|
||
const CartsPage = () => { | ||
return ( | ||
<> | ||
<BackButton /> | ||
<Border /> | ||
<CartSection /> | ||
</> | ||
); | ||
}; | ||
|
||
export default CartsPage; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use client'; | ||
|
||
//* LIB | ||
import Image from 'next/image'; | ||
import { useRouter } from 'next/navigation'; | ||
import { useState } from 'react'; | ||
import { toast } from 'react-hot-toast'; | ||
|
||
//* IMPORT | ||
import cn from '../../helpers/cn'; | ||
import { displayNumbers } from '../../helpers/numbers'; | ||
import { useCartContext } from '../../providers/CartContextProvider'; | ||
import { CartItem } from '../../types/types'; | ||
import Button from '../buttons/Button'; | ||
|
||
export interface CartCardProps { | ||
item: CartItem; | ||
disableAction?: boolean; | ||
} | ||
|
||
interface Props extends CartCardProps { | ||
index: number; | ||
} | ||
const CartCard = ({ item, index, disableAction = false }: Props) => { | ||
const { cartItems, setCartItems } = useCartContext(); | ||
const router = useRouter(); | ||
|
||
const [isDeleting, setIsDeleting] = useState(false); | ||
|
||
const handleRemove = () => { | ||
setIsDeleting(true); | ||
setCartItems([...cartItems.filter((_, idx) => idx !== index)]); | ||
setTimeout(() => { | ||
setIsDeleting(false); | ||
toast.success('Item successfully removed!'); | ||
}, 200); | ||
}; | ||
|
||
return ( | ||
<div className="p-4 border border-zinc-300 bg-gradient-to-tl from-slate-200 grid sm:grid-cols-4 grid-cols-3 mt-2 gap-2"> | ||
<div className="col-span-1"> | ||
<Image src={item.image} alt={item.id} width={150} height={150} className="w-auto h-auto" /> | ||
</div> | ||
<div className="sm:col-span-3 col-span-2"> | ||
<p | ||
onClick={() => !disableAction && router.push(`/products/${item.id}`)} | ||
className={cn( | ||
'sm:text-lg text-[16px] font-black ease-in duration-75', | ||
!disableAction && 'hover:underline hover:text-red-800 cursor-pointer' | ||
)} | ||
> | ||
{item.name} | ||
</p> | ||
<p className="text-[14px] font-medium">US M{item.size}</p> | ||
<p className="text-md font-bold text-red-800">${displayNumbers(item.price)}</p> | ||
{!disableAction && ( | ||
<div className="flex justify-end"> | ||
<Button onClick={() => handleRemove()} isLoading={isDeleting} color="red" className="py-2 px-6"> | ||
Remove | ||
</Button> | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CartCard; |
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,69 @@ | ||
'use client'; | ||
|
||
//* LIB | ||
import { useRouter } from 'next/navigation'; | ||
import { useMemo, useState } from 'react'; | ||
import { toast } from 'react-hot-toast'; | ||
import { IoArrowForwardOutline } from 'react-icons/io5'; | ||
|
||
//* IMPORT | ||
import { displayNumbers } from '../../helpers/numbers'; | ||
import { useCartContext } from '../../providers/CartContextProvider'; | ||
import { useUserContext } from '../../providers/UserProvider'; | ||
import Button from '../buttons/Button'; | ||
import CartCard from '../cards/CartCard'; | ||
import Loader from '../loaders/Loader'; | ||
import NotFoundText from '../NotFoundText'; | ||
|
||
const CartSection = () => { | ||
const { user } = useUserContext(); | ||
const { cartItems } = useCartContext(); | ||
const [isRedirecting] = useState(false); | ||
|
||
const router = useRouter(); | ||
|
||
const totalPrice = useMemo(() => cartItems.reduce((acc, curr) => acc + (curr?.price ?? 0), 0), [cartItems]); | ||
|
||
const handleCheckout = () => { | ||
if (!user || !user.email) toast.error('Please sign-in before checking out'); | ||
}; | ||
|
||
if (isRedirecting) return <Loader />; | ||
|
||
return ( | ||
<div className="mx-auto max-w-4xl w-full py-4 sm:px-0 px-2"> | ||
<h2 className="text-2xl font-black mb-4">Your Cart ({cartItems.length}):</h2> | ||
{cartItems.length > 0 ? ( | ||
<> | ||
{cartItems.map((item, idx) => ( | ||
<CartCard index={idx} key={item.id + idx} item={item} /> | ||
))} | ||
|
||
<div className="w-full flex flex-col items-end my-10"> | ||
<p className="font-medium mb-2">Your Total</p> | ||
<h2 className="text-4xl font-black mb-4">${displayNumbers(totalPrice)}</h2> | ||
<div className="flex flex-row items-center gap-2"> | ||
<Button onClick={() => router.push('/products')} color="secondary" className="px-6 py-3"> | ||
Continue Shopping | ||
</Button> | ||
|
||
<Button | ||
onClick={() => handleCheckout()} | ||
color="primary" | ||
className="px-6 py-3" | ||
localLoaderOnClick={false} | ||
> | ||
Checkout | ||
<IoArrowForwardOutline className="text-xl ml-2" /> | ||
</Button> | ||
</div> | ||
</div> | ||
</> | ||
) : ( | ||
<NotFoundText>No Items.</NotFoundText> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default CartSection; |
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