Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | 82x 82x 82x 82x 82x 82x 82x 82x 82x 82x 82x 82x 82x 82x 3x 3x 3x 3x 2x 2x 2x 1x 1x 3x 82x 3x 3x 1x 2x 2x 1x 1x 1x 1x 82x 1x 1x 1x 82x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 82x 4x 4x 3x 3x 1x 1x 2x 2x 2x 82x 4x 4x 4x 3x 3x 2x 2x 2x 1x 1x 82x 5x 5x 82x 36x 46x 4x 42x | import { useState } from "react";
import { Link, useParams, useNavigate } from "@tanstack/react-router";
import { useQueryClient } from "@tanstack/react-query";
import { Breadcrumb } from "../../components/ui/breadcrumb";
import { UserActionsCard } from "../../components/admin/UserActionsCard";
import { UserInfoCard } from "../../components/admin/UserInfoCard";
import { UserRolesSection } from "../../components/admin/UserRolesSection";
import {
adminApi,
type AdminUser,
} from "../../lib/api";
import { notify } from "../../lib/notify";
import { useConfirmDialog } from "../../hooks";
import { Skeleton } from "../../components/ui/skeleton";
import {
useAdminUser,
useAdminProLookup,
} from "../../hooks/queries/useAdminQueries";
import { queryKeys } from "../../lib/query-keys";
interface ApiErrorWithMessage {
message?: string;
}
export function AdminUserDetailPage() {
const { userId } = useParams({ strict: false });
const navigate = useNavigate();
const queryClient = useQueryClient();
const { confirm, dialog: confirmDialog } = useConfirmDialog();
const [isSaving, setIsSaving] = useState(false);
// React Query hooks
const { data: userData, isLoading: userLoading } = useAdminUser(userId ?? null);
const { data: proLookupData, isLoading: prosLoading } = useAdminProLookup();
// Local mutable copy of user for form editing
const [userEdits, setUserEdits] = useState<AdminUser | null>(null);
const user = userEdits ?? userData?.user ?? null;
const roles = userData?.roles ?? [];
const pros = proLookupData ?? [];
const isLoading = userLoading || prosLoading;
const hasChanges = userEdits !== null;
const handleSave = async () => {
Iif (!user || !userId || !hasChanges) return;
setIsSaving(true);
try {
await adminApi.updateUser(userId, {
name: user.name,
email: user.email,
phoneNumber: user.phoneNumber,
});
setUserEdits(null);
queryClient.invalidateQueries({ queryKey: queryKeys.admin.users.detail(userId) });
notify.success("User saved successfully");
} catch (err) {
console.error("Failed to save user:", err);
notify.error("Failed to save changes");
} finally {
setIsSaving(false);
}
};
const handleDelete = async () => {
Iif (!userId) return;
if (
!(await confirm({
title: "Delete user",
description: "Are you sure you want to delete this user? This action cannot be undone.",
confirmLabel: "Delete",
variant: "destructive",
}))
) {
return;
}
try {
await adminApi.deleteUser(userId);
navigate({ to: "/admin/users" });
} catch (err: unknown) {
console.error("Failed to delete user:", err);
const apiError = err as ApiErrorWithMessage;
notify.error(apiError.message || "Failed to delete user");
}
};
const handlePasswordReset = async (password: string) => {
Iif (!userId) return;
await adminApi.resetUserPassword(userId, password);
notify.success("Password reset successfully");
};
const handleAddRole = async (role: {
tenantType: "platform" | "pro";
tenantId?: string;
role: string;
}) => {
Iif (!userId) return;
try {
const result = await adminApi.addUserRole(userId, {
tenantType: role.tenantType,
tenantId: role.tenantId,
role: role.role,
});
Eif (result.data) {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.users.detail(userId) });
notify.success("Role added successfully");
}
} catch (err: unknown) {
console.error("Failed to add role:", err);
const apiError = err as ApiErrorWithMessage;
notify.error(apiError.message || "Failed to add role");
throw err; // Re-throw so UserRolesSection knows it failed
}
};
const handleRemoveRole = async (roleId: number) => {
Iif (!userId) return;
if (!(await confirm({ title: "Remove role", description: "Are you sure you want to remove this role?", confirmLabel: "Remove", variant: "destructive" }))) return;
try {
await adminApi.removeUserRole(userId, roleId);
queryClient.invalidateQueries({ queryKey: queryKeys.admin.users.detail(userId) });
notify.success("Role removed successfully");
} catch (err: unknown) {
console.error("Failed to remove role:", err);
const apiError = err as ApiErrorWithMessage;
notify.error(apiError.message || "Failed to remove role");
}
};
const handleToggleBan = async () => {
Iif (!user || !userId) return;
const action = user.banned ? "unban" : "ban";
if (!(await confirm({ title: `${action.charAt(0).toUpperCase() + action.slice(1)} user`, description: `Are you sure you want to ${action} ${user.name}?`, confirmLabel: action.charAt(0).toUpperCase() + action.slice(1), variant: action === "ban" ? "destructive" : "default" }))) return;
try {
await adminApi.banUser(userId, !user.banned);
setUserEdits(null);
queryClient.invalidateQueries({ queryKey: queryKeys.admin.users.detail(userId) });
notify.success(`User ${action}ned successfully`);
} catch (err) {
console.error(`Failed to ${action} user:`, err);
notify.error(`Failed to ${action} user`);
}
};
const updateField = (field: keyof AdminUser, value: unknown) => {
Iif (!user) return;
setUserEdits({ ...user, [field]: value });
};
if (isLoading) {
return (
<div className="space-y-6">
<Skeleton className="h-8 w-48" />
<Skeleton className="h-64 w-full" />
<Skeleton className="h-80 w-full" />
</div>
);
}
if (!user) {
return (
<div className="text-center py-12">
<p className="text-foreground-muted">User not found</p>
<Link to="/admin/users" className="text-primary-600 hover:underline">
Back to users
</Link>
</div>
);
}
return (
<>
<div className="space-y-6">
{/* Breadcrumb */}
<Breadcrumb
items={[
{ label: "Users", href: "/admin/users" },
{ label: user.name },
]}
/>
<UserActionsCard
user={user}
isSaving={isSaving}
hasChanges={hasChanges}
onSave={handleSave}
onDelete={handleDelete}
onToggleBan={handleToggleBan}
onResetPassword={handlePasswordReset}
/>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
<UserInfoCard user={user} onUpdateField={updateField} />
</div>
<UserRolesSection
roles={roles}
pros={pros}
onAddRole={handleAddRole}
onRemoveRole={handleRemoveRole}
/>
</div>
{confirmDialog}
</>
);
}
|