Skip to content

Commit

Permalink
change how repositioning of user card works
Browse files Browse the repository at this point in the history
instead of swapping between 2 cards just move all users +1 or -1
  • Loading branch information
LoV432 committed Nov 19, 2023
1 parent c829ced commit 56732fd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
67 changes: 36 additions & 31 deletions app/api/edit/change-index/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export async function POST(request: Request) {
if (
!body.index ||
body.index === '' ||
isNaN(parseInt(body.index)) ||
!body.macAddress ||
body.macAddress === ''
) {
Expand All @@ -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) {
Expand All @@ -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
});
);
}
4 changes: 2 additions & 2 deletions app/components/user-cards/UserCard.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function DropDown({
}}
className="flex h-12 justify-center"
>
Change Index
Change Position
</p>
</li>
<li>
Expand Down Expand Up @@ -309,7 +309,7 @@ function ChangeIndexPopUp({
return (
<dialog ref={setChangeIndexModal} className="modal">
<div className="modal-box bg-zinc-900">
<h3 className="pb-5 text-lg font-bold">Enter New Index</h3>
<h3 className="pb-5 text-lg font-bold">Enter New Position</h3>
<input
onKeyDown={(e) => {
if (e.key === 'Enter') {
Expand Down

0 comments on commit 56732fd

Please sign in to comment.