diff --git a/src/components/Cart/CartContents.component.tsx b/src/components/Cart/CartContents.component.tsx index 7f6e913b3..86a69a3d1 100644 --- a/src/components/Cart/CartContents.component.tsx +++ b/src/components/Cart/CartContents.component.tsx @@ -1,19 +1,14 @@ -/*eslint complexity: ["error", 20]*/ -// Imports import { useContext, useEffect } from 'react'; import { useMutation, useQuery } from '@apollo/client'; import Link from 'next/link'; +import Image from 'next/image'; import { useRouter } from 'next/router'; import { v4 as uuidv4 } from 'uuid'; -// State import { CartContext } from '@/stores/CartProvider'; - -// Components import Button from '@/components/UI/Button.component'; import LoadingSpinner from '../LoadingSpinner/LoadingSpinner.component'; -// Utils import { getFormattedCart, getUpdatedItems, @@ -21,45 +16,28 @@ import { IProductRootObject, } from '@/utils/functions/functions'; -// GraphQL import { GET_CART } from '@/utils/gql/GQL_QUERIES'; import { UPDATE_CART } from '@/utils/gql/GQL_MUTATIONS'; -/** - * Renders cart contents. - * @function CartContents - * @returns {JSX.Element} - Rendered component - */ const CartContents = () => { const router = useRouter(); - const { setCart } = useContext(CartContext); - const isCheckoutPage = router.pathname === '/kasse'; - // Get cart data query const { data, refetch } = useQuery(GET_CART, { notifyOnNetworkStatusChange: true, onCompleted: () => { - // Update cart in the localStorage. const updatedCart = getFormattedCart(data); - if (!updatedCart && !data.cart.contents.nodes.length) { - // Clear the localStorage if we have no remote cart - localStorage.removeItem('woocommerce-cart'); setCart(null); return; } - localStorage.setItem('woocommerce-cart', JSON.stringify(updatedCart)); - - // Update cart data in React Context. setCart(updatedCart); }, }); - // Update Cart Mutation. const [updateCart, { loading: updateCartProcessing }] = useMutation( UPDATE_CART, { @@ -76,11 +54,8 @@ const CartContents = () => { cartKey: string, products: IProductRootObject[], ) => { - if (products.length) { - // By passing the newQty to 0 in updateCart Mutation, it will remove the item. - const newQty = 0; - const updatedItems = getUpdatedItems(products, newQty, cartKey); - + if (products?.length) { + const updatedItems = getUpdatedItems(products, 0, cartKey); updateCart({ variables: { input: { @@ -90,9 +65,7 @@ const CartContents = () => { }, }); } - refetch(); - setTimeout(() => { refetch(); }, 3000); @@ -102,50 +75,46 @@ const CartContents = () => { refetch(); }, [refetch]); + const cartTotal = data?.cart?.total || '0'; + + const getUnitPrice = (subtotal: string, quantity: number) => { + const numericSubtotal = parseFloat(subtotal.replace(/[^0-9.-]+/g, '')); + return isNaN(numericSubtotal) + ? 'N/A' + : (numericSubtotal / quantity).toFixed(2); + }; + return ( -
-
- {data?.cart?.contents?.nodes.length ? ( - data.cart.contents.nodes.map((item: IProductRootObject) => ( -
-
- - Slett:
-
- - - -
-
- - Navn:
-
- - {item.product.node.name} - -
-
- - Antall:
-
- + alt={item.product.node.name} + layout="fill" + objectFit="cover" + className="rounded" + /> +
+
+

+ {item.product.node.name} +

+

+ kr {getUnitPrice(item.subtotal, item.quantity)} +

+
+
{ updateCartProcessing, ); }} + className="w-16 px-2 py-1 text-center border border-gray-300 rounded mr-2" /> - -
-
- - Total:
-
- - {item.subtotal} - + +
+
+

{item.subtotal}

+
+ ))} +
+
+
+ Subtotal: + {cartTotal}
- )) - ) : ( -

- Ingen produkter i handlekurven -

- )} - {updateCartProcessing && ( -
-
- Oppdaterer antall, vennligst vent ... - -
+ {!isCheckoutPage && ( +
+ + + +
+ )}
- )} - {!isCheckoutPage && data?.cart?.contents?.nodes.length ? ( -
- - - + + ) : ( +
+

+ Ingen produkter i handlekurven +

+ + + +
+ )} + {updateCartProcessing && ( +
+
+

Oppdaterer handlekurv...

+
- ) : null} -
-
+ + )} + ); }; diff --git a/src/components/Layout/Layout.component.tsx b/src/components/Layout/Layout.component.tsx index 9f43c36f5..9c6a62c00 100644 --- a/src/components/Layout/Layout.component.tsx +++ b/src/components/Layout/Layout.component.tsx @@ -56,7 +56,7 @@ const Layout = ({ children, title }: ILayoutProps) => { }, [refetch]); return ( -
+
diff --git a/src/components/Product/SingleProduct.component.tsx b/src/components/Product/SingleProduct.component.tsx index c67151bbb..234222f1c 100644 --- a/src/components/Product/SingleProduct.component.tsx +++ b/src/components/Product/SingleProduct.component.tsx @@ -115,7 +115,7 @@ const SingleProduct = ({ product }: IProductRootObject) => {