All files / src/components/admin CreateUserModal.tsx

100% Statements 12/12
100% Branches 6/6
100% Functions 9/9
100% Lines 11/11

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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233                                                                                                  271x   271x   165x                                                                                             20x                                 20x                                 20x                               4x                       10x                                       4x         50x                                 2x                                                        
import { X } from "lucide-react";
import { Button } from "../ui/button";
import { Input } from "../ui/input";
import type { Pro } from "../../lib/api";
import { useDialogAccessibility } from "../../hooks";
 
interface CreateUserModalProps {
	isOpen: boolean;
	newUser: {
		name: string;
		email: string;
		password: string;
		phoneNumber: string;
	};
	pros: Pro[];
	assignToPro: boolean;
	selectedProId: string;
	selectedRole: "owner" | "manager" | "staff";
	isCreating: boolean;
	error: string | null;
	onClose: () => void;
	onUserChange: (user: {
		name: string;
		email: string;
		password: string;
		phoneNumber: string;
	}) => void;
	onAssignToProChange: (value: boolean) => void;
	onSelectedProIdChange: (value: string) => void;
	onSelectedRoleChange: (value: "owner" | "manager" | "staff") => void;
	onSubmit: () => void;
}
 
export function CreateUserModal({
	isOpen,
	newUser,
	pros,
	assignToPro,
	selectedProId,
	selectedRole,
	isCreating,
	error,
	onClose,
	onUserChange,
	onAssignToProChange,
	onSelectedProIdChange,
	onSelectedRoleChange,
	onSubmit,
}: CreateUserModalProps) {
	const { dialogRef, handleFocusTrap } = useDialogAccessibility(onClose);
 
	if (!isOpen) return null;
 
	return (
		<div className="fixed inset-0 z-50 flex items-center justify-center" tabIndex={-1}>
			<button
				type="button"
				aria-label="Close modal"
				className="absolute inset-0 bg-black/50 cursor-default"
				onClick={onClose}
				tabIndex={-1}
			/>
			<div
				ref={dialogRef}
				role="dialog"
				aria-modal="true"
				aria-labelledby="create-user-dialog-title"
				onKeyDown={handleFocusTrap}
				className="relative bg-background-elevated rounded-lg shadow-xl w-full max-w-md mx-4 p-6"
			>
				<div className="flex items-center justify-between mb-4">
					<h2 id="create-user-dialog-title" className="text-lg font-semibold">Create New User</h2>
					<button
						type="button"
						onClick={onClose}
						aria-label="Close"
						className="p-1 hover:bg-background-muted rounded"
					>
						<X className="h-5 w-5" />
					</button>
				</div>
 
				{error && (
					<div className="mb-4 p-3 bg-error-light text-error rounded-md text-sm">
						{error}
					</div>
				)}
 
				<div className="space-y-4">
					<div>
						<label
							htmlFor="new-user-name"
							className="block text-sm font-medium text-foreground-default mb-1"
						>
							Name *
						</label>
						<Input
							id="new-user-name"
							value={newUser.name}
							onChange={(e) =>
								onUserChange({ ...newUser, name: e.target.value })
							}
							placeholder="John Doe"
						/>
					</div>
					<div>
						<label
							htmlFor="new-user-email"
							className="block text-sm font-medium text-foreground-default mb-1"
						>
							Email *
						</label>
						<Input
							id="new-user-email"
							type="email"
							value={newUser.email}
							onChange={(e) =>
								onUserChange({ ...newUser, email: e.target.value })
							}
							placeholder="john@example.com"
						/>
					</div>
					<div>
						<label
							htmlFor="new-user-password"
							className="block text-sm font-medium text-foreground-default mb-1"
						>
							Password *
						</label>
						<Input
							id="new-user-password"
							type="password"
							value={newUser.password}
							onChange={(e) =>
								onUserChange({ ...newUser, password: e.target.value })
							}
							placeholder="Minimum 8 characters"
						/>
					</div>
					<div>
						<label
							htmlFor="new-user-phone"
							className="block text-sm font-medium text-foreground-default mb-1"
						>
							Phone Number
						</label>
						<Input
							id="new-user-phone"
							value={newUser.phoneNumber}
							onChange={(e) =>
								onUserChange({ ...newUser, phoneNumber: e.target.value })
							}
							placeholder="+91 98765 43210"
						/>
					</div>
 
					{/* Pro Assignment */}
					<div className="border-t border-border-default pt-4 mt-4">
						<label className="flex items-center gap-2 cursor-pointer">
							<input
								type="checkbox"
								checked={assignToPro}
								onChange={(e) => onAssignToProChange(e.target.checked)}
								className="rounded border-border-default text-primary-600 focus:ring-primary-500"
							/>
							<span className="text-sm font-medium text-foreground-default">
								Assign to pro
							</span>
						</label>
 
						{assignToPro && (
							<div className="mt-3 space-y-3 pl-6">
								<div>
									<label
										htmlFor="assign-pro"
										className="block text-sm font-medium text-foreground-default mb-1"
									>
										Pro *
									</label>
									<select
										id="assign-pro"
										value={selectedProId}
										onChange={(e) => onSelectedProIdChange(e.target.value)}
										className="h-10 w-full rounded-md border border-border-default bg-background-elevated px-3 text-sm text-foreground-default focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
									>
										<option value="">Select pro</option>
										{pros.map((pro) => (
											<option key={pro.id} value={pro.id}>
												{pro.businessName}
											</option>
										))}
									</select>
								</div>
								<div>
									<label
										htmlFor="assign-role"
										className="block text-sm font-medium text-foreground-default mb-1"
									>
										Role *
									</label>
									<select
										id="assign-role"
										value={selectedRole}
										onChange={(e) =>
											onSelectedRoleChange(
												e.target.value as "owner" | "manager" | "staff",
											)
										}
										className="h-10 w-full rounded-md border border-border-default bg-background-elevated px-3 text-sm text-foreground-default focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
									>
										<option value="owner">Owner</option>
										<option value="manager">Manager</option>
										<option value="staff">Staff</option>
									</select>
								</div>
							</div>
						)}
					</div>
				</div>
 
				<div className="flex justify-end gap-2 mt-6">
					<Button variant="outline" onClick={onClose}>
						Cancel
					</Button>
					<Button onClick={onSubmit} isLoading={isCreating}>
						Create User
					</Button>
				</div>
			</div>
		</div>
	);
}