added changeSafeState function
This commit is contained in:
@@ -24,7 +24,11 @@ import {
|
|||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { deleteItem, handleEditItems } from "@/utils/userActions";
|
import {
|
||||||
|
deleteItem,
|
||||||
|
handleEditItems,
|
||||||
|
changeSafeState,
|
||||||
|
} from "@/utils/userActions";
|
||||||
import AddItemForm from "./AddItemForm";
|
import AddItemForm from "./AddItemForm";
|
||||||
import { formatDateTime } from "@/utils/userFuncs";
|
import { formatDateTime } from "@/utils/userFuncs";
|
||||||
|
|
||||||
@@ -211,6 +215,12 @@ const ItemTable: React.FC = () => {
|
|||||||
/>
|
/>
|
||||||
</Table.Cell>
|
</Table.Cell>
|
||||||
<Table.Cell>
|
<Table.Cell>
|
||||||
|
<Button
|
||||||
|
onClick={() =>
|
||||||
|
changeSafeState(item.id).then(() => setReload(!reload))
|
||||||
|
}
|
||||||
|
size="sm"
|
||||||
|
>
|
||||||
{item.inSafe ? (
|
{item.inSafe ? (
|
||||||
<Tag.Root
|
<Tag.Root
|
||||||
size="md"
|
size="md"
|
||||||
@@ -260,6 +270,7 @@ const ItemTable: React.FC = () => {
|
|||||||
</Text>
|
</Text>
|
||||||
</Tag.Root>
|
</Tag.Root>
|
||||||
)}
|
)}
|
||||||
|
</Button>
|
||||||
</Table.Cell>
|
</Table.Cell>
|
||||||
<Table.Cell>{formatDateTime(item.entry_created_at)}</Table.Cell>
|
<Table.Cell>{formatDateTime(item.entry_created_at)}</Table.Cell>
|
||||||
<Table.Cell>
|
<Table.Cell>
|
||||||
|
@@ -180,3 +180,24 @@ export const handleEditItems = async (
|
|||||||
return { success: false };
|
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 };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
@@ -319,9 +319,9 @@ router.post("/updateItemByID", authenticate, async (req, res) => {
|
|||||||
return res.status(500).json({ message: "Failed to update item" });
|
return res.status(500).json({ message: "Failed to update item" });
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post("/setSafeState", authenticate, async (req, res) => {
|
router.put("/changeSafeState/:itemId", authenticate, async (req, res) => {
|
||||||
const { itemId, state } = req.body || {};
|
const itemId = req.params.itemId;
|
||||||
const result = await changeInSafeStateV2(itemId, state);
|
const result = await changeInSafeStateV2(itemId);
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
return res
|
return res
|
||||||
.status(200)
|
.status(200)
|
||||||
|
@@ -39,10 +39,10 @@ export const getLoanByCodeV2 = async (loan_code) => {
|
|||||||
return { success: false };
|
return { success: false };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const changeInSafeStateV2 = async (itemId, state) => {
|
export const changeInSafeStateV2 = async (itemId) => {
|
||||||
const [result] = await pool.query(
|
const [result] = await pool.query(
|
||||||
"UPDATE items SET inSafe = ? WHERE id = ?",
|
"UPDATE items SET inSafe = NOT inSafe WHERE id = ?",
|
||||||
[state, itemId]
|
[itemId]
|
||||||
);
|
);
|
||||||
if (result.affectedRows > 0) {
|
if (result.affectedRows > 0) {
|
||||||
return { success: true };
|
return { success: true };
|
||||||
|
Reference in New Issue
Block a user