Compare commits
2 Commits
755ebfd06b
...
5159877d8d
Author | SHA1 | Date | |
---|---|---|---|
5159877d8d | |||
b3ddfd9aa5 |
@@ -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,55 +215,62 @@ const ItemTable: React.FC = () => {
|
|||||||
/>
|
/>
|
||||||
</Table.Cell>
|
</Table.Cell>
|
||||||
<Table.Cell>
|
<Table.Cell>
|
||||||
{item.inSafe ? (
|
<Button
|
||||||
<Tag.Root
|
onClick={() =>
|
||||||
size="md"
|
changeSafeState(item.id).then(() => setReload(!reload))
|
||||||
bg="green.500"
|
}
|
||||||
color="white"
|
size="sm"
|
||||||
px={4}
|
>
|
||||||
py={1.5}
|
{item.inSafe ? (
|
||||||
rounded="full"
|
<Tag.Root
|
||||||
display="inline-flex"
|
size="md"
|
||||||
alignItems="center"
|
bg="green.500"
|
||||||
gap={2}
|
color="white"
|
||||||
shadow="sm"
|
px={4}
|
||||||
_hover={{ shadow: "md" }}
|
py={1.5}
|
||||||
>
|
rounded="full"
|
||||||
<Icon as={CheckCircle2} boxSize={4} />
|
display="inline-flex"
|
||||||
<Text
|
alignItems="center"
|
||||||
as="span"
|
gap={2}
|
||||||
fontSize="xs"
|
shadow="sm"
|
||||||
letterSpacing="wide"
|
_hover={{ shadow: "md" }}
|
||||||
textTransform="uppercase"
|
|
||||||
>
|
>
|
||||||
Yes
|
<Icon as={CheckCircle2} boxSize={4} />
|
||||||
</Text>
|
<Text
|
||||||
</Tag.Root>
|
as="span"
|
||||||
) : (
|
fontSize="xs"
|
||||||
<Tag.Root
|
letterSpacing="wide"
|
||||||
size="md"
|
textTransform="uppercase"
|
||||||
bg="red.500"
|
>
|
||||||
color="white"
|
Yes
|
||||||
px={4}
|
</Text>
|
||||||
py={1.5}
|
</Tag.Root>
|
||||||
rounded="full"
|
) : (
|
||||||
display="inline-flex"
|
<Tag.Root
|
||||||
alignItems="center"
|
size="md"
|
||||||
gap={2}
|
bg="red.500"
|
||||||
shadow="sm"
|
color="white"
|
||||||
_hover={{ shadow: "md" }}
|
px={4}
|
||||||
>
|
py={1.5}
|
||||||
<Icon as={XCircle} boxSize={4} />
|
rounded="full"
|
||||||
<Text
|
display="inline-flex"
|
||||||
as="span"
|
alignItems="center"
|
||||||
fontSize="xs"
|
gap={2}
|
||||||
letterSpacing="wide"
|
shadow="sm"
|
||||||
textTransform="uppercase"
|
_hover={{ shadow: "md" }}
|
||||||
>
|
>
|
||||||
No
|
<Icon as={XCircle} boxSize={4} />
|
||||||
</Text>
|
<Text
|
||||||
</Tag.Root>
|
as="span"
|
||||||
)}
|
fontSize="xs"
|
||||||
|
letterSpacing="wide"
|
||||||
|
textTransform="uppercase"
|
||||||
|
>
|
||||||
|
No
|
||||||
|
</Text>
|
||||||
|
</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>
|
||||||
@@ -273,8 +284,13 @@ const ItemTable: React.FC = () => {
|
|||||||
if (response.success) {
|
if (response.success) {
|
||||||
setError(
|
setError(
|
||||||
"success",
|
"success",
|
||||||
"User edited",
|
"Gegenstand erfolgreich bearbeitet!",
|
||||||
"The user has been successfully edited."
|
"Gegenstand " +
|
||||||
|
'"' +
|
||||||
|
item.item_name +
|
||||||
|
'" mit ID ' +
|
||||||
|
item.id +
|
||||||
|
" bearbeitet."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@@ -163,9 +163,41 @@ export const handleEditItems = async (
|
|||||||
can_borrow_role: string
|
can_borrow_role: string
|
||||||
) => {
|
) => {
|
||||||
try {
|
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) {
|
} catch (error) {
|
||||||
console.error("Error editing item:", error);
|
console.error("Error editing item:", error);
|
||||||
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 };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
@@ -309,18 +309,19 @@ router.post("/changePWadmin", authenticate, async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.post("/updateItemByID", authenticate, async (req, res) => {
|
router.post("/updateItemByID", authenticate, async (req, res) => {
|
||||||
const itemId = req.body.id;
|
const role = req.body.can_borrow_role;
|
||||||
const { item_name, can_borrow_role } = req.body || {};
|
const itemId = req.body.itemId;
|
||||||
const result = await updateItemByID(itemId, item_name, can_borrow_role);
|
const item_name = req.body.item_name;
|
||||||
|
const result = await updateItemByID(itemId, item_name, role);
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
return res.status(200).json({ message: "Item updated successfully" });
|
return res.status(200).json({ message: "Item updated successfully" });
|
||||||
}
|
}
|
||||||
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