From 0d9f846992170007c6a0906e1eb280ff7f29bbfd Mon Sep 17 00:00:00 2001 From: ismailbenlaredj Date: Tue, 14 Nov 2023 22:02:26 +0100 Subject: [PATCH] feat(items page): update items page --- src/components/input/index.jsx | 7 ++- src/constants/index.js | 14 ++--- src/layout/Layout.jsx | 2 +- src/lib/firebase/firestoreFunctions.js | 15 +++++ src/pages/products/index.jsx | 80 ++++++++++++++++++++++++-- 5 files changed, 104 insertions(+), 14 deletions(-) diff --git a/src/components/input/index.jsx b/src/components/input/index.jsx index f64c09b..2934f7f 100644 --- a/src/components/input/index.jsx +++ b/src/components/input/index.jsx @@ -1,4 +1,7 @@ import clsx from "clsx"; + +import { cn } from "@/lib/utils"; + const Input = ({ name, type, @@ -8,6 +11,7 @@ const Input = ({ requiredMessage, validation, errors, + className = "", }) => { return ( <> @@ -24,12 +28,13 @@ const Input = ({ })} placeholder={placeholder} type={type} - className={clsx( + className={cn( "p-2 block w-full border rounded-md focus:outline-none focus:ring-2 focus:ring-green", { "border-red": errors[name], "border-slate-300": !errors[name], }, + className, )} /> {errors[name] && ( diff --git a/src/constants/index.js b/src/constants/index.js index 0738f59..177101e 100644 --- a/src/constants/index.js +++ b/src/constants/index.js @@ -101,42 +101,42 @@ export const categories = [ id: 1, title: "Electronics", imgSrc: "/images/category_img/category_electronics.webp", - href: "/?category=electronics", + href: "products/?category=electronics", }, { id: 2, title: "Academic", imgSrc: "/images/category_img/category_academic.webp", - href: "/?category=academic", + href: "products/?category=academic", }, { id: 3, title: "Clothes", imgSrc: "/images/category_img/category_clothes.webp", - href: "/?category=clothes", + href: "products/?category=clothes", }, { id: 4, title: "Dorm", imgSrc: "/images/category_img/category_dorm.webp", - href: "/?category=dorm", + href: "products/?category=dorm", }, { id: 5, title: "Entertainment", imgSrc: "/images/category_img/category_entertainment.webp", - href: "/?category=entertainment", + href: "products/?category=entertainment", }, { id: 6, title: "Beauty", imgSrc: "/images/category_img/category_beauty.webp", - href: "/?category=beauty", + href: "products/?category=beauty", }, { id: 7, title: "Other", imgSrc: "/images/category_img/category_other.webp", - href: "/?category=other", + href: "products/?category=other", }, ]; diff --git a/src/layout/Layout.jsx b/src/layout/Layout.jsx index e5039ae..b93962f 100644 --- a/src/layout/Layout.jsx +++ b/src/layout/Layout.jsx @@ -4,7 +4,7 @@ import Navbar from "@/components/navbar/Navbar"; export default function Layout({ children }) { return (
-
+
{children}
diff --git a/src/lib/firebase/firestoreFunctions.js b/src/lib/firebase/firestoreFunctions.js index 97f0c5f..8f05bd8 100644 --- a/src/lib/firebase/firestoreFunctions.js +++ b/src/lib/firebase/firestoreFunctions.js @@ -5,6 +5,8 @@ import { doc, getDoc, getDocs, + limit, + orderBy, query, updateDoc, where, @@ -95,3 +97,16 @@ export const getItemByCategory = async (category) => { export const deleteDocData = async (collection, docId) => { return await deleteDoc(doc(db, collection, docId)); }; + +// GET ALL +export const getAllItems = async () => { + const q = query(collection(db, "items"), orderBy("createdAt"), limit(15)); + const querySnapshot = await getDocs(q); + const items = []; + querySnapshot.forEach((doc) => { + let data = doc.data(); + data.id = doc.id; + items.push(data); + }); + return items; +}; diff --git a/src/pages/products/index.jsx b/src/pages/products/index.jsx index 408cbbc..a84b04d 100644 --- a/src/pages/products/index.jsx +++ b/src/pages/products/index.jsx @@ -1,13 +1,83 @@ +import { useForm } from "react-hook-form"; + +import { getAllItems } from "@/lib/firebase/firestoreFunctions"; + +import Button from "@/components/button/Button"; import CategoryCard from "@/components/categorycard/CategoryCard"; +import Input from "@/components/input"; +import ItemCard from "@/components/itemcard/ItemCard"; import { categories } from "@/constants"; -export default function ProductsPage() { +export default function ProductsPage({ items }) { + const { + register, + handleSubmit, + formState: { errors, isSubmitting }, + } = useForm(); return ( -
- {categories.map((category) => { - return ; - })} +
+
+

+ Browse by category:{" "} +

+
+ {categories.map((category) => { + return ; + })} +
+
+
+ + + + +
+
+ {items.map((item) => ( + + ))} +
); } + +export async function getServerSideProps() { + let items = await getAllItems(); + items = JSON.parse(JSON.stringify(items)); + return { + props: { + items, + }, + }; +}