Refactor API and frontend components: update item state handling, adjust API key length, and improve table layout for MyLoansPage
This commit is contained in:
@@ -21,7 +21,7 @@ export const getItemsFromDatabaseV2 = async () => {
|
||||
|
||||
export const getLoanByCodeV2 = async (loan_code) => {
|
||||
const [result] = await pool.query(
|
||||
"SELECT first_name, returned_date, take_date, lockers FROM loans WHERE loan_code = ?;",
|
||||
"SELECT username, returned_date, take_date, lockers FROM loans WHERE loan_code = ?;",
|
||||
[loan_code]
|
||||
);
|
||||
if (result.length > 0) {
|
||||
@@ -32,7 +32,7 @@ export const getLoanByCodeV2 = async (loan_code) => {
|
||||
|
||||
export const changeInSafeStateV2 = async (itemId) => {
|
||||
const [result] = await pool.query(
|
||||
"UPDATE items SET inSafe = NOT inSafe WHERE id = ?",
|
||||
"UPDATE items SET in_safe = NOT in_safe WHERE id = ?",
|
||||
[itemId]
|
||||
);
|
||||
if (result.affectedRows > 0) {
|
||||
@@ -59,7 +59,7 @@ export const setReturnDateV2 = async (loanCode) => {
|
||||
: JSON.parse(items[0].loaned_items_id || "[]");
|
||||
|
||||
const [setItemStates] = await pool.query(
|
||||
"UPDATE items SET inSafe = 1, currently_borrowing = NULL, last_borrowed_person = (?) WHERE id IN (?)",
|
||||
"UPDATE items SET in_safe = 1, currently_borrowing = NULL, last_borrowed_person = (?) WHERE id IN (?)",
|
||||
[owner[0].username, itemIds]
|
||||
);
|
||||
|
||||
@@ -92,7 +92,7 @@ export const setTakeDateV2 = async (loanCode) => {
|
||||
: JSON.parse(items[0].loaned_items_id || "[]");
|
||||
|
||||
const [setItemStates] = await pool.query(
|
||||
"UPDATE items SET inSafe = 0, currently_borrowing = (?) WHERE id IN (?)",
|
||||
"UPDATE items SET in_safe = 0, currently_borrowing = (?) WHERE id IN (?)",
|
||||
[owner[0].username, itemIds]
|
||||
);
|
||||
|
||||
|
||||
@@ -23,25 +23,16 @@ router.get("/items/:key", authenticate, async (req, res) => {
|
||||
});
|
||||
|
||||
// Route for API to control the safe state of an item
|
||||
router.post(
|
||||
"/change-state/:key/:itemId/:state",
|
||||
authenticate,
|
||||
async (req, res) => {
|
||||
const itemId = req.params.itemId;
|
||||
const state = req.params.state;
|
||||
router.post("/change-state/:key/:itemId", authenticate, async (req, res) => {
|
||||
const itemId = req.params.itemId;
|
||||
|
||||
if (state === "1" || state === "0") {
|
||||
const result = await changeInSafeStateV2(itemId, state);
|
||||
if (result.success) {
|
||||
res.status(200).json({ data: result.data });
|
||||
} else {
|
||||
res.status(500).json({ message: "Failed to update item state" });
|
||||
}
|
||||
} else {
|
||||
res.status(400).json({ message: "Invalid state value" });
|
||||
}
|
||||
const result = await changeInSafeStateV2(itemId);
|
||||
if (result.success) {
|
||||
res.status(200).json({ data: result.data });
|
||||
} else {
|
||||
res.status(500).json({ message: "Failed to update item state" });
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
// Route for API to get a loan by its code
|
||||
router.get(
|
||||
|
||||
Reference in New Issue
Block a user