-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/66 admin dashboard users tab #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f178ca6
initial design of the Users page
Berny-ft 9736545
layout corrections
Berny-ft f80d744
small ui fixes
Berny-ft fb69210
refactored code
Berny-ft d7947ec
small ui fixes
Berny-ft 392f020
fixed scrolling and adaptive entry count depending on page size
Berny-ft 3a4d63e
fixed table bottom border stretch
Berny-ft 646ff7d
fixed search box padding and page indicator color
Berny-ft 065f534
adding string type for state variable
Berny-ft 0cb63c0
added last login to the user profile in the db schema and added a non…
Berny-ft 470351e
updated schema documentation
Berny-ft f0b3534
refactored card header in authenticated page to simplify structure an…
Berny-ft d04ba7c
replaced UsersPageSkeleton with Spinner component for improved loadin…
Berny-ft 07d050b
replaced LoaderIcon with Loader2Icon in Spinner component for updated…
Berny-ft 423f492
refactored UsersPage to require admin access, updated user fetching t…
Berny-ft 9cf31e5
refactored user avatar display to use Avatar component and replaced s…
Berny-ft f403d3f
fix(users): fetch roles after users to limit concurrent DB connections
Berny-ft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| "use client"; | ||
|
|
||
| import { useMemo, useState } from "react"; | ||
| import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; | ||
| import { | ||
| Select, | ||
| SelectContent, | ||
| SelectItem, | ||
| SelectTrigger, | ||
| SelectValue, | ||
| } from "@/components/ui/select"; | ||
| import { Input } from "@/components/ui/input"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { ArrowDownAZ, ArrowUpZA, Search } from "lucide-react"; | ||
| import { UsersDataTable } from "./users-data-table"; | ||
| import { usersColumns } from "./users-columns"; | ||
| import type { UserRow } from "../profile-role-label"; | ||
|
|
||
| const USER_SUBSCRIPTION_VIEW_TABS = [ | ||
| { value: "all", label: "All Users" }, | ||
| { value: "active", label: "Active" }, | ||
| { value: "inactive", label: "Inactive" }, | ||
| ] as const; | ||
|
|
||
| export type UserSubscriptionViewTab = | ||
| (typeof USER_SUBSCRIPTION_VIEW_TABS)[number]["value"]; | ||
|
|
||
| type SortDir = "asc" | "desc"; | ||
|
|
||
| export interface RoleFilterOption { | ||
| value: string; | ||
| label: string; | ||
| } | ||
|
|
||
| interface UsersClientProps { | ||
| users: UserRow[]; | ||
| roleFilterOptions: RoleFilterOption[]; | ||
| } | ||
|
|
||
| export function UsersClient({ users, roleFilterOptions }: UsersClientProps) { | ||
| const [tab, setTab] = useState<UserSubscriptionViewTab>("all"); | ||
| const [roleFilter, setRoleFilter] = useState<string>("all"); | ||
| const [nameQuery, setNameQuery] = useState<string>(""); | ||
| const [sortDir, setSortDir] = useState<SortDir>("asc"); | ||
|
|
||
| const filtered = useMemo(() => { | ||
| const query = nameQuery.trim().toLowerCase(); | ||
|
|
||
| return users | ||
| .filter((u) => { | ||
| if (!query) return true; | ||
| const name = `${u.firstName} ${u.lastName}`.toLowerCase(); | ||
| return ( | ||
| name.includes(query) || u.email.toLowerCase().includes(query) | ||
| ); | ||
| }) | ||
| .filter((u) => { | ||
| if (tab === "all") return true; | ||
| if (tab === "active") return u.isActive; | ||
| return !u.isActive; | ||
| }) | ||
| .filter((u) => { | ||
| if (roleFilter === "all") return true; | ||
| return u.role === roleFilter; | ||
| }) | ||
| .sort((a, b) => { | ||
| const nameA = `${a.firstName} ${a.lastName}`.toLowerCase(); | ||
| const nameB = `${b.firstName} ${b.lastName}`.toLowerCase(); | ||
| return sortDir === "asc" | ||
| ? nameA.localeCompare(nameB) | ||
| : nameB.localeCompare(nameA); | ||
| }); | ||
| }, [users, nameQuery, tab, roleFilter, sortDir]); | ||
|
|
||
| return ( | ||
| <div className="flex min-h-0 min-w-0 flex-1 flex-col overflow-hidden"> | ||
| <Tabs | ||
| value={tab} | ||
| onValueChange={(v) => setTab(v as UserSubscriptionViewTab)} | ||
| className="flex min-h-0 w-full min-w-0 flex-1 flex-col gap-2 overflow-hidden" | ||
| > | ||
| <div className="flex w-full min-w-0 shrink-0 flex-wrap items-center justify-between gap-x-3 gap-y-2"> | ||
| <div className="flex min-w-0 flex-1 flex-wrap items-center gap-2 sm:gap-3"> | ||
| <div className="relative p-0.5 min-w-[min(100%,10rem)] max-w-full grow sm:max-w-xs sm:grow-0 sm:basis-56"> | ||
| <Search className="absolute left-3 top-1/2 z-10 h-4 w-4 -translate-y-1/2 text-muted-foreground pointer-events-none" /> | ||
| <Input | ||
| id="users-search" | ||
| placeholder="Search by name or email..." | ||
| value={nameQuery} | ||
| onChange={(e) => setNameQuery(e.target.value)} | ||
| className="w-full min-w-0 pl-9" | ||
| /> | ||
| </div> | ||
|
|
||
| <TabsList className="h-auto min-h-8 max-w-full min-w-0 flex-wrap justify-start border border-border"> | ||
| {USER_SUBSCRIPTION_VIEW_TABS.map(({ value, label }) => ( | ||
| <TabsTrigger key={value} value={value}> | ||
| {label} | ||
| </TabsTrigger> | ||
| ))} | ||
| </TabsList> | ||
| </div> | ||
|
|
||
| <div className="flex min-w-0 shrink-0 flex-wrap items-center justify-end gap-2"> | ||
| <Select value={roleFilter} onValueChange={setRoleFilter}> | ||
| <SelectTrigger | ||
| id="users-role-filter" | ||
| className="w-[min(100%,11rem)] min-w-[8.5rem] sm:w-[140px]" | ||
| > | ||
| <SelectValue placeholder="All Roles" /> | ||
| </SelectTrigger> | ||
| <SelectContent> | ||
| {roleFilterOptions.map(({ value, label }) => ( | ||
| <SelectItem key={value} value={value}> | ||
| {label} | ||
| </SelectItem> | ||
| ))} | ||
| </SelectContent> | ||
| </Select> | ||
|
|
||
| <Button | ||
| id="users-sort-toggle" | ||
| variant="outline" | ||
| size="icon" | ||
| className="shrink-0" | ||
| onClick={() => | ||
| setSortDir((prev) => (prev === "asc" ? "desc" : "asc")) | ||
| } | ||
| aria-label={ | ||
| sortDir === "asc" ? "Sort Z to A" : "Sort A to Z" | ||
| } | ||
| > | ||
| {sortDir === "asc" ? ( | ||
| <ArrowDownAZ className="h-4 w-4" /> | ||
| ) : ( | ||
| <ArrowUpZA className="h-4 w-4" /> | ||
| )} | ||
| </Button> | ||
| </div> | ||
| </div> | ||
|
|
||
| <TabsContent | ||
| value={tab} | ||
| className="flex min-h-0 min-w-0 flex-1 flex-col overflow-hidden focus-visible:outline-none" | ||
| > | ||
| <UsersDataTable | ||
| columns={usersColumns} | ||
| data={filtered} | ||
| emptyMessage="No users match the current filters." | ||
| rowLabel={(n) => `${n} user${n === 1 ? "" : "s"}`} | ||
| /> | ||
| </TabsContent> | ||
| </Tabs> | ||
| </div> | ||
| ); | ||
| } |
129 changes: 129 additions & 0 deletions
129
app/(authenticated)/users/_components/users-columns.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| "use client"; | ||
|
|
||
| import { ColumnDef } from "@tanstack/react-table"; | ||
| import { Pencil, Tag } from "lucide-react"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { Badge } from "@/components/ui/badge"; | ||
| import { Avatar, AvatarFallback} from "@/components/ui/avatar"; | ||
| import { | ||
| Tooltip, | ||
| TooltipContent, | ||
| TooltipTrigger, | ||
| } from "@/components/ui/tooltip"; | ||
|
|
||
| import { profileRoleLabel, type UserRow } from "../profile-role-label"; | ||
|
|
||
| export const usersColumns: ColumnDef<UserRow>[] = [ | ||
| { | ||
| id: "profile", | ||
| header: "User Profile", | ||
| meta: { | ||
| colWidth: "32%", | ||
| tdClassName: "whitespace-normal align-middle", | ||
| }, | ||
| cell: ({ row }) => { | ||
| const u = row.original; | ||
| const fullName = `${u.firstName} ${u.lastName}`; | ||
| return ( | ||
| <div className="flex min-w-0 max-w-full items-center gap-2 sm:gap-3"> | ||
| <Avatar> | ||
| <AvatarFallback className="bg-muted text-xs font-semibold text-muted-foreground"> | ||
| {`${u.firstName[0] ?? ""}${u.lastName[0] ?? ""}`.toUpperCase()} | ||
| </AvatarFallback> | ||
| </Avatar> | ||
| <div className="flex min-w-0 flex-1 flex-col"> | ||
| <span className="truncate font-semibold text-sm"> | ||
| {fullName} | ||
| </span> | ||
| <span className="truncate text-xs text-muted-foreground"> | ||
| {u.email} | ||
| </span> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }, | ||
| }, | ||
| { | ||
| accessorKey: "role", | ||
| header: "Role", | ||
| meta: { colWidth: "14%" }, | ||
| cell: ({ row }) => ( | ||
| <span className="inline-flex max-w-full min-w-0 items-center rounded-full border border-border px-2 py-0.5 text-xs font-medium capitalize text-foreground"> | ||
| <span className="truncate"> | ||
| {profileRoleLabel(row.original.role)} | ||
| </span> | ||
| </span> | ||
| ), | ||
| }, | ||
| { | ||
| id: "status", | ||
| header: "Status", | ||
| meta: { colWidth: "14%" }, | ||
| cell: ({ row }) => { | ||
| const active = row.original.isActive; | ||
| return ( | ||
| <Badge variant={active ? "default" : "secondary"} className="gap-1.5"> | ||
| <span | ||
| className={`h-2 w-2 rounded-full ${ | ||
| active ? "bg-green-500" : "bg-muted-foreground/50" | ||
| }`} | ||
| /> | ||
| {active ? "Active" : "Inactive"} | ||
| </Badge> | ||
| ); | ||
| }, | ||
| }, | ||
| { | ||
| id: "lastLoginAt", | ||
| header: "Last Login", | ||
| meta: { colWidth: "18%" }, | ||
| cell: ({ row }) => ( | ||
| <span className="block min-w-0 truncate text-sm text-muted-foreground"> | ||
| {new Intl.DateTimeFormat("en-CA", { | ||
| year: "numeric", | ||
| month: "short", | ||
| day: "numeric", | ||
| }).format(new Date(row.original.lastLoginAt || "Never Logged in" ))} | ||
| </span> | ||
| ), | ||
| }, | ||
| { | ||
| id: "actions", | ||
| header: () => <div className="text-right">Actions</div>, | ||
| meta: { | ||
| colWidth: "22%", | ||
| thClassName: "text-right", | ||
| tdClassName: "text-right", | ||
| }, | ||
| cell: () => ( | ||
| <div className="flex items-center justify-end gap-0.5"> | ||
| <Tooltip> | ||
| <TooltipTrigger asChild> | ||
| <Button | ||
| variant="ghost" | ||
| size="icon-sm" | ||
| aria-label="Edit user" | ||
| disabled | ||
| > | ||
| <Pencil /> | ||
| </Button> | ||
| </TooltipTrigger> | ||
| <TooltipContent>Edit (coming soon)</TooltipContent> | ||
| </Tooltip> | ||
| <Tooltip> | ||
| <TooltipTrigger asChild> | ||
| <Button | ||
| variant="ghost" | ||
| size="icon-sm" | ||
| aria-label="Give discount" | ||
| disabled | ||
| > | ||
| <Tag /> | ||
| </Button> | ||
| </TooltipTrigger> | ||
| <TooltipContent>Give coupon (coming soon)</TooltipContent> | ||
| </Tooltip> | ||
| </div> | ||
| ), | ||
| }, | ||
| ]; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed