Skip to content

Commit

Permalink
Add user deletion functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
kcoderhtml committed Apr 20, 2024
1 parent 4c74a89 commit 2eda61c
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/pages/users.astro
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ if (!team.lastSync) {
console.log(team);
let slackUsers = users.filter((user) => user.role === "invited");
let error = null;
if (Astro.request.method === "POST") {
const data = await Astro.request.formData();
console.log(data);
Expand Down Expand Up @@ -97,6 +98,17 @@ if (Astro.request.method === "POST") {
}
await db.update(Organization).set({ lastSync: new Date() }).where(like(Organization.team, session.team));
} else if (data.has("delete")) {
const userId = data.get("delete") as string;
if (session.user.id !== userId && session.user.role === "admin") {
console.log("deleting user", userId);
console.log(session.user.id, userId, session.user.id === userId)
await db.delete(User).where(like(User.userId, userId));
users = users.filter((user) => user.userId !== userId);
slackUsers = slackUsers.filter((user) => user.userId !== userId);
} else {
error = "You can't delete yourself";
}
}
}
---
Expand Down Expand Up @@ -139,13 +151,19 @@ if (Astro.request.method === "POST") {
)
}
<section class="users">
{
error && (
<h3 style="color: red; text-align: center;">Error: {error}</h3>
)
}
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Image</th>
<th>Role</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
Expand All @@ -154,14 +172,21 @@ if (Astro.request.method === "POST") {
</tr>

{
users.map(({ team, name, email, image, role }) => (
users.map(({ userId, team, name, email, image, role }) => (
<tr>
<td>{name}</td>
<td>{email || "N/A"}</td>
<td>
<img src=`${image}` alt="" class="profile-pic"/>
</td>
<td>{role}</td>
<td>
<div class="actions">
<form method="post" style="background: transparent; display: flex">
<button name="delete" type="submit" value={userId} style="margin-top: 0;">Delete</button>
</form>
</div>
</td>
</tr>
))
}
Expand Down

0 comments on commit 2eda61c

Please sign in to comment.