From 56732fd078b6f6fe8cb8c48fb80db094f4e934d1 Mon Sep 17 00:00:00 2001
From: LoV432
Date: Mon, 20 Nov 2023 00:01:51 +0500
Subject: [PATCH] change how repositioning of user card works
instead of swapping between 2 cards just move all users +1 or -1
---
app/api/edit/change-index/route.ts | 67 ++++++++++---------
app/components/user-cards/UserCard.client.tsx | 4 +-
2 files changed, 38 insertions(+), 33 deletions(-)
diff --git a/app/api/edit/change-index/route.ts b/app/api/edit/change-index/route.ts
index c3b9d79..70acd83 100644
--- a/app/api/edit/change-index/route.ts
+++ b/app/api/edit/change-index/route.ts
@@ -11,6 +11,7 @@ export async function POST(request: Request) {
if (
!body.index ||
body.index === '' ||
+ isNaN(parseInt(body.index)) ||
!body.macAddress ||
body.macAddress === ''
) {
@@ -19,7 +20,7 @@ export async function POST(request: Request) {
});
}
- let currentUser = db
+ const currentUser = db
.prepare('SELECT * FROM users WHERE mac_address = ?')
.get(body.macAddress) as userReturnType | undefined;
if (!currentUser) {
@@ -31,40 +32,44 @@ export async function POST(request: Request) {
);
}
- let conflictingUser = db
- .prepare('SELECT * FROM users WHERE index_number = ?')
- .get(body.index) as userReturnType | undefined;
- if (!conflictingUser) {
- let updateIndex = db
- .prepare('UPDATE users SET index_number = ? WHERE mac_address = ?')
- .run(body.index, body.macAddress);
- return new Response(JSON.stringify(updateIndex), {
- status: 200
- });
- }
+ const transaction = db.transaction(() => {
+ const selectedUser = db.prepare(
+ 'UPDATE users SET index_number = ? WHERE mac_address = ?'
+ );
- let updateIndex = db.prepare(
- 'UPDATE users SET index_number = @index WHERE mac_address = @mac'
- );
+ const newIndex = currentUser.index_number;
+
+ if (newIndex > parseInt(body.index)) {
+ selectedUser.run(parseInt(body.index) - 0.5, body.macAddress);
+ } else {
+ selectedUser.run(parseInt(body.index) + 0.5, body.macAddress);
+ }
+
+ let index = 1;
+ const allUsers = db
+ .prepare('SELECT * FROM users ORDER BY index_number ASC')
+ .all() as userReturnType[];
- let updateMany = db.transaction((users: { index: number; mac: string }[]) => {
- users.forEach((user) => {
- updateIndex.run(user);
+ allUsers.forEach((user) => {
+ db.prepare('UPDATE users SET index_number = ? WHERE id = ?').run(
+ index,
+ user.id
+ );
+ index++;
});
- });
- let updateIndexMany = updateMany([
- {
- index: parseInt(body.index),
- mac: body.macAddress
- },
+ return true;
+ })();
+
+ if (!transaction) {
+ return new Response(JSON.stringify({ error: 'Failed to update index' }), {
+ status: 400
+ });
+ }
+ return new Response(
+ JSON.stringify({ success: 'Index updated successfully' }),
{
- index: currentUser.index_number,
- mac: conflictingUser.mac_address
+ status: 200
}
- ]);
-
- return new Response(JSON.stringify(updateIndexMany), {
- status: 200
- });
+ );
}
diff --git a/app/components/user-cards/UserCard.client.tsx b/app/components/user-cards/UserCard.client.tsx
index 05bf1dc..84395ac 100644
--- a/app/components/user-cards/UserCard.client.tsx
+++ b/app/components/user-cards/UserCard.client.tsx
@@ -148,7 +148,7 @@ function DropDown({
}}
className="flex h-12 justify-center"
>
- Change Index
+ Change Position
@@ -309,7 +309,7 @@ function ChangeIndexPopUp({
return (