From b1c1643b04b526425473c38fee918ee55c45eacb Mon Sep 17 00:00:00 2001 From: Shivansh Date: Sun, 27 Oct 2024 04:25:22 +0530 Subject: [PATCH] update .env.example --- .env.example | 4 + CONTRIBUTING.md | 4 +- actions/cart-actions.tsx | 67 ++++++- actions/wishlist-actions.tsx | 30 +-- app/(Customer)/Cart/components/cart-item.tsx | 113 ++++++----- app/(Customer)/Cart/components/summary.tsx | 18 +- app/(Customer)/Cart/page.tsx | 85 +++----- .../WishList/components/wishlistItem.tsx | 152 ++++++++++++--- app/(Customer)/WishList/page.tsx | 7 +- app/(Customer)/layout.tsx | 15 +- .../[storeId]/components/ProductForm.tsx | 184 +++++++++++------- app/(Customer)/shops/[storeId]/page.tsx | 2 +- context/cartContext.tsx | 52 +++++ 13 files changed, 493 insertions(+), 240 deletions(-) create mode 100644 context/cartContext.tsx diff --git a/.env.example b/.env.example index b819c58..85b90d2 100644 --- a/.env.example +++ b/.env.example @@ -11,5 +11,9 @@ SMTP_USER=your email SMTP_PASS=you email app password (not gmail password, create an app password from gmail) NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME=your cloudinary id +NEXT_PUBLIC_CLOUDINARY_CLOUD_PRESET=your cloudinary preset NEXT_PUBLIC_BASE_URL=base url of the hosted website/development server + +NEXT_PUBLIC_BASE_Cover=url of default image (should be from cloudinary) + diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3a9f13b..2d0064e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -127,4 +127,6 @@ npm run build > > **PRs that have been open for a duration exceeding 45 days will be automatically closed, so please plan accordingly.** > ->**Additionally, PRs that are improperly linted or have a failing build will not be merged. Ensure that your code passes linting checks and builds successfully before submitting your PR.** \ No newline at end of file +>**Additionally, PRs that are improperly linted or have a failing build will not be merged. Ensure that your code passes linting checks and builds successfully before submitting your PR.** + +>**Don't forget to check .env.example for better insight of the environment variable** \ No newline at end of file diff --git a/actions/cart-actions.tsx b/actions/cart-actions.tsx index 0dffe48..fd9c0fc 100644 --- a/actions/cart-actions.tsx +++ b/actions/cart-actions.tsx @@ -1,15 +1,15 @@ "use server" import prismadb from "@/lib/prismadb"; -import { Prisma } from "@prisma/client"; -// import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library"; +import { Cart, Prisma } from "@prisma/client"; export async function CartPost( data: FormData - ): Promise<{ success: boolean; error?: string }> { + ): Promise<{ success: boolean; data?: Cart, error?: string }> { const productId = data.get("productid") as string; const userId = data.get("userid") as string; + console.log(productId,userId) try { const res = await prismadb.cart.create({ data: { @@ -18,8 +18,8 @@ export async function CartPost( }, }); - console.log(res); - return { success: true }; + // console.log(res); + return { success: true,data:res }; } catch (err) { if (err instanceof Prisma.PrismaClientKnownRequestError) { console.log("Prisma error in post cart item route:", err.message); @@ -28,7 +28,60 @@ export async function CartPost( error: err.message, }; } - console.log(err); + // console.log(err); return { success: false, error: "An unexpected error occurred." }; } - } \ No newline at end of file + } + + +export async function CartGetByUser( + userId: string +): Promise<{ success: boolean; data?: any; error?: string }> { + // console.log("contrl") + try { + const res = await prismadb.cart.findMany({ + where: { + userId, + }, + include: { + product: { + include: { + images: true, + }, + }, + }, + }); + + // console.log(res); + return { success: true, data: res }; + } catch (err) { + console.log(err); + + return { success: false, error: "An unexpected error occurred." }; + } +} + +export async function CartDeleteProduct( + Id: string, +): Promise<{ success: boolean; error?: string }> { + + // console.log(Id); + try { + const res = await prismadb.cart.deleteMany({ + where: { + id:Id + }, + }); + + if(res.count==0){ + return {success:false, error:"item does not exist on the wishlist"} + } + console.log(res); + // console.log(res) + return { success: true}; // Include success and return the data + } catch (err) { + // console.log(err); + + return { success: false, error: "An unexpected error occurred while deleting" }; + } +} \ No newline at end of file diff --git a/actions/wishlist-actions.tsx b/actions/wishlist-actions.tsx index e372cf1..fcf1fde 100644 --- a/actions/wishlist-actions.tsx +++ b/actions/wishlist-actions.tsx @@ -88,22 +88,22 @@ export async function WishlistDeleteProduct( } } -export async function WishlistSpecificEntry( - userId:string - ,productId:string): Promise<{ success: boolean; error?: string }> { - - const res=await prismadb.wishlist.findUnique({ - where:{ - userId_productId:{ - userId, - productId - } - } - }) +// export async function WishlistSpecificEntry( +// userId:string +// ,productId:string): Promise<{ success: boolean; error?: string }> { - // console.log(res); +// const res=await prismadb.wishlist.findUnique({ +// where:{ +// userId_productId:{ +// userId, +// productId +// } +// } +// }) - return {success:true} +// // console.log(res); + +// return {success:true} -} +// } diff --git a/app/(Customer)/Cart/components/cart-item.tsx b/app/(Customer)/Cart/components/cart-item.tsx index 4f6497f..779389c 100644 --- a/app/(Customer)/Cart/components/cart-item.tsx +++ b/app/(Customer)/Cart/components/cart-item.tsx @@ -1,58 +1,77 @@ "use client" -import { orderItemProps } from "@/app/(Customer)/MyOrders/components/orderItem"; import Image from "next/image"; +// import { cartProps } from "../page"; +import { Button } from "@/components/ui/button"; +import { X } from "lucide-react"; +import toast from "react-hot-toast"; +import { CartDeleteProduct } from "@/actions/cart-actions"; +import { cartProps, useCart } from "@/context/cartContext"; - -const CartItem:React.FC = ({ - item +interface cartItemProps{ + item:cartProps +} +const CartItem:React.FC = ({ + item, }) => { + const {handleRemove} = useCart(); + const onDelete=async()=>{ - // const cart=useCart(); - - // const onRemove=()=>{ - // // cart.removeItem(data.id); - // } + const res=await CartDeleteProduct(item.id); + + if(res.success){ + toast.success("removed from cart"); + handleRemove(item.id); + } + else{ + toast.error(res.error||"error occured"); + } + + } + return ( -
  • -
    - product image -
    -
    -
    - {/* } - /> */} -
    -
    -
    -

    - {item.name} -

    -
    -
    -

    - {/* haha */} - {item.variant} -

    -

    - {/* nunu */} - {item.count} -

    -
    -
    ₹{item.price}
    - -
    -
    -
  • +
  • + {/* Image Section */} +
    + {`${item.product.name} +
    + + {/* Product Details Section */} +
    +
    +

    + {item.product.name} +

    + + {/* Remove Button */} + +
    + + {/* Description */} +

    + {item.product.description} +

    + + {/* Price Section */} +
    + ₹{item.product.price} +
    +
    +
  • + ); } diff --git a/app/(Customer)/Cart/components/summary.tsx b/app/(Customer)/Cart/components/summary.tsx index 710f2bb..6fc9efc 100644 --- a/app/(Customer)/Cart/components/summary.tsx +++ b/app/(Customer)/Cart/components/summary.tsx @@ -2,16 +2,26 @@ import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; +import { useCart } from "@/context/cartContext"; +import { useSession } from "next-auth/react"; import { useState } from "react"; const Summary = () => { + const session=useSession() + console.log(session.data) const [userDetails, updateUserDetails] = useState({ - Fullname: "", + Fullname: session.data?.user.name, Address: "", Phone: "", - Email: "" + Email: session.data?.user.email }); + const {cartItems}=useCart(); + + const amount=cartItems.reduce((initial,value)=>{ + return initial+value.product.price + },0); + const handleInputChange = (field: keyof typeof userDetails) => (e: React.ChangeEvent) => { updateUserDetails({ ...userDetails, // Spread the previous state @@ -44,6 +54,7 @@ const Summary = () => { placeholder="Name" onChange={handleInputChange("Fullname")} // Call handleInputChange with the specific field // data={userDetails.Fullname} + value={userDetails.Fullname} /> { placeholder="Email" onChange={handleInputChange("Email")} // data={userDetails.Email} + value={userDetails.Email} /> @@ -70,7 +82,7 @@ const Summary = () => {
    Order Total
    {/* */} - ₹{566} + ₹{amount} + +
    { + e.preventDefault(); // Prevent default form submission + const target = e.target as HTMLFormElement; // Type assertion + const formData = new FormData(target); + onAddToCart(formData); // Call handleSubmit with FormData + }} + className="p-1" + > + + + +
    - } - // console.log(item); - return ( -
    - {item.name} -

    {item.name}

    -

    ₹{item.price}

    -
    - - -
    + + + + + + + +
    - ); - }; - - export default WishlistItem; - \ No newline at end of file + + ); +}; + +export default WishlistItem; diff --git a/app/(Customer)/WishList/page.tsx b/app/(Customer)/WishList/page.tsx index 7dd4925..5942093 100644 --- a/app/(Customer)/WishList/page.tsx +++ b/app/(Customer)/WishList/page.tsx @@ -4,7 +4,7 @@ import WishlistItem from "./components/wishlistItem"; import { useSession } from "next-auth/react"; import { WishlistGetByUser } from "@/actions/wishlist-actions"; import { useEffect, useState } from "react"; -import toast from "react-hot-toast"; +import toast, { Toaster } from "react-hot-toast"; import { Image, Product, Wishlist } from "@prisma/client"; // import { useTheme } from "@/context/themeProvider"; import { Spinner } from "@/components/ui/spinner"; @@ -79,7 +79,7 @@ const WishlistPage = () => { setWishlistItems((prevItems) => prevItems.filter((item) => item.id !== id)); }; - if (loading) { + if (loading||!userId) { return ; } @@ -96,7 +96,7 @@ const WishlistPage = () => { {wishlistItems.length > 0 ? (
    {wishlistItems.map((item) => ( - + ))}
    ) : ( @@ -110,6 +110,7 @@ const WishlistPage = () => { )} + ); diff --git a/app/(Customer)/layout.tsx b/app/(Customer)/layout.tsx index 45dff0b..0786ad6 100644 --- a/app/(Customer)/layout.tsx +++ b/app/(Customer)/layout.tsx @@ -2,6 +2,7 @@ import Footer from "@/components/footer"; import Navbar from "@/components/Navbar/navbar"; import { Spinner } from "@/components/ui/spinner"; +import { CartProvider } from "@/context/cartContext"; import { useSession } from "next-auth/react"; import { useEffect, useState } from "react"; @@ -26,14 +27,20 @@ export default function Layout({ }, [session, status]); if (loading) { - return
    ; + return ( +
    + +
    + ); } return ( <> - - {children} -