diff --git a/src/pages/UserViewCart.tsx b/src/pages/UserViewCart.tsx index 9c16c79a..a5d362a4 100644 --- a/src/pages/UserViewCart.tsx +++ b/src/pages/UserViewCart.tsx @@ -1,9 +1,9 @@ /* eslint-disable */ -import React, { useEffect, useState } from "react"; -import { Meta } from "../components/Meta"; -import { useAppDispatch, useAppSelector } from "../store/store"; -import { PuffLoader, PulseLoader } from "react-spinners"; -import { toast } from "react-toastify"; +import React, { useEffect, useState } from 'react'; +import { Meta } from '../components/Meta'; +import { useAppDispatch, useAppSelector } from '../store/store'; +import { PuffLoader, PulseLoader } from 'react-spinners'; +import { toast } from 'react-toastify'; import { checkout, getUserCarts, @@ -15,7 +15,7 @@ import { createSessionStripe, updateCartStatus, userSaveOrder, -} from "../store/features/carts/cartSlice"; +} from '../store/features/carts/cartSlice'; import { FaCheckSquare, FaMinus, @@ -24,21 +24,20 @@ import { FaTrash, FaGift, FaShippingFast, -} from "react-icons/fa"; -import { GiBroom } from "react-icons/gi"; -import { useNavigate } from "react-router-dom"; -import Dialog from "@mui/material/Dialog"; -import DialogActions from "@mui/material/DialogActions"; -import DialogContent from "@mui/material/DialogContent"; -import DialogContentText from "@mui/material/DialogContentText"; -import DialogTitle from "@mui/material/DialogTitle"; -import Product from "../components/product/Product"; -import { Box, LinearProgress } from "@mui/material"; -import Button from "@mui/material/Button"; -import Dispatch from "react"; -import { fetchUserProfile } from "../store/features/user/userSlice"; -import { useLocation } from "react-router-dom"; -import { Empty } from "antd"; +} from 'react-icons/fa'; +import { GiBroom } from 'react-icons/gi'; +import { useNavigate } from 'react-router-dom'; +import Dialog from '@mui/material/Dialog'; +import DialogActions from '@mui/material/DialogActions'; +import DialogContent from '@mui/material/DialogContent'; +import DialogContentText from '@mui/material/DialogContentText'; +import DialogTitle from '@mui/material/DialogTitle'; +import Product from '../components/product/Product'; +import { Box, LinearProgress } from '@mui/material'; +import Button from '@mui/material/Button'; +import Dispatch from 'react'; +import { fetchUserProfile } from '../store/features/user/userSlice'; +import { useLocation } from 'react-router-dom'; const UserViewCart: React.FC = () => { const dispatch = useAppDispatch(); @@ -57,8 +56,8 @@ const UserViewCart: React.FC = () => { const [open, setOpen] = useState(false); const [cartToPay, setCartToPay] = useState(null); const [amountToPay, setAmountToPay] = useState(null); - const [stripePrice, setStripePrice] = useState(""); - const [currentEndpoint, setCurrentEndpoint] = useState(""); + const [stripePrice, setStripePrice] = useState(''); + const [currentEndpoint, setCurrentEndpoint] = useState(''); const navigate = useNavigate(); const location = useLocation(); @@ -74,20 +73,20 @@ const UserViewCart: React.FC = () => { setIsLoading(true); const response = await dispatch(getUserCarts()); const response1 = await dispatch(getUserCarts()).unwrap(); - if (response.payload === "Not authorized") { + if (response.payload === 'Not authorized') { setIsLoggedOut(true); - toast.error("Please login first"); - navigate("/login"); + toast.error('Please login first'); + navigate('/login'); } setCartResponseData(response1.data); setIsLoading(false); } catch (error: any) { - if (error === "Not authorized") { + if (error === 'Not authorized') { setIsLoggedOut(true); - toast.error("Please login first"); - navigate("/login"); + toast.error('Please login first'); + navigate('/login'); } - console.error("Error fetching carts:", error); + console.error('Error fetching carts:', error); setIsLoading(false); setIsError(true); toast.error(error.message); @@ -109,12 +108,12 @@ const UserViewCart: React.FC = () => { const response = await dispatch(checkout(cartId)); if (!response.payload) { throw new Error( - "Checkout failed, Check your internet connection or try again later" + 'Checkout failed, Check your internet connection or try again later' ); } - localStorage.setItem("cartToPay", cartId); + localStorage.setItem('cartToPay', cartId); - localStorage.setItem("productsToSave", JSON.stringify(productsArr)); + localStorage.setItem('productsToSave', JSON.stringify(productsArr)); const totalCartAmount = response.payload.data.totalAmount; const array = cartResponseData.carts[index]; @@ -127,14 +126,14 @@ const UserViewCart: React.FC = () => { ); setTotalProductPrice(totalProductPrice); - const names = productsArr.map((product) => product.name).join(", "); + const names = productsArr.map((product) => product.name).join(', '); const descriptions = productsArr .map((product) => product.description) - .join(", "); + .join(', '); const image1 = productsArr[0]?.image; const image2 = productsArr[1]?.image; const shopidToSave: any = productsArr[0]?.shopId; - localStorage.setItem("shopIdToSave", shopidToSave); + localStorage.setItem('shopIdToSave', shopidToSave); const unit_amount = Math.round(totalCartAmount * 100); @@ -150,16 +149,16 @@ const UserViewCart: React.FC = () => { ); console.log(stripeProduct); localStorage.setItem( - "stripePrice", + 'stripePrice', stripeProduct.payload.data.product.default_price ); setCheckoutSuccess(true); - toast.success("Checkout is done Successfully"); + toast.success('Checkout is done Successfully'); } catch (error) { - console.error("Checkout failed", error); - toast.error("Checkout failed"); + console.error('Checkout failed', error); + toast.error('Checkout failed'); } finally { setIsPreloader(false); } @@ -170,19 +169,17 @@ const UserViewCart: React.FC = () => { setIsPreloader(true); const profile: any = await dispatch(fetchUserProfile()); const data = { - successUrl: - "https://e-commerce-ninja-fn-staging.netlify.app/shopping-cart?success", - cancelUrl: - "https://e-commerce-ninja-fn-staging.netlify.app/shopping-cart?cancel", + successUrl: 'https://e-commerce-ninja-fn-staging.netlify.app/shopping-cart?success', + cancelUrl: 'https://e-commerce-ninja-fn-staging.netlify.app/shopping-cart?cancel', customerEmail: profile.payload.email, - price: localStorage.getItem("stripePrice"), + price: localStorage.getItem('stripePrice'), }; const response = await dispatch(createSessionStripe(data)); const url = response.payload.data.session.url; window.location.href = url; } catch (error) { - console.error("Checkout failed", error); - toast.error("Payment initialization failed, try again later"); + console.error('Checkout failed', error); + toast.error('Payment initialization failed, try again later'); } finally { setIsPreloader(false); } @@ -190,24 +187,24 @@ const UserViewCart: React.FC = () => { const checkPayFailOrSuccess = async () => { const params = window.location.search.slice(1); - if (params === "success") { + if (params === 'success') { setIsPreloader(true); - const cartId = localStorage.getItem("cartToPay"); - const products = localStorage.getItem("productsToSave"); - const shopId = localStorage.getItem("shopIdToSave"); + const cartId = localStorage.getItem('cartToPay'); + const products = localStorage.getItem('productsToSave'); + const shopId = localStorage.getItem('shopIdToSave'); console.log("Caert id", cartId); - console.log("Product ", products); - console.log("Shop id ", shopId); + console.log("Product ", products) + console.log("Shop id ", shopId) if (!cartId || !products || !shopId) { - navigate("/shopping-cart"); - toast.error("Unknown error occurred saving order"); + navigate('/shopping-cart'); + toast.error('Unknown error occurred saving order'); return; } const data = { cartId: cartId, - status: "Paid", + status: 'Paid', }; - + const response = await dispatch(getUserCarts()); const response1 = await dispatch(getUserCarts()).unwrap(); setCartResponseData(response1.data); @@ -215,20 +212,20 @@ const UserViewCart: React.FC = () => { const order = await dispatch( userSaveOrder({ cartId: cartId, - paymentMethodId: "Stripe", + paymentMethodId: 'Stripe', products: products, shopId: shopId, }) ); - toast.success("Cart Payment successful"); - localStorage.removeItem("cartToPay"); - localStorage.removeItem("productsToSave"); - localStorage.removeItem("shopIdToSave"); - navigate("/my-orders"); + toast.success('Cart Payment successful'); + localStorage.removeItem('cartToPay'); + localStorage.removeItem('productsToSave'); + localStorage.removeItem('shopIdToSave'); + navigate('/my-orders'); setIsPreloader(false); - } else if (params === "cancel") { - toast.error("Payment cancelled successfully"); - navigate("/shopping-cart"); + } else if (params === 'cancel') { + toast.error('Payment cancelled successfully'); + navigate('/shopping-cart'); setIsPreloader(false); } }; @@ -259,7 +256,7 @@ const UserViewCart: React.FC = () => { if (!cartResponseData || cartResponseData?.carts?.length < 1) { return (
- +

Your cart is empty.

); } @@ -286,12 +283,12 @@ const UserViewCart: React.FC = () => { toast.error(response.message); } } catch (error: any) { - if (error === "Not authorized") { - localStorage.setItem("pendingCartProduct", productId); - toast.error("Please login first"); - navigate("/login"); + if (error === 'Not authorized') { + localStorage.setItem('pendingCartProduct', productId); + toast.error('Please login first'); + navigate('/login'); } else { - toast.error("Something went wrong. Please try again later."); + toast.error('Something went wrong. Please try again later.'); } } finally { setIsLoading(false); @@ -314,8 +311,8 @@ const UserViewCart: React.FC = () => { await dispatch(clearCarts()).unwrap(); setIsPreloader(false); await fetchCarts(); - toast.success("Cart cleared successfully"); - navigate("/shopping-cart"); + toast.success('Cart cleared successfully'); + navigate('/shopping-cart'); handleClose(); }; @@ -331,10 +328,10 @@ const UserViewCart: React.FC = () => { ); setCartResponseData({ ...cartResponseData, carts: remainingCarts }); setIsPreloader(false); - toast.success("Cart cleared successfully"); + toast.success('Cart cleared successfully'); } catch (error) { - console.error("Error clearing cart:", error); - toast.error("Failed to clear the cart"); + console.error('Error clearing cart:', error); + toast.error('Failed to clear the cart'); } finally { setIsPreloader(false); } @@ -354,9 +351,9 @@ const UserViewCart: React.FC = () => { const response = await dispatch(clearCartProduct({ cartId, productId })); const response1 = await dispatch(getUserCarts()).unwrap(); setCartResponseData(response1.data); - toast.success("product cleared successfully"); + toast.success('product cleared successfully'); } catch (error) { - toast.error("Failed to clear the product "); + toast.error('Failed to clear the product '); throw error; } finally { setIsPreloader(false); @@ -373,12 +370,12 @@ const UserViewCart: React.FC = () => { {isPreloader && (
- + @@ -404,7 +401,7 @@ const UserViewCart: React.FC = () => {

Shopping Cart

- {cart.status === "Paid" ? ( + {cart.status === 'Paid' ? ( @@ -415,7 +412,7 @@ const UserViewCart: React.FC = () => { } className={`checkout-btn`} > - {"Checkout"} + {'Checkout'} )} @@ -447,14 +444,11 @@ const UserViewCart: React.FC = () => { {product.discount}
{product.price} RWF
-
+
{product.description}
- {cart.status !== "Paid" && ( + {cart.status !== 'Paid' && (
@@ -533,7 +527,7 @@ const UserViewCart: React.FC = () => { {arrayOfProduct.products.map((product, index) => (
-
{product.name} :
+
{product.name}:
{product.price} RWF
))} @@ -557,12 +551,12 @@ const UserViewCart: React.FC = () => { aria-describedby="alert-dialog-description" > - {"Delete all Carts"} + {'Delete all Carts'} Are you sure you want to delete all Carts ? @@ -571,12 +565,12 @@ const UserViewCart: React.FC = () => {