+
);
}
diff --git a/src/lib/firebase/firestoreFunctions.js b/src/lib/firebase/firestoreFunctions.js
index d1113d3..97f0c5f 100644
--- a/src/lib/firebase/firestoreFunctions.js
+++ b/src/lib/firebase/firestoreFunctions.js
@@ -1,6 +1,7 @@
import {
addDoc,
collection,
+ deleteDoc,
doc,
getDoc,
getDocs,
@@ -67,7 +68,9 @@ export const getItemsByUser = async (userId) => {
const querySnapshot = await getDocs(q);
const items = [];
querySnapshot.forEach((doc) => {
- items.push(doc.data());
+ let data = doc.data();
+ data.id = doc.id;
+ items.push(data);
});
return items;
};
@@ -86,3 +89,9 @@ export const getItemByCategory = async (category) => {
});
return items;
};
+
+// DELETE DOC
+
+export const deleteDocData = async (collection, docId) => {
+ return await deleteDoc(doc(db, collection, docId));
+};
diff --git a/src/pages/account/index.jsx b/src/pages/account/index.jsx
new file mode 100644
index 0000000..f943a58
--- /dev/null
+++ b/src/pages/account/index.jsx
@@ -0,0 +1,61 @@
+import { useEffect, useState } from "react";
+import { AiOutlineDelete, AiOutlineEdit } from "react-icons/ai";
+
+import { getItemsByUser } from "@/lib/firebase/firestoreFunctions";
+
+import DeleteWarning from "@/components/delete-warning";
+import ItemCard from "@/components/itemcard/ItemCard";
+import Profile from "@/components/profile/Profile";
+
+import { useAuth } from "@/context/AuthContext";
+
+export default function MyAccount() {
+ const { currentUser } = useAuth();
+ const [items, setItems] = useState([]);
+ const [deleteWarningItem, setDeleteWarningItem] = useState();
+
+ useEffect(() => {
+ getItemsByUser(currentUser.uid).then((data) => {
+ setItems(data);
+ });
+ }, [currentUser]);
+ return (
+ <>
+ {deleteWarningItem && (
+
+ )}
+
+
+
+
+ My Items:
+
+
+
+ {items.map((item) => (
+
+
+
+ setDeleteWarningItem(item.id)
+ }
+ className='text-red text-3xl cursor-pointer'
+ />
+
+
+
+
+ ))}
+
+
+ >
+ );
+}
diff --git a/src/pages/add-item/index.jsx b/src/pages/add-item/index.jsx
index d1e5724..cb1ca86 100644
--- a/src/pages/add-item/index.jsx
+++ b/src/pages/add-item/index.jsx
@@ -1,10 +1,11 @@
import { clsx } from "clsx";
import { serverTimestamp } from "firebase/firestore";
+import { useRouter } from "next/router";
import { appWithTranslation } from "next-i18next";
import { useState } from "react";
import { useForm } from "react-hook-form";
-import { setDoc } from "@/lib/firebase/firestoreFunctions";
+import { setDoc, updateDocData } from "@/lib/firebase/firestoreFunctions";
import UseUploadImage from "@/lib/hooks/useUploadImage";
import Button from "@/components/button/Button";
@@ -14,15 +15,32 @@ import { useAuth } from "@/context/AuthContext";
function AddItemPage() {
const { currentUser } = useAuth();
- const [files, setFiles] = useState([]);
+ const router = useRouter();
+ const [files, setFiles] = useState();
const [uploadFile] = UseUploadImage();
+ const itemQueryData = router.query;
+ const updateItemPageMode = !itemQueryData.isEmpty;
+ let inputDefaultValues = {};
+ if (updateItemPageMode) {
+ inputDefaultValues = {
+ itemName: itemQueryData.title,
+ category: itemQueryData.categories,
+ description: itemQueryData.description,
+ location: itemQueryData.location,
+ };
+ if (typeof itemQueryData.imagesList === "string") {
+ itemQueryData.imagesList = [itemQueryData.imagesList];
+ }
+ }
const {
register,
handleSubmit,
formState: { errors, isSubmitting },
reset,
clearErrors,
- } = useForm();
+ } = useForm({
+ defaultValues: inputDefaultValues,
+ });
const submitItem = async (data) => {
const uploadPromises = files.map(async (file) => {
@@ -48,15 +66,38 @@ function AddItemPage() {
await setDoc(itemData, "items");
reset();
};
+ const updateItem = async (data) => {
+ let downloadUrls;
+ if (files) {
+ const uploadPromises = files.map(async (file) => {
+ return await uploadFile(file);
+ });
+ downloadUrls = await Promise.all(uploadPromises);
+ }
+ const itemData = {
+ title: data.itemName,
+ categories: data.category,
+ description: data.description,
+ location: data.location,
+ imagesList: downloadUrls ? downloadUrls : itemQueryData.imagesList,
+ updatedAt: serverTimestamp(),
+ };
+ await updateDocData("items", itemQueryData.id, itemData);
+ router.push(`/item/${itemQueryData.id}`);
+ };
return (
- Add new Item
+ {updateItemPageMode ? "Update Item" : "Add new Item"}