From e107f8b6c250759ab0f884b2fdd0283fae15cfe5 Mon Sep 17 00:00:00 2001
From: Md Saban <45597394+mdsaban@users.noreply.github.com>
Date: Sun, 30 Jun 2024 03:44:44 +0530
Subject: ui: refactor admin settings page (#249)

* ui: refactor admin ui

* fix: pr comments

* chore: lint fix

* chore: refactor

* minor tweaks

---------

Co-authored-by: MohamedBassem <me@mbassem.com>
---
 apps/web/components/dashboard/admin/UserList.tsx | 75 ++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
 create mode 100644 apps/web/components/dashboard/admin/UserList.tsx

(limited to 'apps/web/components/dashboard/admin/UserList.tsx')

diff --git a/apps/web/components/dashboard/admin/UserList.tsx b/apps/web/components/dashboard/admin/UserList.tsx
new file mode 100644
index 00000000..024325a3
--- /dev/null
+++ b/apps/web/components/dashboard/admin/UserList.tsx
@@ -0,0 +1,75 @@
+"use client";
+
+import { ActionButton } from "@/components/ui/action-button";
+import LoadingSpinner from "@/components/ui/spinner";
+import {
+  Table,
+  TableBody,
+  TableCell,
+  TableHead,
+  TableHeader,
+  TableRow,
+} from "@/components/ui/table";
+import { toast } from "@/components/ui/use-toast";
+import { api } from "@/lib/trpc";
+import { Trash } from "lucide-react";
+import { useSession } from "next-auth/react";
+
+export default function UsersSection() {
+  const { data: session } = useSession();
+  const invalidateUserList = api.useUtils().users.list.invalidate;
+  const { data: users } = api.users.list.useQuery();
+  const { mutate: deleteUser, isPending: isDeletionPending } =
+    api.users.delete.useMutation({
+      onSuccess: () => {
+        toast({
+          description: "User deleted",
+        });
+        invalidateUserList();
+      },
+      onError: (e) => {
+        toast({
+          variant: "destructive",
+          description: `Something went wrong: ${e.message}`,
+        });
+      },
+    });
+
+  if (!users) {
+    return <LoadingSpinner />;
+  }
+
+  return (
+    <>
+      <div className="mb-2 text-xl font-medium">Users List</div>
+
+      <Table>
+        <TableHeader className="bg-gray-200">
+          <TableHead>Name</TableHead>
+          <TableHead>Email</TableHead>
+          <TableHead>Role</TableHead>
+          <TableHead>Action</TableHead>
+        </TableHeader>
+        <TableBody>
+          {users.users.map((u) => (
+            <TableRow key={u.id}>
+              <TableCell className="py-1">{u.name}</TableCell>
+              <TableCell className="py-1">{u.email}</TableCell>
+              <TableCell className="py-1 capitalize">{u.role}</TableCell>
+              <TableCell className="py-1">
+                <ActionButton
+                  variant="outline"
+                  onClick={() => deleteUser({ userId: u.id })}
+                  loading={isDeletionPending}
+                  disabled={session!.user.id == u.id}
+                >
+                  <Trash size={16} color="red" />
+                </ActionButton>
+              </TableCell>
+            </TableRow>
+          ))}
+        </TableBody>
+      </Table>
+    </>
+  );
+}
-- 
cgit v1.2.3-70-g09d2

