All files / src/components/admin UserActionsCard.tsx

88% Statements 22/25
86.66% Branches 13/15
100% Functions 6/6
91.66% Lines 22/24

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                                                            107x 107x 107x 107x   107x 4x 4x       4x 4x 4x 4x 3x 3x   1x   4x       107x                                           17x                                                                                       1x             3x 3x 3x                                                   9x                    
import { useState } from "react";
import { Link } from "@tanstack/react-router";
import { ArrowLeft, Ban, CheckCircle, Key, Save, Trash2 } from "lucide-react";
import { AdminPageHead } from "./AdminPageHead";
import { Button } from "../ui/button";
import { DrawerDialog } from "../ui/drawer-dialog";
import { ErrorText, Field, Label } from "../ui/field";
import { Input } from "../ui/input";
import { StatusBadge } from "../ui/status-badge";
import type { AdminUser } from "../../lib/api";
 
interface UserActionsCardProps {
	user: AdminUser;
	isSaving: boolean;
	hasChanges?: boolean;
	onSave: () => void;
	onDelete: () => void;
	onToggleBan: () => void;
	onResetPassword: (password: string) => Promise<void>;
}
 
export function UserActionsCard({
	user,
	isSaving,
	hasChanges = true,
	onSave,
	onDelete,
	onToggleBan,
	onResetPassword,
}: UserActionsCardProps) {
	const [showPasswordReset, setShowPasswordReset] = useState(false);
	const [newPassword, setNewPassword] = useState("");
	const [isResettingPassword, setIsResettingPassword] = useState(false);
	const [passwordError, setPasswordError] = useState<string | null>(null);
 
	const handlePasswordReset = async () => {
		Iif (!newPassword) return;
		Iif (newPassword.length < 8) {
			setPasswordError("Password must be at least 8 characters");
			return;
		}
		setIsResettingPassword(true);
		setPasswordError(null);
		try {
			await onResetPassword(newPassword);
			setShowPasswordReset(false);
			setNewPassword("");
		} catch (_err) {
			setPasswordError("Failed to reset password");
		} finally {
			setIsResettingPassword(false);
		}
	};
 
	return (
		<>
			<AdminPageHead
				title={
					<>
						<Link
							to="/admin/users"
							aria-label="Back to users"
							className="rounded-md p-1 text-foreground-muted transition-colors hover:bg-foreground-default/7 hover:text-foreground-default"
						>
							<ArrowLeft className="h-5 w-5" aria-hidden="true" />
						</Link>
						{user.name}
						{/* A ban is a state, so it is a printed code. */}
						{user.banned && <StatusBadge status="Banned" variant="error" />}
					</>
				}
				description={user.email}
				actions={
					<>
						<Button
							variant="outline"
							onClick={() => setShowPasswordReset(true)}
						>
							<Key aria-hidden="true" />
							Reset Password
						</Button>
						<Button
							variant={user.banned ? "outline" : "destructive"}
							onClick={onToggleBan}
						>
							{user.banned ? (
								<>
									<CheckCircle aria-hidden="true" />
									Unban
								</>
							) : (
								<>
									<Ban aria-hidden="true" />
									Ban
								</>
							)}
						</Button>
						<Button variant="destructive" onClick={onDelete}>
							<Trash2 aria-hidden="true" />
							Delete
						</Button>
						<Button
							onClick={onSave}
							isLoading={isSaving}
							disabled={!hasChanges}
						>
							<Save aria-hidden="true" />
							Save Changes
						</Button>
					</>
				}
			/>
 
			{/*
			 * Password Reset — on the DrawerDialog primitive, which carries the
			 * focus trap, the Escape handler and the dialog role the hand-rolled
			 * overlay did not.
			 */}
			<DrawerDialog
				open={showPasswordReset}
				onClose={() => setShowPasswordReset(false)}
				title="Reset Password"
				footer={
					<div className="flex justify-end gap-2">
						<Button
							variant="outline"
							onClick={() => {
								setShowPasswordReset(false);
								setNewPassword("");
								setPasswordError(null);
							}}
						>
							Cancel
						</Button>
						<Button
							onClick={handlePasswordReset}
							isLoading={isResettingPassword}
							disabled={!newPassword || newPassword.length < 8}
						>
							Reset Password
						</Button>
					</div>
				}
			>
				<div className="space-y-4">
					<p className="text-sm text-foreground-muted">
						Enter a new password for {user.name}. The password must be at least
						8 characters long.
					</p>
					<Field controlId="new-password">
						<Label>New Password</Label>
						<Input
							id="new-password"
							type="password"
							value={newPassword}
							onChange={(e) => setNewPassword(e.target.value)}
							placeholder="Enter new password"
						/>
						{passwordError && <ErrorText>{passwordError}</ErrorText>}
					</Field>
				</div>
			</DrawerDialog>
		</>
	);
}