-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from atlp-rwanda/bg-fix-final-dem
Removed chat, tried to fix enable2FA, and handled success succeess page
- Loading branch information
Showing
47 changed files
with
2,155 additions
and
355 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
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 |
---|---|---|
@@ -1,67 +1,84 @@ | ||
import React, { useEffect, useState } from "react" | ||
import Navbar from "../navbar/Navbar" | ||
import CartItem from "./CartItem" | ||
import { Link } from "react-router-dom" | ||
import { ICartProduct, ICartsHookResponse, ICartsResponse } from "../../utils/schemas" | ||
import { useGetCartsQuery } from "../../services/cartApi" | ||
import React, { useEffect, useState } from 'react'; | ||
import Navbar from '../navbar/Navbar'; | ||
import CartItem from './CartItem'; | ||
import { Link } from 'react-router-dom'; | ||
import { ICartProduct, ICartsHookResponse, ICartsResponse } from '../../utils/schemas'; | ||
import { useGetCartsQuery } from '../../services/cartApi'; | ||
|
||
const Cart: React.FC = () => { | ||
const { data: carts, isLoading, isSuccess, isError, error } = useGetCartsQuery<ICartsResponse>() as ICartsHookResponse; | ||
const [totalPrice, setTotalPrice] = useState(0) | ||
const [items, setItems] = useState(0) | ||
const { | ||
data: carts, | ||
isLoading, | ||
isSuccess, | ||
isError, | ||
error, | ||
} = useGetCartsQuery<ICartsResponse>() as ICartsHookResponse; | ||
const [totalPrice, setTotalPrice] = useState(0); | ||
const [items, setItems] = useState(0); | ||
|
||
useEffect(() => { | ||
if (isSuccess && carts) { | ||
// Calculate total price | ||
const calculatedTotalPrice = carts.cartProducts.reduce((total: number, cart: ICartProduct) => { | ||
const pricePerItem = cart.sizes[0]?.price || 0; | ||
const itemTotalPrice = pricePerItem * cart.quantity; | ||
return total + itemTotalPrice; | ||
}, 0); | ||
useEffect(() => { | ||
if (isSuccess && carts) { | ||
// Calculate total price | ||
const calculatedTotalPrice = carts.cartProducts.reduce((total: number, cart: ICartProduct) => { | ||
const pricePerItem = cart.sizes[0]?.price || 0; | ||
const itemTotalPrice = pricePerItem * cart.quantity; | ||
return total + itemTotalPrice; | ||
}, 0); | ||
|
||
// Update total price state | ||
setTotalPrice(calculatedTotalPrice); | ||
// Update total price state | ||
setTotalPrice(calculatedTotalPrice); | ||
|
||
// Optionally calculate the number of items if needed | ||
const totalItems = carts.cartProducts.length; | ||
setItems(totalItems); | ||
} | ||
}, [isSuccess, carts]); | ||
// Optionally calculate the number of items if needed | ||
const totalItems = carts.cartProducts.length; | ||
setItems(totalItems); | ||
} | ||
}, [isSuccess, carts]); | ||
|
||
let content; | ||
if (isLoading) { | ||
content = <div>Loading</div> | ||
} else if (isSuccess) { | ||
const renderedCart = carts?.cartProducts.map((cart, index) => { | ||
const price = cart.sizes[0]?.price | ||
return <CartItem key={index} {...cart} price={price} /> | ||
let content; | ||
if (isLoading) { | ||
content = <div>Loading</div>; | ||
} else if (isSuccess) { | ||
const renderedCart = | ||
carts?.cartProducts?.length === 0 ? ( | ||
<p>No item in the cart</p> | ||
) : ( | ||
carts?.cartProducts.map((cart, index) => { | ||
const price = cart.sizes[0]?.price; | ||
return <CartItem key={index} {...cart} price={price} />; | ||
}) | ||
content = renderedCart | ||
} else if (isError) { | ||
content = <div>{error?.toString()}</div> | ||
} | ||
return ( | ||
<div className="w-full min-h-screen overflow-x-hidden font-roboto flex flex-col 2xl:items-center"> | ||
<Navbar /> | ||
<div className="w-full px-3 pt-5 flex flex-col gap-2 md:p-4 xl:px-10 2xl:w-[1440px]"> | ||
<h1 className="leading-none font-semibold text-lg subpixel-antialiased tracking-wide">Shopping Cart ({items})</h1> | ||
<p className="leading-none subpixel-antialiased text-xs tracking-wide font-light hidden md:block">Your cart contains {items} items and comes to a total of ${totalPrice}</p> | ||
<div className="flex flex-col md:flex-row-reverse lg:gap-10 xl:gap-12"> | ||
<div className="flex flex-col gap-4 px-3 py-2 pt-4 md:flex-grow lg:w-72 lg:flex-grow-0 xl:flex-grow-0 xl:w-80"> | ||
<div className="flex justify-between text-sm font-medium"> | ||
<span className="md:font-semibold md:text-lg">Total:</span> | ||
<span className="md:font-semibold md:text-lg">${totalPrice}</span> | ||
</div> | ||
<Link to="/checkoutbag" | ||
className="leading-none bg-greenColor hover:bg-[#1a6461] rounded-full px-5 py-3 text-whiteColor font-medium text-xs transition-all delay-75 ease-in cursor-pointer text-center" | ||
>Checkout</Link> | ||
</div> | ||
<div className="bg-[#f7f7f7] flex flex-col py-3 mt-2 md:flex-grow"> | ||
{content} | ||
</div> | ||
</div> | ||
); | ||
content = renderedCart; | ||
} else if (isError) { | ||
content = <div>{error?.toString()}</div>; | ||
} | ||
|
||
return ( | ||
<div className='w-full min-h-screen overflow-x-hidden font-roboto flex flex-col 2xl:items-center'> | ||
<Navbar /> | ||
<div className='w-full px-3 pt-5 flex flex-col gap-2 md:p-4 xl:px-10 2xl:w-[1440px]'> | ||
<h1 className='leading-none font-semibold text-lg subpixel-antialiased tracking-wide'> | ||
Shopping Cart ({items}) | ||
</h1> | ||
<p className='leading-none subpixel-antialiased text-xs tracking-wide font-light hidden md:block'> | ||
Your cart contains {items} items and comes to a total of ${totalPrice} | ||
</p> | ||
<div className='flex flex-col md:flex-row-reverse lg:gap-10 xl:gap-12'> | ||
<div className='flex flex-col gap-4 px-3 py-2 pt-4 md:flex-grow lg:w-72 lg:flex-grow-0 xl:flex-grow-0 xl:w-80'> | ||
<div className='flex justify-between text-sm font-medium'> | ||
<span className='md:font-semibold md:text-lg'>Total:</span> | ||
<span className='md:font-semibold md:text-lg'>${totalPrice}</span> | ||
</div> | ||
<Link | ||
to='/checkoutbag' | ||
className='leading-none bg-greenColor hover:bg-[#1a6461] rounded-full px-5 py-3 text-whiteColor font-medium text-xs transition-all delay-75 ease-in cursor-pointer text-center' | ||
> | ||
Checkout | ||
</Link> | ||
</div> | ||
<div className='bg-[#f7f7f7] flex flex-col py-3 mt-2 md:flex-grow'>{content}</div> | ||
</div> | ||
) | ||
} | ||
export default Cart | ||
</div> | ||
</div> | ||
); | ||
}; | ||
export default Cart; |
Oops, something went wrong.