diff --git a/admin/src/components/ItemTable.tsx b/admin/src/components/ItemTable.tsx index efaad69..79b372f 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 5a9b4c2..7942c2e 100644 --- a/admin/src/utils/userActions.ts +++ b/admin/src/utils/userActions.ts @@ -192,3 +192,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 0c4634f..7bf442d 100644 --- a/backend/services/database.js +++ b/backend/services/database.js @@ -40,10 +40,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 };