358 lines
10 KiB
JavaScript
358 lines
10 KiB
JavaScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { useSession } from "next-auth/react";
|
|
import { useRouter, useParams } from "next/navigation";
|
|
import Link from "next/link";
|
|
import { Card, CardHeader, CardContent } from "@/components/ui/Card";
|
|
import Button from "@/components/ui/Button";
|
|
import { Input } from "@/components/ui/Input";
|
|
import PageContainer from "@/components/ui/PageContainer";
|
|
import PageHeader from "@/components/ui/PageHeader";
|
|
import { LoadingState } from "@/components/ui/States";
|
|
|
|
export default function EditUserPage() {
|
|
const [user, setUser] = useState(null);
|
|
const [formData, setFormData] = useState({
|
|
name: "",
|
|
username: "",
|
|
role: "user",
|
|
is_active: true,
|
|
initial: "",
|
|
password: ""
|
|
});
|
|
const [loading, setLoading] = useState(true);
|
|
const [saving, setSaving] = useState(false);
|
|
const [error, setError] = useState("");
|
|
const [success, setSuccess] = useState("");
|
|
|
|
const { data: session, status } = useSession();
|
|
const router = useRouter();
|
|
const params = useParams();
|
|
|
|
// Check if user is admin
|
|
useEffect(() => {
|
|
if (status === "loading") return;
|
|
if (!session || session.user.role !== "admin") {
|
|
router.push("/");
|
|
return;
|
|
}
|
|
}, [session, status, router]);
|
|
|
|
// Fetch user data
|
|
useEffect(() => {
|
|
if (session?.user?.role === "admin" && params.id) {
|
|
fetchUser();
|
|
}
|
|
}, [session, params.id]);
|
|
|
|
const fetchUser = async () => {
|
|
try {
|
|
setLoading(true);
|
|
const response = await fetch(`/api/admin/users/${params.id}`);
|
|
|
|
if (!response.ok) {
|
|
if (response.status === 404) {
|
|
setError("User not found");
|
|
return;
|
|
}
|
|
throw new Error("Failed to fetch user");
|
|
}
|
|
|
|
const userData = await response.json();
|
|
setUser(userData);
|
|
setFormData({
|
|
name: userData.name,
|
|
username: userData.username,
|
|
role: userData.role,
|
|
is_active: userData.is_active,
|
|
initial: userData.initial || "",
|
|
password: "" // Never populate password field
|
|
});
|
|
} catch (err) {
|
|
setError(err.message);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleSubmit = async (e) => {
|
|
e.preventDefault();
|
|
setSaving(true);
|
|
setError("");
|
|
setSuccess("");
|
|
|
|
try {
|
|
// Prepare update data (exclude empty password)
|
|
const updateData = {
|
|
name: formData.name,
|
|
username: formData.username,
|
|
role: formData.role,
|
|
is_active: formData.is_active,
|
|
initial: formData.initial.trim() || null
|
|
};
|
|
|
|
// Only include password if it's provided
|
|
if (formData.password.trim()) {
|
|
if (formData.password.length < 6) {
|
|
throw new Error("Password must be at least 6 characters long");
|
|
}
|
|
updateData.password = formData.password;
|
|
}
|
|
|
|
const response = await fetch(`/api/admin/users/${params.id}`, {
|
|
method: "PUT",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(updateData),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json();
|
|
throw new Error(errorData.error || "Failed to update user");
|
|
}
|
|
|
|
const updatedUser = await response.json();
|
|
setUser(updatedUser);
|
|
setSuccess("User updated successfully");
|
|
|
|
// Clear password field after successful update
|
|
setFormData(prev => ({ ...prev, password: "" }));
|
|
|
|
} catch (err) {
|
|
setError(err.message);
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
};
|
|
|
|
if (status === "loading" || !session) {
|
|
return <LoadingState />;
|
|
}
|
|
|
|
if (session.user.role !== "admin") {
|
|
return (
|
|
<PageContainer>
|
|
<div className="text-center py-12">
|
|
<h2 className="text-2xl font-bold text-gray-900 mb-4">Access Denied</h2>
|
|
<p className="text-gray-600 mb-6">You need admin privileges to access this page.</p>
|
|
<Link href="/">
|
|
<Button>Go Home</Button>
|
|
</Link>
|
|
</div>
|
|
</PageContainer>
|
|
);
|
|
}
|
|
|
|
if (loading) {
|
|
return <LoadingState />;
|
|
}
|
|
|
|
if (error && !user) {
|
|
return (
|
|
<PageContainer>
|
|
<div className="text-center py-12">
|
|
<h2 className="text-2xl font-bold text-gray-900 mb-4">Error</h2>
|
|
<p className="text-gray-600 mb-6">{error}</p>
|
|
<Link href="/admin/users">
|
|
<Button>Back to Users</Button>
|
|
</Link>
|
|
</div>
|
|
</PageContainer>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<PageContainer>
|
|
<PageHeader
|
|
title={`Edit User: ${user?.name}`}
|
|
description="Update user information and permissions"
|
|
>
|
|
<Link href="/admin/users">
|
|
<Button variant="outline">
|
|
<svg className="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10 19l-7-7m0 0l7-7m-7 7h18" />
|
|
</svg>
|
|
Back to Users
|
|
</Button>
|
|
</Link>
|
|
</PageHeader>
|
|
|
|
{error && (
|
|
<div className="mb-6 p-4 bg-red-50 border border-red-200 rounded-md">
|
|
<p className="text-red-600">{error}</p>
|
|
</div>
|
|
)}
|
|
|
|
{success && (
|
|
<div className="mb-6 p-4 bg-green-50 border border-green-200 rounded-md">
|
|
<p className="text-green-600">{success}</p>
|
|
</div>
|
|
)}
|
|
|
|
<Card>
|
|
<CardHeader>
|
|
<h3 className="text-lg font-semibold">User Information</h3>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<form onSubmit={handleSubmit} className="space-y-6">
|
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
|
<div>
|
|
<label className="block text-sm font-medium text-gray-700 mb-2">
|
|
Name *
|
|
</label>
|
|
<Input
|
|
type="text"
|
|
value={formData.name}
|
|
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
|
|
required
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-medium text-gray-700 mb-2">
|
|
Username *
|
|
</label>
|
|
<Input
|
|
type="text"
|
|
value={formData.username}
|
|
onChange={(e) => setFormData({ ...formData, username: e.target.value })}
|
|
required
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-medium text-gray-700 mb-2">
|
|
Role *
|
|
</label>
|
|
<select
|
|
value={formData.role}
|
|
onChange={(e) => setFormData({ ...formData, role: e.target.value })}
|
|
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
|
|
required
|
|
>
|
|
<option value="read_only">Read Only</option>
|
|
<option value="user">User</option>
|
|
<option value="project_manager">Project Manager</option>
|
|
<option value="team_lead">Team Lead</option>
|
|
<option value="admin">Admin</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-medium text-gray-700 mb-2">
|
|
New Password
|
|
</label>
|
|
<Input
|
|
type="password"
|
|
value={formData.password}
|
|
onChange={(e) => setFormData({ ...formData, password: e.target.value })}
|
|
placeholder="Leave blank to keep current password"
|
|
minLength={6}
|
|
/>
|
|
<p className="text-xs text-gray-500 mt-1">
|
|
Leave blank to keep the current password
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-medium text-gray-700 mb-2">
|
|
Initial
|
|
</label>
|
|
<Input
|
|
type="text"
|
|
value={formData.initial}
|
|
onChange={(e) => setFormData({ ...formData, initial: e.target.value })}
|
|
placeholder="1-2 letter identifier"
|
|
maxLength={2}
|
|
className="w-full md:w-1/2"
|
|
/>
|
|
<p className="text-xs text-gray-500 mt-1">
|
|
Optional 1-2 letter identifier for the user
|
|
</p>
|
|
</div>
|
|
|
|
<div className="flex items-center">
|
|
<input
|
|
type="checkbox"
|
|
id="is_active"
|
|
checked={formData.is_active}
|
|
onChange={(e) => setFormData({ ...formData, is_active: e.target.checked })}
|
|
className="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded"
|
|
disabled={user?.id === session?.user?.id}
|
|
/>
|
|
<label htmlFor="is_active" className="ml-2 block text-sm text-gray-900">
|
|
Active User
|
|
{user?.id === session?.user?.id && (
|
|
<span className="text-gray-500 ml-1">(Cannot deactivate your own account)</span>
|
|
)}
|
|
</label>
|
|
</div>
|
|
|
|
<div className="flex space-x-4 pt-6 border-t border-gray-200">
|
|
<Button type="submit" disabled={saving}>
|
|
{saving ? "Saving..." : "Save Changes"}
|
|
</Button>
|
|
<Link href="/admin/users">
|
|
<Button type="button" variant="outline">
|
|
Cancel
|
|
</Button>
|
|
</Link>
|
|
</div>
|
|
</form>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
{/* User Details Card */}
|
|
{user && (
|
|
<Card className="mt-6">
|
|
<CardHeader>
|
|
<h3 className="text-lg font-semibold">Account Details</h3>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="grid grid-cols-1 md:grid-cols-3 gap-6">
|
|
<div>
|
|
<p className="text-sm font-medium text-gray-500">Created</p>
|
|
<p className="text-sm text-gray-900">{new Date(user.created_at).toLocaleDateString()}</p>
|
|
</div>
|
|
<div>
|
|
<p className="text-sm font-medium text-gray-500">Last Updated</p>
|
|
<p className="text-sm text-gray-900">
|
|
{user.updated_at ? new Date(user.updated_at).toLocaleDateString() : "Never"}
|
|
</p>
|
|
</div>
|
|
<div>
|
|
<p className="text-sm font-medium text-gray-500">Last Login</p>
|
|
<p className="text-sm text-gray-900">
|
|
{user.last_login ? new Date(user.last_login).toLocaleDateString() : "Never"}
|
|
</p>
|
|
</div>
|
|
<div>
|
|
<p className="text-sm font-medium text-gray-500">Failed Login Attempts</p>
|
|
<p className="text-sm text-gray-900">{user.failed_login_attempts || 0}</p>
|
|
</div>
|
|
<div>
|
|
<p className="text-sm font-medium text-gray-500">Account Status</p>
|
|
<p className="text-sm text-gray-900">
|
|
{user.is_active ? "Active" : "Inactive"}
|
|
</p>
|
|
</div>
|
|
<div>
|
|
<p className="text-sm font-medium text-gray-500">Account Locked</p>
|
|
<p className="text-sm text-gray-900">
|
|
{user.locked_until && new Date(user.locked_until) > new Date()
|
|
? `Until ${new Date(user.locked_until).toLocaleDateString()}`
|
|
: "No"
|
|
}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
)}
|
|
</PageContainer>
|
|
);
|
|
}
|