From 5159877d8d25bca148742f6369a5e6ca3898b288 Mon Sep 17 00:00:00 2001 From: Theis Gaedigk Date: Tue, 16 Sep 2025 13:00:15 +0200 Subject: [PATCH] added changeSafeState function --- admin/src/components/ItemTable.tsx | 107 ++++++++++++++++------------- admin/src/utils/userActions.ts | 21 ++++++ backend/routes/api.js | 6 +- backend/services/database.js | 6 +- 4 files changed, 86 insertions(+), 54 deletions(-) diff --git a/admin/src/components/ItemTable.tsx b/admin/src/components/ItemTable.tsx index 5151c96..604113b 100644 --- a/admin/src/components/ItemTable.tsx +++ b/admin/src/components/ItemTable.tsx @@ -24,7 +24,11 @@ import { } from "lucide-react"; import Cookies from "js-cookie"; import { useState, useEffect } from "react"; -import { deleteItem, handleEditItems } from "@/utils/userActions"; +import { + deleteItem, + handleEditItems, + changeSafeState, +} from "@/utils/userActions"; import AddItemForm from "./AddItemForm"; import { formatDateTime } from "@/utils/userFuncs"; @@ -211,55 +215,62 @@ const ItemTable: React.FC = () => { /> - {item.inSafe ? ( - - - + changeSafeState(item.id).then(() => setReload(!reload)) + } + size="sm" + > + {item.inSafe ? ( + - Yes - - - ) : ( - - - + + Yes + + + ) : ( + - No - - - )} + + + No + + + )} + {formatDateTime(item.entry_created_at)} diff --git a/admin/src/utils/userActions.ts b/admin/src/utils/userActions.ts index 372b53d..beac000 100644 --- a/admin/src/utils/userActions.ts +++ b/admin/src/utils/userActions.ts @@ -180,3 +180,24 @@ export const handleEditItems = async ( return { success: false }; } }; + +export const changeSafeState = async (itemId: number) => { + try { + const response = await fetch( + `http://localhost:8002/api/changeSafeState/${itemId}`, + { + method: "PUT", + headers: { + Authorization: `Bearer ${Cookies.get("token")}`, + }, + } + ); + if (!response.ok) { + throw new Error("Failed to change safe state"); + } + return { success: true }; + } catch (error) { + console.error("Error changing safe state:", error); + return { success: false }; + } +}; diff --git a/backend/routes/api.js b/backend/routes/api.js index c7c4c3f..2b08eed 100644 --- a/backend/routes/api.js +++ b/backend/routes/api.js @@ -319,9 +319,9 @@ router.post("/updateItemByID", authenticate, async (req, res) => { return res.status(500).json({ message: "Failed to update item" }); }); -router.post("/setSafeState", authenticate, async (req, res) => { - const { itemId, state } = req.body || {}; - const result = await changeInSafeStateV2(itemId, state); +router.put("/changeSafeState/:itemId", authenticate, async (req, res) => { + const itemId = req.params.itemId; + const result = await changeInSafeStateV2(itemId); if (result.success) { return res .status(200) diff --git a/backend/services/database.js b/backend/services/database.js index 5242861..8a83a0e 100644 --- a/backend/services/database.js +++ b/backend/services/database.js @@ -39,10 +39,10 @@ export const getLoanByCodeV2 = async (loan_code) => { return { success: false }; }; -export const changeInSafeStateV2 = async (itemId, state) => { +export const changeInSafeStateV2 = async (itemId) => { const [result] = await pool.query( - "UPDATE items SET inSafe = ? WHERE id = ?", - [state, itemId] + "UPDATE items SET inSafe = NOT inSafe WHERE id = ?", + [itemId] ); if (result.affectedRows > 0) { return { success: true };