hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBc35a83aBurak Boduroğlu4 months ago440 commits
| 1 | import { createMemo, createSignal, Show } from "solid-js"; |
| 2 | import type { ColumnDef } from "@tanstack/solid-table"; |
| 3 | import type { Role, User } from "@/api/client"; |
| 4 | import type { MessageKey } from "@/i18n/messages"; |
| 5 | import { Select } from "@/components/ui/select"; |
| 6 | import { Button } from "@/components/ui/button"; |
| 7 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; |
| 8 | import { DataTable } from "@/components/ui/data-table"; |
| 9 | import { TableRowActions } from "@/components/ui/table-row-actions"; |
| 10 | import { IconCheck, IconEye, IconUsers } from "@/components/ui/icons"; |
| 11 | import { ROLES } from "@/lib/roles"; |
| 12 | import { useT } from "@/stores/preferences-context"; |
| 13 | |
| 14 | function displayName(user: User): string { |
| 15 | return [user.name, user.surname].filter(Boolean).join(" ") || "—"; |
| 16 | } |
| 17 | |
| 18 | function UserRoleActions(props: { |
| 19 | user: User; |
| 20 | currentUserId: string; |
| 21 | onRoleChange: (userId: string, role: Role) => Promise<void>; |
| 22 | onParentClick?: (user: User) => void; |
| 23 | }) { |
| 24 | const t = useT(); |
| 25 | const isSelf = () => props.user.id === props.currentUserId; |
| 26 | const [pendingRole, setPendingRole] = createSignal<Role>(props.user.role); |
| 27 | const [confirmOpen, setConfirmOpen] = createSignal(false); |
| 28 | const dirty = () => pendingRole() !== props.user.role; |
| 29 | |
| 30 | return ( |
| 31 | <> |
| 32 | <Select |
| 33 | class="h-9 rounded-md text-xs" |
| 34 | value={pendingRole()} |
| 35 | disabled={isSelf()} |
| 36 | onChange={(e) => setPendingRole(e.currentTarget.value as Role)} |
| 37 | aria-label={t("admin.role")} |
| 38 | > |
| 39 | {ROLES.map((r) => ( |
| 40 | <option value={r}>{t(`role.${r}` as MessageKey)}</option> |
| 41 | ))} |
| 42 | </Select> |
| 43 | <Show when={!isSelf() && dirty()}> |
| 44 | <Button type="button" size="sm" class="mt-2 h-10 rounded-md px-3" onClick={(e) => { e.stopPropagation(); setConfirmOpen(true); }}> |
| 45 | <IconCheck /> |
| 46 | {t("common.update")} |
| 47 | </Button> |
| 48 | </Show> |
| 49 | <ConfirmDialog |
| 50 | open={confirmOpen()} |
| 51 | onOpenChange={setConfirmOpen} |
| 52 | title={t("confirm.updateTitle")} |
| 53 | summary={t("confirm.updateRole", { |
| 54 | user: props.user.username, |
| 55 | from: t(`role.${props.user.role}` as MessageKey), |
| 56 | to: t(`role.${pendingRole()}` as MessageKey), |
| 57 | })} |
| 58 | onConfirm={async () => { |
| 59 | await props.onRoleChange(props.user.id, pendingRole()); |
| 60 | }} |
| 61 | /> |
| 62 | </> |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | export function UserTable(props: { |
| 67 | users: User[]; |
| 68 | currentUserId: string; |
| 69 | onRoleChange: (userId: string, role: Role) => Promise<void>; |
| 70 | onUserClick?: (user: User) => void; |
| 71 | onParentClick?: (user: User) => void; |
| 72 | }) { |
| 73 | const t = useT(); |
| 74 | const searchUser = (user: User, query: string) => |
| 75 | [user.username, displayName(user), user.email, user.id, t(`role.${user.role}` as MessageKey)] |
| 76 | .join(" ") |
| 77 | .toLocaleLowerCase() |
| 78 | .includes(query.toLocaleLowerCase()); |
| 79 | const columns = createMemo<ColumnDef<User>[]>(() => [ |
| 80 | { |
| 81 | accessorKey: "username", |
| 82 | header: t("admin.username"), |
| 83 | size: 220, |
| 84 | minSize: 140, |
| 85 | meta: { cellClass: "truncate font-medium" }, |
| 86 | }, |
| 87 | { |
| 88 | id: "name", |
| 89 | header: t("profile.name"), |
| 90 | size: 180, |
| 91 | minSize: 120, |
| 92 | meta: { cellClass: "truncate" }, |
| 93 | cell: (cell) => displayName(cell.row.original), |
| 94 | }, |
| 95 | { |
| 96 | accessorKey: "email", |
| 97 | header: t("profile.email"), |
| 98 | size: 240, |
| 99 | minSize: 180, |
| 100 | meta: { cellClass: "truncate text-muted-foreground" }, |
| 101 | cell: (cell) => cell.row.original.email || "—", |
| 102 | }, |
| 103 | { |
| 104 | id: "update", |
| 105 | header: t("admin.role"), |
| 106 | size: 208, |
| 107 | minSize: 208, |
| 108 | enableHiding: false, |
| 109 | meta: { headerClass: "w-52", cellClass: "w-52" }, |
| 110 | cell: (cell) => ( |
| 111 | <UserRoleActions user={cell.row.original} currentUserId={props.currentUserId} onRoleChange={props.onRoleChange} onParentClick={props.onParentClick} /> |
| 112 | ), |
| 113 | }, |
| 114 | { |
| 115 | id: "actions", |
| 116 | header: t("common.actions"), |
| 117 | size: 112, |
| 118 | minSize: 112, |
| 119 | enableHiding: false, |
| 120 | meta: { headerClass: "w-28 min-w-28 text-center whitespace-nowrap", cellClass: "w-28 text-center" }, |
| 121 | cell: (cell) => ( |
| 122 | <TableRowActions |
| 123 | label={t("common.actions")} |
| 124 | actions={[ |
| 125 | { |
| 126 | label: t("common.view"), |
| 127 | icon: <IconEye class="h-4 w-4" />, |
| 128 | onSelect: () => props.onUserClick?.(cell.row.original), |
| 129 | }, |
| 130 | ...(cell.row.original.role === "parent" |
| 131 | ? [ |
| 132 | { |
| 133 | label: t("nav.myStudents"), |
| 134 | icon: <IconUsers class="h-4 w-4" />, |
| 135 | onSelect: () => props.onParentClick?.(cell.row.original), |
| 136 | }, |
| 137 | ] |
| 138 | : []), |
| 139 | ]} |
| 140 | /> |
| 141 | ), |
| 142 | }, |
| 143 | ]); |
| 144 | |
| 145 | return ( |
| 146 | <DataTable |
| 147 | title={t("nav.users")} |
| 148 | description={String(props.users.length)} |
| 149 | columns={columns()} |
| 150 | data={props.users} |
| 151 | empty={t("admin.noUsers")} |
| 152 | storageKey="admin-users" |
| 153 | searchPredicate={searchUser} |
| 154 | enablePagination |
| 155 | pageSize={20} |
| 156 | onRowClick={props.onUserClick} |
| 157 | /> |
| 158 | ); |
| 159 | } |