AdminPage.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. import { useState, useMemo, useEffect, useRef } from "react";
  2. import { useUsers } from "@/hooks/useUsers";
  3. import { useTranslation } from "react-i18next";
  4. import {
  5. Table,
  6. TableBody,
  7. TableCell,
  8. TableHead,
  9. TableHeader,
  10. TableRow,
  11. } from "@/components/ui/table";
  12. import {
  13. Card,
  14. CardContent,
  15. CardDescription,
  16. CardHeader,
  17. CardTitle,
  18. } from "@/components/ui/card";
  19. import { Input } from "@/components/ui/input";
  20. import { Button } from "@/components/ui/button";
  21. import {
  22. AlertDialog,
  23. AlertDialogAction,
  24. AlertDialogCancel,
  25. AlertDialogContent,
  26. AlertDialogDescription,
  27. AlertDialogHeader,
  28. AlertDialogTitle,
  29. } from "@/components/ui/alert-dialog";
  30. import { Trash2, ArrowUpDown, ArrowUp, ArrowDown, ChevronLeft, ChevronRight } from "lucide-react";
  31. type SortField = "firstName" | "lastName" | "email" | "phoneNumber";
  32. type SortOrder = "asc" | "desc" | null;
  33. type TabType = "customers" | "couriers";
  34. const UserTable = ({
  35. users,
  36. onDelete,
  37. isDeleting,
  38. t,
  39. page,
  40. size,
  41. totalCount,
  42. totalPages,
  43. onPageChange,
  44. onSizeChange,
  45. loading,
  46. }: {
  47. users: any[];
  48. onDelete: (user: any) => void;
  49. isDeleting: string | null;
  50. t: any;
  51. page: number;
  52. size: number;
  53. totalCount: number;
  54. totalPages: number;
  55. onPageChange: (page: number) => void;
  56. onSizeChange: (size: number) => void;
  57. loading: boolean;
  58. }) => {
  59. const [sortField, setSortField] = useState<SortField | null>(null);
  60. const [sortOrder, setSortOrder] = useState<SortOrder>(null);
  61. const handleSort = (field: SortField) => {
  62. if (sortField === field) {
  63. if (sortOrder === "asc") {
  64. setSortOrder("desc");
  65. } else if (sortOrder === "desc") {
  66. setSortOrder(null);
  67. setSortField(null);
  68. }
  69. } else {
  70. setSortField(field);
  71. setSortOrder("asc");
  72. }
  73. };
  74. // Sortuj użytkowników (bez filtrowania, bo filtrowanie już robi backend)
  75. const displayedUsers = useMemo(() => {
  76. const displayList = [...users];
  77. if (sortField && sortOrder) {
  78. displayList.sort((a, b) => {
  79. const aValue = (a[sortField] || "").toString().toLowerCase();
  80. const bValue = (b[sortField] || "").toString().toLowerCase();
  81. if (aValue < bValue) return sortOrder === "asc" ? -1 : 1;
  82. if (aValue > bValue) return sortOrder === "asc" ? 1 : -1;
  83. return 0;
  84. });
  85. }
  86. return displayList;
  87. }, [users, sortField, sortOrder]);
  88. return (
  89. <div className="space-y-4">
  90. <div className="rounded-lg border overflow-hidden relative">
  91. {loading && (
  92. <div className="absolute inset-0 flex items-center justify-center bg-background/80 backdrop-blur-sm z-10 rounded-lg">
  93. <div className="flex flex-col items-center gap-2">
  94. <div className="animate-spin">
  95. <ChevronRight className="h-8 w-8 text-primary" />
  96. </div>
  97. <p className="text-muted-foreground text-sm">{t("common.loading")}</p>
  98. </div>
  99. </div>
  100. )}
  101. <Table>
  102. <TableHeader>
  103. <TableRow>
  104. <TableHead>{t("admin.users.id") || "ID"}</TableHead>
  105. <TableHead>
  106. <button
  107. onClick={() => handleSort("firstName")}
  108. className="flex items-center gap-2 hover:text-primary"
  109. >
  110. {t("admin.users.firstName") || "Imię"}
  111. {sortField !== "firstName" ? (
  112. <ArrowUpDown className="h-4 w-4" />
  113. ) : sortOrder === "asc" ? (
  114. <ArrowUp className="h-4 w-4" />
  115. ) : (
  116. <ArrowDown className="h-4 w-4" />
  117. )}
  118. </button>
  119. </TableHead>
  120. <TableHead>
  121. <button
  122. onClick={() => handleSort("lastName")}
  123. className="flex items-center gap-2 hover:text-primary"
  124. >
  125. {t("admin.users.lastName") || "Nazwisko"}
  126. {sortField !== "lastName" ? (
  127. <ArrowUpDown className="h-4 w-4" />
  128. ) : sortOrder === "asc" ? (
  129. <ArrowUp className="h-4 w-4" />
  130. ) : (
  131. <ArrowDown className="h-4 w-4" />
  132. )}
  133. </button>
  134. </TableHead>
  135. <TableHead>
  136. <button
  137. onClick={() => handleSort("email")}
  138. className="flex items-center gap-2 hover:text-primary"
  139. >
  140. {t("admin.users.email") || "Email"}
  141. {sortField !== "email" ? (
  142. <ArrowUpDown className="h-4 w-4" />
  143. ) : sortOrder === "asc" ? (
  144. <ArrowUp className="h-4 w-4" />
  145. ) : (
  146. <ArrowDown className="h-4 w-4" />
  147. )}
  148. </button>
  149. </TableHead>
  150. <TableHead>
  151. <button
  152. onClick={() => handleSort("phoneNumber")}
  153. className="flex items-center gap-2 hover:text-primary"
  154. >
  155. {t("admin.users.phoneNumber") || "Telefon"}
  156. {sortField !== "phoneNumber" ? (
  157. <ArrowUpDown className="h-4 w-4" />
  158. ) : sortOrder === "asc" ? (
  159. <ArrowUp className="h-4 w-4" />
  160. ) : (
  161. <ArrowDown className="h-4 w-4" />
  162. )}
  163. </button>
  164. </TableHead>
  165. <TableHead>{t("admin.users.createdAt") || "Data Utworzenia"}</TableHead>
  166. <TableHead className="text-right">{t("common.actions") || "Akcje"}</TableHead>
  167. </TableRow>
  168. </TableHeader>
  169. <TableBody>
  170. {displayedUsers.length > 0 ? (
  171. displayedUsers.map((user) => (
  172. <TableRow key={user.id}>
  173. <TableCell className="font-mono text-sm">{user.id}</TableCell>
  174. <TableCell>{user.firstName || "-"}</TableCell>
  175. <TableCell>{user.lastName || "-"}</TableCell>
  176. <TableCell>{user.email}</TableCell>
  177. <TableCell>{user.phoneNumber || "-"}</TableCell>
  178. <TableCell>
  179. {user.createdAt
  180. ? new Date(user.createdAt).toLocaleDateString("pl-PL")
  181. : "-"}
  182. </TableCell>
  183. <TableCell className="text-right">
  184. <Button
  185. variant="destructive"
  186. size="sm"
  187. onClick={() => onDelete(user)}
  188. disabled={isDeleting === user.id}
  189. >
  190. <Trash2 className="h-4 w-4" />
  191. </Button>
  192. </TableCell>
  193. </TableRow>
  194. ))
  195. ) : (
  196. <TableRow>
  197. <TableCell colSpan={7} className="text-muted-foreground text-center">
  198. {t("admin.users.noUsers") || "Brak użytkowników"}
  199. </TableCell>
  200. </TableRow>
  201. )}
  202. </TableBody>
  203. </Table>
  204. </div>
  205. {/* Pagination Controls */}
  206. <div className="flex items-center justify-between gap-4">
  207. <div className="flex items-center gap-2">
  208. <label className="text-sm text-muted-foreground">
  209. {t("admin.fleet.itemsPerPage") || "Pozycji na stronę"}:
  210. </label>
  211. <select
  212. value={size}
  213. onChange={(e) => onSizeChange(parseInt(e.target.value))}
  214. className="px-2 py-1 border rounded text-sm"
  215. >
  216. <option value={10}>10</option>
  217. <option value={20}>20</option>
  218. <option value={50}>50</option>
  219. </select>
  220. </div>
  221. <div className="text-sm text-muted-foreground">
  222. {t("admin.users.totalUsers") || "Razem użytkowników"}: {totalCount}
  223. </div>
  224. <div className="flex gap-2">
  225. <Button
  226. variant="outline"
  227. size="sm"
  228. onClick={() => onPageChange(page - 1)}
  229. disabled={page === 0 || loading}
  230. >
  231. <ChevronLeft className="h-4 w-4" />
  232. </Button>
  233. <div className="flex items-center gap-2">
  234. {Array.from({ length: totalPages }, (_, i) => i).map((p) => (
  235. <Button
  236. key={p}
  237. variant={page === p ? "default" : "outline"}
  238. size="sm"
  239. onClick={() => onPageChange(p)}
  240. disabled={loading}
  241. className="min-w-10"
  242. >
  243. {p + 1}
  244. </Button>
  245. ))}
  246. </div>
  247. <Button
  248. variant="outline"
  249. size="sm"
  250. onClick={() => onPageChange(page + 1)}
  251. disabled={page >= totalPages - 1 || loading}
  252. >
  253. <ChevronRight className="h-4 w-4" />
  254. </Button>
  255. </div>
  256. </div>
  257. </div>
  258. );
  259. };
  260. export const AdminPage = () => {
  261. const { t } = useTranslation();
  262. const {
  263. users,
  264. loading,
  265. error,
  266. deleteUser,
  267. page,
  268. size,
  269. totalCount,
  270. totalPages,
  271. countByType,
  272. fetchUsersByTypePaged,
  273. searchUsersByType,
  274. } = useUsers();
  275. const [activeTab, setActiveTab] = useState<TabType>("customers");
  276. const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
  277. const [userToDelete, setUserToDelete] = useState<{ id: string; email: string } | null>(null);
  278. const [isDeleting, setIsDeleting] = useState<string | null>(null);
  279. const [searchTerm, setSearchTerm] = useState("");
  280. const debounceTimer = useRef<NodeJS.Timeout | null>(null);
  281. // Pobierz użytkowników po typie gdy zmienia się aktywna zakladka
  282. useEffect(() => {
  283. const userType = activeTab === "customers" ? "CUSTOMER" : "COURIER";
  284. setSearchTerm("");
  285. fetchUsersByTypePaged(userType, 0, size);
  286. }, [activeTab]);
  287. // Wyszukuj użytkowników gdy zmienia się searchTerm - z debouncing
  288. useEffect(() => {
  289. if (debounceTimer.current) {
  290. clearTimeout(debounceTimer.current);
  291. }
  292. const userType = activeTab === "customers" ? "CUSTOMER" : "COURIER";
  293. debounceTimer.current = setTimeout(() => {
  294. if (searchTerm.trim()) {
  295. searchUsersByType(userType, searchTerm, 0, size);
  296. } else {
  297. fetchUsersByTypePaged(userType, 0, size);
  298. }
  299. }, 300); // Czeka 300ms zanim wyśle żądanie
  300. return () => {
  301. if (debounceTimer.current) {
  302. clearTimeout(debounceTimer.current);
  303. }
  304. };
  305. // eslint-disable-next-line react-hooks/exhaustive-deps
  306. }, [searchTerm]);
  307. // Niestandardowe handlery paginacji dla wyszukiwania
  308. const handlePageChangeWithSearch = (newPage: number) => {
  309. const userType = activeTab === "customers" ? "CUSTOMER" : "COURIER";
  310. if (searchTerm.trim()) {
  311. searchUsersByType(userType, searchTerm, newPage, size);
  312. } else {
  313. fetchUsersByTypePaged(userType, newPage, size);
  314. }
  315. };
  316. const handleSizeChangeWithSearch = (newSize: number) => {
  317. const userType = activeTab === "customers" ? "CUSTOMER" : "COURIER";
  318. if (searchTerm.trim()) {
  319. searchUsersByType(userType, searchTerm, 0, newSize);
  320. } else {
  321. fetchUsersByTypePaged(userType, 0, newSize);
  322. }
  323. };
  324. const handleDeleteClick = (user: any) => {
  325. setUserToDelete({ id: user.id, email: user.email });
  326. setDeleteDialogOpen(true);
  327. };
  328. const handleConfirmDelete = async () => {
  329. if (!userToDelete) return;
  330. setIsDeleting(userToDelete.id);
  331. const result = await deleteUser(userToDelete.id);
  332. if (result.success) {
  333. setDeleteDialogOpen(false);
  334. setUserToDelete(null);
  335. } else {
  336. console.error("Delete failed:", result.error);
  337. }
  338. setIsDeleting(null);
  339. };
  340. return (
  341. <div className={`container mx-auto max-w-6xl px-4 py-8 transition-opacity duration-200 ${loading ? 'opacity-60 pointer-events-none' : 'opacity-100'}`}>
  342. {error && (
  343. <div className="mb-4 rounded-lg bg-destructive/10 p-4">
  344. <p className="text-destructive font-semibold">{t("common.error")}</p>
  345. <p className="text-muted-foreground text-sm">{error}</p>
  346. </div>
  347. )}
  348. <div className="mb-8">
  349. <h1 className="text-4xl font-bold">
  350. {t("admin.users.title") || "Zarządzanie Użytkownikami"}
  351. </h1>
  352. <p className="text-muted-foreground mt-2">
  353. {t("admin.users.manageDesc") || "Zarządzaj użytkownikami systemu, dodawaj, edytuj i usuwaj konta"}
  354. </p>
  355. </div>
  356. {/* Stats Cards */}
  357. {countByType && (
  358. <div className="grid grid-cols-1 md:grid-cols-4 gap-4 mb-8">
  359. <Card>
  360. <CardHeader className="pb-2">
  361. <CardTitle className="text-sm font-medium text-muted-foreground">
  362. {t("admin.users.totalUsers") || "Razem Użytkowników"}
  363. </CardTitle>
  364. </CardHeader>
  365. <CardContent>
  366. <div className="text-2xl font-bold">{countByType.totalUsers}</div>
  367. </CardContent>
  368. </Card>
  369. <Card>
  370. <CardHeader className="pb-2">
  371. <CardTitle className="text-sm font-medium text-muted-foreground">
  372. {t("admin.users.customers") || "Klienci"}
  373. </CardTitle>
  374. </CardHeader>
  375. <CardContent>
  376. <div className="text-2xl font-bold">{countByType.customerCount}</div>
  377. </CardContent>
  378. </Card>
  379. <Card>
  380. <CardHeader className="pb-2">
  381. <CardTitle className="text-sm font-medium text-muted-foreground">
  382. {t("admin.users.couriers") || "Kurierzy"}
  383. </CardTitle>
  384. </CardHeader>
  385. <CardContent>
  386. <div className="text-2xl font-bold">{countByType.courierCount}</div>
  387. </CardContent>
  388. </Card>
  389. <Card>
  390. <CardHeader className="pb-2">
  391. <CardTitle className="text-sm font-medium text-muted-foreground">
  392. {t("admin.users.admins") || "Administratorzy"}
  393. </CardTitle>
  394. </CardHeader>
  395. <CardContent>
  396. <div className="text-2xl font-bold">{countByType.adminCount}</div>
  397. </CardContent>
  398. </Card>
  399. </div>
  400. )}
  401. {/* Search */}
  402. <div className="mb-6">
  403. <Input
  404. placeholder={t("admin.users.search") || "Szukaj po imieniu, nazwisku lub emailu..."}
  405. value={searchTerm}
  406. onChange={(e) => setSearchTerm(e.target.value)}
  407. className="max-w-sm"
  408. />
  409. </div>
  410. {/* Tab Navigation */}
  411. <div className="flex gap-2 border-b mb-8">
  412. <button
  413. onClick={() => setActiveTab("customers")}
  414. className={`flex items-center gap-2 px-4 py-3 font-medium transition-colors ${
  415. activeTab === "customers"
  416. ? "border-b-2 border-primary text-primary"
  417. : "text-muted-foreground hover:text-foreground"
  418. }`}
  419. >
  420. {t("admin.users.customers") || "Klienci"}
  421. <span className="text-sm bg-muted rounded-full px-2 py-1">{countByType?.customerCount || 0}</span>
  422. </button>
  423. <button
  424. onClick={() => setActiveTab("couriers")}
  425. className={`flex items-center gap-2 px-4 py-3 font-medium transition-colors ${
  426. activeTab === "couriers"
  427. ? "border-b-2 border-primary text-primary"
  428. : "text-muted-foreground hover:text-foreground"
  429. }`}
  430. >
  431. {t("admin.users.couriers") || "Kurierzy"}
  432. <span className="text-sm bg-muted rounded-full px-2 py-1">{countByType?.courierCount || 0}</span>
  433. </button>
  434. </div>
  435. {/* Customers Tab */}
  436. {activeTab === "customers" && (
  437. <Card>
  438. <CardHeader>
  439. <CardTitle>{t("admin.users.customers") || "Klienci"}</CardTitle>
  440. <CardDescription>
  441. {t("admin.users.customersDesc") || "Lista wszystkich zarejestrowanych klientów"}
  442. </CardDescription>
  443. </CardHeader>
  444. <CardContent>
  445. <UserTable
  446. key="customers"
  447. users={users}
  448. onDelete={handleDeleteClick}
  449. isDeleting={isDeleting}
  450. t={t}
  451. page={page}
  452. size={size}
  453. totalCount={totalCount}
  454. totalPages={totalPages}
  455. onPageChange={handlePageChangeWithSearch}
  456. onSizeChange={handleSizeChangeWithSearch}
  457. loading={loading}
  458. />
  459. </CardContent>
  460. </Card>
  461. )}
  462. {/* Couriers Tab */}
  463. {activeTab === "couriers" && (
  464. <Card>
  465. <CardHeader>
  466. <CardTitle>{t("admin.users.couriers") || "Kurierzy"}</CardTitle>
  467. <CardDescription>
  468. {t("admin.users.couriersDesc") || "Lista wszystkich zarejestrowanych kurierów"}
  469. </CardDescription>
  470. </CardHeader>
  471. <CardContent>
  472. <UserTable
  473. key="couriers"
  474. users={users}
  475. onDelete={handleDeleteClick}
  476. isDeleting={isDeleting}
  477. t={t}
  478. page={page}
  479. size={size}
  480. totalCount={totalCount}
  481. totalPages={totalPages}
  482. onPageChange={handlePageChangeWithSearch}
  483. onSizeChange={handleSizeChangeWithSearch}
  484. loading={loading}
  485. />
  486. </CardContent>
  487. </Card>
  488. )}
  489. {/* Delete User Dialog */}
  490. <AlertDialog open={deleteDialogOpen} onOpenChange={setDeleteDialogOpen}>
  491. <AlertDialogContent>
  492. <AlertDialogHeader>
  493. <AlertDialogTitle>
  494. {t("common.confirmDelete") || "Potwierdzenie usunięcia"}
  495. </AlertDialogTitle>
  496. <AlertDialogDescription>
  497. {t("admin.users.deleteWarning") || "Czy na pewno chcesz usunąć tego użytkownika"}{" "}
  498. <strong>{userToDelete?.email}</strong>
  499. {t("admin.users.deleteWarningDetails") || "? Ta operacja nie może być cofnięta."}
  500. </AlertDialogDescription>
  501. </AlertDialogHeader>
  502. <AlertDialogAction onClick={handleConfirmDelete} className="bg-destructive">
  503. {isDeleting ? t("common.deleting") || "Usuwanie..." : t("common.delete") || "Usuń"}
  504. </AlertDialogAction>
  505. <AlertDialogCancel>{t("common.cancel") || "Anuluj"}</AlertDialogCancel>
  506. </AlertDialogContent>
  507. </AlertDialog>
  508. </div>
  509. );
  510. };