diff --git a/src/components/file-input/FileInput.jsx b/src/components/file-input/FileInput.jsx
index 956b897..f1a5559 100644
--- a/src/components/file-input/FileInput.jsx
+++ b/src/components/file-input/FileInput.jsx
@@ -13,7 +13,6 @@ const FileInput = ({ files, setFiles, register, errors, clearErrors }) => {
setFiles(Array.from(e.target.files));
setInput(true);
};
-
return (
{
>
{
accept='image/*'
multiple
/>
- {input ? (
+ {input || files ? (
{files &&
files.map((file) => (
))}
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"}