From b3ddfd9aa56e48ab167285b6752eb7669b791fc0 Mon Sep 17 00:00:00 2001 From: Theis Gaedigk Date: Tue, 16 Sep 2025 11:13:19 +0200 Subject: [PATCH] added working item change route --- admin/src/components/ItemTable.tsx | 9 +++++++-- admin/src/utils/userActions.ts | 15 +++++++++++++-- backend/routes/api.js | 7 ++++--- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/admin/src/components/ItemTable.tsx b/admin/src/components/ItemTable.tsx index d18123e..5151c96 100644 --- a/admin/src/components/ItemTable.tsx +++ b/admin/src/components/ItemTable.tsx @@ -273,8 +273,13 @@ const ItemTable: React.FC = () => { if (response.success) { setError( "success", - "User edited", - "The user has been successfully edited." + "Gegenstand erfolgreich bearbeitet!", + "Gegenstand " + + '"' + + item.item_name + + '" mit ID ' + + item.id + + " bearbeitet." ); } }) diff --git a/admin/src/utils/userActions.ts b/admin/src/utils/userActions.ts index c0536a5..372b53d 100644 --- a/admin/src/utils/userActions.ts +++ b/admin/src/utils/userActions.ts @@ -163,9 +163,20 @@ export const handleEditItems = async ( can_borrow_role: string ) => { try { - // write the logic for editing an item + const response = await fetch("http://localhost:8002/api/updateItemByID", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${Cookies.get("token")}`, + }, + body: JSON.stringify({ itemId, item_name, can_borrow_role }), + }); + if (!response.ok) { + throw new Error("Failed to edit item"); + } + return { success: true }; } catch (error) { console.error("Error editing item:", error); return { success: false }; } -}; \ No newline at end of file +}; diff --git a/backend/routes/api.js b/backend/routes/api.js index 9228d28..c7c4c3f 100644 --- a/backend/routes/api.js +++ b/backend/routes/api.js @@ -309,9 +309,10 @@ router.post("/changePWadmin", authenticate, async (req, res) => { }); router.post("/updateItemByID", authenticate, async (req, res) => { - const itemId = req.body.id; - const { item_name, can_borrow_role } = req.body || {}; - const result = await updateItemByID(itemId, item_name, can_borrow_role); + const role = req.body.can_borrow_role; + const itemId = req.body.itemId; + const item_name = req.body.item_name; + const result = await updateItemByID(itemId, item_name, role); if (result.success) { return res.status(200).json({ message: "Item updated successfully" }); }