From 7ef27058d6c729f486c1a5057148b67b74a80d20 Mon Sep 17 00:00:00 2001 From: Kirtan Tank Date: Sat, 17 Aug 2024 00:53:53 +0530 Subject: [PATCH] changes in components and added new layout --- components/{Vendor.js => AddVendor.jsx} | 154 +++++----- components/DeleteVendorModal.jsx | 2 +- components/EditVendorModal.jsx | 367 ++++++++++++++++-------- components/LogBtn.js | 61 ++-- components/Navbar.js | 24 +- components/NoDataFound.jsx | 17 ++ components/Pagination.js | 2 +- components/VendorCard.jsx | 98 +++---- models/User.js | 19 ++ 9 files changed, 464 insertions(+), 280 deletions(-) rename components/{Vendor.js => AddVendor.jsx} (62%) create mode 100644 components/NoDataFound.jsx create mode 100644 models/User.js diff --git a/components/Vendor.js b/components/AddVendor.jsx similarity index 62% rename from components/Vendor.js rename to components/AddVendor.jsx index 3e086a0..de0eb65 100644 --- a/components/Vendor.js +++ b/components/AddVendor.jsx @@ -1,24 +1,41 @@ import { useState } from "react"; import { ToastContainer, toast } from "react-toastify"; import "react-toastify/dist/ReactToastify.css"; +import { useSession } from 'next-auth/react'; const VendorComp = () => { - const [name, setName] = useState(""); - const [bankName, setBankName] = useState(""); - const [accNo, setAccNo] = useState(""); - const [address1, setAddress1] = useState(""); - const [address2, setAddress2] = useState(""); - const [city, setCity] = useState(""); - const [zipCode, setZipCode] = useState(""); - const [country, setCountry] = useState(""); + const [formData, setFormData] = useState({ + name: "", + bankName: "", + accNo: "", + address1: "", + address2: "", + city: "", + zipCode: "", + country: "", + }); + const [inputError, setInputError] = useState({}); + const { data: session } = useSession(); + const userId = session?.userId; + + const handleChange = ({ target: { name, value } }) => { + setFormData({ + ...formData, + [name]: value, + }); + setInputError({ + ...inputError, + [name]: "", + }); + }; const validateInput = () => { const newErrors = {}; - if (accNo.length !== 12) { + if (formData.accNo.length !== 12) { newErrors.accNo = "Account number must be exactly 12 digits."; } - if (zipCode.length > 10) { + if (formData.zipCode.length > 10) { newErrors.zipCode = "Zip code must be less than 10 digits."; } @@ -30,19 +47,14 @@ const VendorComp = () => { e.preventDefault(); if (!validateInput()) { - toast("Please fix the inputError before submitting", { type: "error" }); + toast("Please fix the errors before submitting", { type: "error" }); return; } + // Add userId to formData const VendorPayload = { - accNo, - address1, - address2, - bankName, - city, - country, - name, - zipCode, + ...formData, + userId, }; // Send Data to Server @@ -54,34 +66,40 @@ const VendorComp = () => { }, }) .then((res) => { - if (res) { + if (res.ok) { toast("Vendor created successfully", { type: "success" }); + } else { + throw new Error("Network response was not ok."); } }) .catch((err) => { - console.log(err); - toast("Sorry, could not create", { type: "error" }); + console.log("Error: ", err); + toast("Sorry, could not create vendor", { type: "error" }); }); - setAccNo(""); - setAddress1(""); - setAddress2(""); - setBankName(""); - setCity(""); - setCountry(""); - setName(""); - setZipCode(""); + + setFormData({ + name: "", + bankName: "", + accNo: "", + address1: "", + address2: "", + city: "", + zipCode: "", + country: "", + }); }; return ( -
+
Create New Vendor
-
+
setName(target.value)} + name="name" + value={formData.name} + onChange={handleChange} className="border-solid border-gray-400 border-2 p-3 md:text-xl w-full rounded-md" id="vendorName" placeholder="Vendor Name" @@ -92,8 +110,9 @@ const VendorComp = () => {
setBankName(target.value)} + name="bankName" + value={formData.bankName} + onChange={handleChange} className="border-solid border-gray-400 border-2 p-3 md:text-xl w-full rounded-md" id="bankName" placeholder="Bank Name" @@ -104,32 +123,25 @@ const VendorComp = () => {
{ - setAccNo(target.value); - setInputError({ ...inputError, accNo: "" }); - }} - cols="30" - rows="8" + name="accNo" + value={formData.accNo} + onChange={handleChange} className="border-solid border-gray-400 border-2 p-3 md:text-xl w-full rounded-md" id="accNumber" placeholder="Bank Account Number" required /> - - {inputError.accNo && ( -

{inputError.accNo}

- )} -
+ {inputError.accNo && ( +

{inputError.accNo}

+ )}
setAddress1(target.value)} + name="address1" + value={formData.address1} + onChange={handleChange} className="border-solid border-gray-400 border-2 p-3 md:text-xl w-full rounded-md" id="address1" placeholder="Address Line 1" @@ -140,10 +152,9 @@ const VendorComp = () => {
setAddress2(target.value)} + name="address2" + value={formData.address2} + onChange={handleChange} className="border-solid border-gray-400 border-2 p-3 md:text-xl w-full rounded-md" id="address2" placeholder="Address Line 2" @@ -153,8 +164,9 @@ const VendorComp = () => {
setCity(target.value)} + name="city" + value={formData.city} + onChange={handleChange} className="border-solid border-gray-400 border-2 p-3 md:text-xl w-full rounded-md" id="city" placeholder="City" @@ -165,30 +177,25 @@ const VendorComp = () => {
{ - setZipCode(target.value); - setInputError({ ...inputError, zipCode: "" }); - }} + name="zipCode" + value={formData.zipCode} + onChange={handleChange} className="border-solid border-gray-400 border-2 p-3 md:text-xl w-full rounded-md" id="zip" placeholder="ZipCode" required /> - - {inputError.zipCode && ( -

{inputError.zipCode}

- )} -
+ {inputError.zipCode && ( +

{inputError.zipCode}

+ )}
setCountry(target.value)} + name="country" + value={formData.country} + onChange={handleChange} className="border-solid border-gray-400 border-2 p-3 md:text-xl w-full rounded-md" id="country" placeholder="Country" @@ -198,9 +205,8 @@ const VendorComp = () => {
diff --git a/components/DeleteVendorModal.jsx b/components/DeleteVendorModal.jsx index 19a0c5c..f95afb0 100644 --- a/components/DeleteVendorModal.jsx +++ b/components/DeleteVendorModal.jsx @@ -41,7 +41,7 @@ const DeleteVendorModal = ({ >
diff --git a/components/EditVendorModal.jsx b/components/EditVendorModal.jsx index 25c2f85..4784ab5 100644 --- a/components/EditVendorModal.jsx +++ b/components/EditVendorModal.jsx @@ -1,9 +1,16 @@ import React from "react"; import { toast } from "react-toastify"; import "react-toastify/dist/ReactToastify.css"; +import { + Dialog, + DialogBackdrop, + DialogPanel, + DialogTitle, +} from "@headlessui/react"; const EditVendorModal = ({ setShowEditVendorModal, + showEditVendorModal, _id, accNo, address1, @@ -64,146 +71,256 @@ const EditVendorModal = ({ return ( <> -
- {updatedDetailsOfVendors.map((ele) => { - return ( -
+ +
+
+ -
- Edit Vendor Details -
- -
-
- setName(e.target.value)} - required - /> -
+
+
+
+ + Update Vendor + +
+ {updatedDetailsOfVendors.map((ele) => { + return ( +
+ +
+
+ setName(e.target.value)} + required + /> +
-
- setBankName(e.target.value)} - required - /> -
+
+ + setBankName(e.target.value) + } + required + /> +
-
- setAccNo(e.target.value)} - required - /> -
+
+ setAccNo(e.target.value)} + required + /> +
-
- setAddress1(e.target.value)} - required - /> -
+
+ + setAddress1(e.target.value) + } + required + /> +
-
- setAddress2(e.target.value)} - /> -
+
+ + setAddress2(e.target.value) + } + /> +
-
- setCity(e.target.value)} - required - /> -
+
+ setCity(e.target.value)} + required + /> +
-
- setZipCode(e.target.value)} - required - /> -
+
+ setZipCode(e.target.value)} + required + /> +
-
- setCountry(e.target.value)} - required - /> -
+
+ setCountry(e.target.value)} + required + /> +
-
-
- +
+ +
+ ); + })} +
+
+
+
+ + +
+ +
+
+ + + ); +}; -
- +export default EditVendorModal; + + +/* +"use client"; + +import { + Dialog, + DialogBackdrop, + DialogPanel, + DialogTitle, +} from "@headlessui/react"; + return ( + + + +
+
+ +
+
+
+
+
+ + Delete Vendor + +
+

+ Are you sure you want to delete this vendor? All of your + data will be permanently removed. This action cannot be + undone. +

- +
+
+
+ +
- ); - })} +
+
- +
); }; -export default EditVendorModal; +export default DeleteVendorModal; + +*/ \ No newline at end of file diff --git a/components/LogBtn.js b/components/LogBtn.js index 8fdf09e..b0b2531 100644 --- a/components/LogBtn.js +++ b/components/LogBtn.js @@ -1,23 +1,44 @@ -import {useSession, signIn, signOut} from 'next-auth/react' +import { useSession, signIn, signOut } from "next-auth/react"; +import { useEffect } from "react"; -const LogBtn = () => { - const {data: session} = useSession() - - if(session){ - return ( -
- {/*

{session.user.email}

*/} - -
- ); - } - else{ - return ( -
- -
- ); +const LogBtn = ({setUserImage}) => { + const { data: session } = useSession(); + + useEffect(() => { + if (session) { + setUserImage(session.user.image); } -} - + }, [session, setUserImage]); + + if (session) { + return ( +
+ +
+ ); + } else { + return ( +
+ +
+ ); + } +}; + export default LogBtn; \ No newline at end of file diff --git a/components/Navbar.js b/components/Navbar.js index d9f4512..eead54f 100644 --- a/components/Navbar.js +++ b/components/Navbar.js @@ -3,30 +3,31 @@ import { useRouter } from "next/router"; import LogBtn from "./LogBtn"; import logo from "../public/Logo.png"; import Image from "next/image"; +import { useState } from 'react'; const Navbar = () => { const router = useRouter(); + const [userImage, setUserImage] = useState(""); const getNavLinkClass = (path) => { return router.pathname === path - ? "bg-blue-700 text-white" - : "hover:bg-blue-700 transition-all hover:text-white"; + ? "bg-[#387F39] text-[#EEEEEE]" + : "hover:bg-[#387F39] transition-all hover:text-[#EEEEEE]"; }; return (
-
-
- - logo +
+
+ + logo VƎNDORVAULT -
diff --git a/components/NoDataFound.jsx b/components/NoDataFound.jsx new file mode 100644 index 0000000..bec3fc3 --- /dev/null +++ b/components/NoDataFound.jsx @@ -0,0 +1,17 @@ +import NoDataFoundImage from "../public/no-data-bro.png"; +import Image from "next/image"; +import Link from "next/link"; + +const NoDataFound = () => { + return ( +
+ No Data Found + Sorry! No data to display + + Get Started + +
+ ); +}; + +export default NoDataFound; diff --git a/components/Pagination.js b/components/Pagination.js index cb92ef5..847c08b 100644 --- a/components/Pagination.js +++ b/components/Pagination.js @@ -6,7 +6,7 @@ const Pagination = ({ vendorsPerPage, totalVendors, paginate }) => { } return ( -