Merge branch 'dev_v1-admin' into debian12_v1-admin
This commit is contained in:
@@ -10,6 +10,7 @@ import {
|
|||||||
Heading,
|
Heading,
|
||||||
Icon,
|
Icon,
|
||||||
Tag,
|
Tag,
|
||||||
|
Input,
|
||||||
} from "@chakra-ui/react";
|
} from "@chakra-ui/react";
|
||||||
import { Tooltip } from "@/components/ui/tooltip";
|
import { Tooltip } from "@/components/ui/tooltip";
|
||||||
import MyAlert from "./myChakra/MyAlert";
|
import MyAlert from "./myChakra/MyAlert";
|
||||||
@@ -19,10 +20,11 @@ import {
|
|||||||
CirclePlus,
|
CirclePlus,
|
||||||
CheckCircle2,
|
CheckCircle2,
|
||||||
XCircle,
|
XCircle,
|
||||||
|
Save,
|
||||||
} 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 } from "@/utils/userActions";
|
import { deleteItem, handleEditItems } from "@/utils/userActions";
|
||||||
import AddItemForm from "./AddItemForm";
|
import AddItemForm from "./AddItemForm";
|
||||||
import { formatDateTime } from "@/utils/userFuncs";
|
import { formatDateTime } from "@/utils/userFuncs";
|
||||||
|
|
||||||
@@ -44,6 +46,18 @@ const ItemTable: React.FC = () => {
|
|||||||
const [reload, setReload] = useState(false);
|
const [reload, setReload] = useState(false);
|
||||||
const [addForm, setAddForm] = useState(false);
|
const [addForm, setAddForm] = useState(false);
|
||||||
|
|
||||||
|
const handleItemNameChange = (id: number, value: string) => {
|
||||||
|
setItems((prev) =>
|
||||||
|
prev.map((it) => (it.id === id ? { ...it, item_name: value } : it))
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleCanBorrowRoleChange = (id: number, value: string) => {
|
||||||
|
setItems((prev) =>
|
||||||
|
prev.map((it) => (it.id === id ? { ...it, can_borrow_role: value } : it))
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const setError = (
|
const setError = (
|
||||||
status: "error" | "success",
|
status: "error" | "success",
|
||||||
message: string,
|
message: string,
|
||||||
@@ -180,8 +194,22 @@ const ItemTable: React.FC = () => {
|
|||||||
{items.map((item) => (
|
{items.map((item) => (
|
||||||
<Table.Row key={item.id}>
|
<Table.Row key={item.id}>
|
||||||
<Table.Cell>{item.id}</Table.Cell>
|
<Table.Cell>{item.id}</Table.Cell>
|
||||||
<Table.Cell>{item.item_name}</Table.Cell>
|
<Table.Cell>
|
||||||
<Table.Cell>{item.can_borrow_role}</Table.Cell>
|
<Input
|
||||||
|
onChange={(e) =>
|
||||||
|
handleItemNameChange(item.id, e.target.value)
|
||||||
|
}
|
||||||
|
value={item.item_name}
|
||||||
|
/>
|
||||||
|
</Table.Cell>
|
||||||
|
<Table.Cell>
|
||||||
|
<Input
|
||||||
|
onChange={(e) =>
|
||||||
|
handleCanBorrowRoleChange(item.id, e.target.value)
|
||||||
|
}
|
||||||
|
value={item.can_borrow_role}
|
||||||
|
/>
|
||||||
|
</Table.Cell>
|
||||||
<Table.Cell>
|
<Table.Cell>
|
||||||
{item.inSafe ? (
|
{item.inSafe ? (
|
||||||
<Tag.Root
|
<Tag.Root
|
||||||
@@ -235,6 +263,32 @@ const ItemTable: React.FC = () => {
|
|||||||
</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>
|
||||||
|
<Button
|
||||||
|
onClick={() =>
|
||||||
|
handleEditItems(
|
||||||
|
item.id,
|
||||||
|
item.item_name,
|
||||||
|
item.can_borrow_role
|
||||||
|
).then((response) => {
|
||||||
|
if (response.success) {
|
||||||
|
setError(
|
||||||
|
"success",
|
||||||
|
"Gegenstand erfolgreich bearbeitet!",
|
||||||
|
"Gegenstand " +
|
||||||
|
'"' +
|
||||||
|
item.item_name +
|
||||||
|
'" mit ID ' +
|
||||||
|
item.id +
|
||||||
|
" bearbeitet."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
colorPalette="teal"
|
||||||
|
size="sm"
|
||||||
|
>
|
||||||
|
<Save />
|
||||||
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
deleteItem(item.id).then((response) => {
|
deleteItem(item.id).then((response) => {
|
||||||
|
@@ -165,3 +165,27 @@ export const createItem = async (
|
|||||||
return { success: false };
|
return { success: false };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const handleEditItems = async (
|
||||||
|
itemId: number,
|
||||||
|
item_name: string,
|
||||||
|
can_borrow_role: string
|
||||||
|
) => {
|
||||||
|
try {
|
||||||
|
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 };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
@@ -20,6 +20,8 @@ import {
|
|||||||
createItem,
|
createItem,
|
||||||
changeUserPassword,
|
changeUserPassword,
|
||||||
changeUserPasswordFRONTEND,
|
changeUserPasswordFRONTEND,
|
||||||
|
changeInSafeStateV2,
|
||||||
|
updateItemByID,
|
||||||
} from "../services/database.js";
|
} from "../services/database.js";
|
||||||
import { authenticate, generateToken } from "../services/tokenService.js";
|
import { authenticate, generateToken } from "../services/tokenService.js";
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
@@ -306,4 +308,26 @@ router.post("/changePWadmin", authenticate, async (req, res) => {
|
|||||||
return res.status(500).json({ message: "Failed to change password" });
|
return res.status(500).json({ message: "Failed to change password" });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.post("/updateItemByID", authenticate, async (req, res) => {
|
||||||
|
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" });
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
if (result.success) {
|
||||||
|
return res
|
||||||
|
.status(200)
|
||||||
|
.json({ message: "Item safe state updated successfully" });
|
||||||
|
}
|
||||||
|
return res.status(500).json({ message: "Failed to update item safe state" });
|
||||||
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
@@ -230,7 +230,7 @@ export const createLoanInDatabase = async (
|
|||||||
// Generate unique loan_code (retry a few times)
|
// Generate unique loan_code (retry a few times)
|
||||||
let loanCode = null;
|
let loanCode = null;
|
||||||
for (let i = 0; i < 6; i++) {
|
for (let i = 0; i < 6; i++) {
|
||||||
const candidate = Math.floor(1000 + Math.random() * 900000); // 4-6 digits
|
const candidate = Math.floor(100000 + Math.random() * 899999); // 6 digits
|
||||||
const [exists] = await conn.query(
|
const [exists] = await conn.query(
|
||||||
"SELECT 1 FROM loans WHERE loan_code = ? LIMIT 1",
|
"SELECT 1 FROM loans WHERE loan_code = ? LIMIT 1",
|
||||||
[candidate]
|
[candidate]
|
||||||
@@ -439,3 +439,12 @@ export const changeUserPasswordFRONTEND = async (
|
|||||||
if (result.affectedRows > 0) return { success: true };
|
if (result.affectedRows > 0) return { success: true };
|
||||||
return { success: false };
|
return { success: false };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const updateItemByID = async (itemId, item_name, can_borrow_role) => {
|
||||||
|
const [result] = await pool.query(
|
||||||
|
"UPDATE items SET item_name = ?, can_borrow_role = ? WHERE id = ?",
|
||||||
|
[item_name, can_borrow_role, itemId]
|
||||||
|
);
|
||||||
|
if (result.affectedRows > 0) return { success: true };
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
Reference in New Issue
Block a user