diff --git a/apps/web/src/app/_components/features/ProductCatalog.tsx b/apps/web/src/app/_components/features/ProductCatalog.tsx
index cc587e5..948f794 100755
--- a/apps/web/src/app/_components/features/ProductCatalog.tsx
+++ b/apps/web/src/app/_components/features/ProductCatalog.tsx
@@ -22,14 +22,6 @@ export default function ProductCatalog() {
const [query, setQuery] = useAtom(searchQueryAtom);
const router = useRouter();
- const utils = api.useUtils();
-
- const { mutate: addItem } = api.shoppingCart.addItem.useMutation({
- onSuccess: async () => {
- await utils.shoppingCart.getItems.invalidate();
- },
- });
-
// Using an infinite query to fetch products with pagination
const { data, fetchNextPage, hasNextPage, isFetchingNextPage } =
api.product.getProducts.useInfiniteQuery(
@@ -99,7 +91,6 @@ export default function ProductCatalog() {
variety={product.name}
price={product.price}
badgeText={product.strength}
- isAddingToShoppingCart={false} // Disable shopping cart action for now
onClick={() => accessProductDetails(product.id)} // Trigger add-to-cart action
/>
);
diff --git a/apps/web/src/app/_components/features/ProductDetails.tsx b/apps/web/src/app/_components/features/ProductDetails.tsx
index e841cd0..b155b8c 100644
--- a/apps/web/src/app/_components/features/ProductDetails.tsx
+++ b/apps/web/src/app/_components/features/ProductDetails.tsx
@@ -4,14 +4,17 @@ import Button from "@repo/ui/button";
import { ChatWithSeller } from "@repo/ui/chatWithSeller";
import { DataCard } from "@repo/ui/dataCard";
import PageHeader from "@repo/ui/pageHeader";
+import { useAtom, useAtomValue } from "jotai";
import Image from "next/image";
import { useRouter } from "next/navigation";
import { useState } from "react";
+import { addItemAtom, cartItemsAtom } from "~/store/cartAtom";
import { ProducerInfo } from "./ProducerInfo";
import { SelectionTypeCard } from "./SelectionTypeCard";
interface ProductDetailsProps {
product: {
+ id: number;
image: string;
name: string;
region: string;
@@ -29,22 +32,39 @@ export default function ProductDetails({ product }: ProductDetailsProps) {
const {
image,
name,
- region,
farmName,
roastLevel,
bagsAvailable,
price,
type,
- description,
process,
} = product;
const [quantity, setQuantity] = useState(1);
const [isLiked, setIsLiked] = useState(false);
const router = useRouter();
+ const [isAddingToCart, setIsAddingToCart] = useState(false);
+ const [, addItem] = useAtom(addItemAtom);
+ const items = useAtomValue(cartItemsAtom);
+ const cartItemsCount = items.reduce(
+ (total, item) => total + item.quantity,
+ 0,
+ );
const isSoldOut = type === "SoldOut";
const isFarmer = type === "Farmer";
+ const handleAddToCart = () => {
+ setIsAddingToCart(true);
+ addItem({
+ id: String(product.id),
+ name: product.name,
+ quantity: quantity,
+ price: product.price,
+ imageUrl: product.image,
+ });
+ setIsAddingToCart(false);
+ };
+
return (
@@ -52,7 +72,8 @@ export default function ProductDetails({ product }: ProductDetailsProps) {
title={
{name}
}
showBackButton
onBackClick={() => router.back()}
- hideCart={false}
+ showCart={true}
+ cartItemsCount={cartItemsCount}
rightActions={
)}
diff --git a/apps/web/src/app/_components/features/ProductList.tsx b/apps/web/src/app/_components/features/ProductList.tsx
index 6b8397b..eb5ca11 100644
--- a/apps/web/src/app/_components/features/ProductList.tsx
+++ b/apps/web/src/app/_components/features/ProductList.tsx
@@ -1,6 +1,7 @@
"use client";
-import { api } from "~/trpc/react";
+import { useAtom } from "jotai";
+import { addItemAtom } from "~/store/cartAtom";
interface Product {
id: number;
@@ -14,16 +15,16 @@ interface ProductListProps {
}
export default function ProductList({ products }: ProductListProps) {
- const utils = api.useUtils();
+ const [, addItem] = useAtom(addItemAtom);
- const { mutate: addToCart } = api.shoppingCart.addItem.useMutation({
- onSuccess: async () => {
- await utils.shoppingCart.getItems.invalidate();
- },
- });
-
- const handleAddToCart = (productId: number) => {
- addToCart({ cartId: "1", productId, quantity: 1 });
+ const handleAddToCart = (product: Product) => {
+ addItem({
+ id: String(product.id),
+ name: product.name,
+ quantity: 1,
+ price: product.price,
+ imageUrl: "/default-image.webp",
+ });
};
return (
@@ -38,7 +39,7 @@ export default function ProductList({ products }: ProductListProps) {
${product.price.toFixed(2)}
-