d
This commit is contained in:
parent
166f48a3da
commit
24de12e481
@ -1,65 +1,302 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import React, { useState, useRef } from "react";
|
import React, { useState, useRef } from "react";
|
||||||
type Role = "admin" | "user" | "editor";
|
|
||||||
type User = {
|
// The roles in UserSeed are ALL CAPS as per your data
|
||||||
email: string;
|
type Role = "ADMIN" | "GUEST" | "SCIENTIST";
|
||||||
name: string;
|
// Forward declarations if you don't have these yet
|
||||||
role: Role;
|
// You can replace Scientist or User with real types if you have them
|
||||||
password: string;
|
|
||||||
|
type Scientist = {
|
||||||
|
id: number;
|
||||||
|
createdAt: Date | string;
|
||||||
|
name: string;
|
||||||
|
level: string; // "JUNIOR" | "SENIOR"
|
||||||
|
userId: number;
|
||||||
|
user: User;
|
||||||
|
superiorId?: number | null;
|
||||||
|
superior?: Scientist | null;
|
||||||
|
subordinates: Scientist[];
|
||||||
|
earthquakes: Earthquake[];
|
||||||
|
observatories: Observatory[];
|
||||||
|
artefacts: Artefact[];
|
||||||
|
};
|
||||||
|
|
||||||
|
type Earthquake = {
|
||||||
|
id: number;
|
||||||
|
createdAt: Date | string;
|
||||||
|
updatedAt: Date | string;
|
||||||
|
date: Date | string;
|
||||||
|
location: string;
|
||||||
|
latitude: string;
|
||||||
|
longitude: string;
|
||||||
|
magnitude: number;
|
||||||
|
depth: number;
|
||||||
|
creatorId?: number | null;
|
||||||
|
creator?: Scientist | null;
|
||||||
|
artefacts: Artefact[];
|
||||||
|
observatories: Observatory[];
|
||||||
};
|
};
|
||||||
|
|
||||||
const initialUsers: User[] = [ // todo - add user reading function
|
type Observatory = {
|
||||||
{ email: "john@example.com", name: "John Doe", role: "admin", password: "secret1" },
|
id: number;
|
||||||
{ email: "jane@example.com", name: "Jane Smith", role: "user", password: "secret2" },
|
createdAt: Date | string;
|
||||||
{ email: "bob@example.com", name: "Bob Brown", role: "editor", password: "secret3" },
|
updatedAt: Date | string;
|
||||||
{ email: "alice@example.com", name: "Alice Johnson", role: "user", password: "secret4" },
|
name: string;
|
||||||
{ email: "eve@example.com", name: "Eve Black", role: "admin", password: "secret5" },
|
location: string;
|
||||||
{ email: "dave@example.com", name: "Dave Clark", role: "user", password: "pw" },
|
longitude: string;
|
||||||
{ email: "fred@example.com", name: "Fred Fox", role: "user", password: "pw" },
|
latitude: string;
|
||||||
{ email: "ginny@example.com", name: "Ginny Hall", role: "editor", password: "pw" },
|
dateEstablished?: number | null;
|
||||||
{ email: "harry@example.com", name: "Harry Lee", role: "admin", password: "pw" },
|
functional: boolean;
|
||||||
{ email: "ivy@example.com", name: "Ivy Volt", role: "admin", password: "pw" },
|
seismicSensorOnline: boolean;
|
||||||
{ email: "kate@example.com", name: "Kate Moss", role: "editor", password: "pw" },
|
creatorId?: number | null;
|
||||||
{ email: "leo@example.com", name: "Leo Garrison", role: "user", password: "pw" },
|
creator?: Scientist | null;
|
||||||
{ email: "isaac@example.com", name: "Isaac Yang", role: "user", password: "pw" },
|
earthquakes: Earthquake[];
|
||||||
];
|
};
|
||||||
const sortFields = [ // Sort box options
|
type Artefact = {
|
||||||
{ label: "Name", value: "name" },
|
id: number;
|
||||||
{ label: "Email", value: "email" },
|
createdAt: Date | string;
|
||||||
] as const;
|
updatedAt: Date | string;
|
||||||
|
type: string;
|
||||||
|
warehouseArea: string;
|
||||||
|
earthquakeId: number;
|
||||||
|
earthquake: Earthquake;
|
||||||
|
creatorId?: number | null;
|
||||||
|
creator?: Scientist | null;
|
||||||
|
required: boolean;
|
||||||
|
shopPrice?: number | null;
|
||||||
|
purchasedById?: number | null;
|
||||||
|
purchasedBy?: User | null;
|
||||||
|
pickedUp: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
type SortField = typeof sortFields[number]["value"];
|
type User= {
|
||||||
|
id: number;
|
||||||
|
createdAt: string;
|
||||||
|
name: string;
|
||||||
|
email: string;
|
||||||
|
passwordHash: string;
|
||||||
|
role: Role; // "ADMIN" | "GUEST" | "SCIENTIST"
|
||||||
|
scientist : Scientist | null
|
||||||
|
purchasedArtefacts: Artefact[];
|
||||||
|
};
|
||||||
|
|
||||||
|
// Example users with scientist references
|
||||||
|
|
||||||
|
const initialUsers: User[] = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
createdAt: "2024-06-20T08:00:00Z",
|
||||||
|
name: "Dr. Alice Volcano",
|
||||||
|
email: "alice.volcano@example.com",
|
||||||
|
passwordHash: "hashed_password_1",
|
||||||
|
role: "SCIENTIST",
|
||||||
|
scientist: {
|
||||||
|
id: 101,
|
||||||
|
createdAt: "2024-06-18T09:00:00Z",
|
||||||
|
name: "Dr. Alice Volcano",
|
||||||
|
level: "SENIOR",
|
||||||
|
userId: 1,
|
||||||
|
user: null as any, // Will be populated after object creation to avoid circular reference, see note below
|
||||||
|
superiorId: null,
|
||||||
|
superior: null,
|
||||||
|
subordinates: [],
|
||||||
|
earthquakes: [
|
||||||
|
{
|
||||||
|
id: 201,
|
||||||
|
createdAt: "2024-06-01T04:00:00Z",
|
||||||
|
updatedAt: "2024-06-10T10:00:00Z",
|
||||||
|
date: "2024-06-01T05:00:00Z",
|
||||||
|
location: "Fuego Ridge",
|
||||||
|
latitude: "14.23",
|
||||||
|
longitude: "-90.88",
|
||||||
|
magnitude: 7.2,
|
||||||
|
depth: 10.1,
|
||||||
|
creatorId: 101,
|
||||||
|
creator: null,
|
||||||
|
artefacts: [],
|
||||||
|
observatories: [],
|
||||||
|
}
|
||||||
|
],
|
||||||
|
observatories: [
|
||||||
|
{
|
||||||
|
id: 301,
|
||||||
|
createdAt: "2024-01-01T08:00:00Z",
|
||||||
|
updatedAt: "2024-06-05T16:30:00Z",
|
||||||
|
name: "Central Vulcanology Lab",
|
||||||
|
location: "Fuego City",
|
||||||
|
longitude: "-90.88",
|
||||||
|
latitude: "14.23",
|
||||||
|
dateEstablished: 2011,
|
||||||
|
functional: true,
|
||||||
|
seismicSensorOnline: true,
|
||||||
|
creatorId: 101,
|
||||||
|
creator: null,
|
||||||
|
earthquakes: [],
|
||||||
|
}
|
||||||
|
],
|
||||||
|
artefacts: [
|
||||||
|
{
|
||||||
|
id: 401,
|
||||||
|
createdAt: "2024-06-11T09:00:00Z",
|
||||||
|
updatedAt: "2024-06-12T10:45:00Z",
|
||||||
|
type: "Lava",
|
||||||
|
warehouseArea: "ZoneA-Shelf1",
|
||||||
|
earthquakeId: 201,
|
||||||
|
earthquake: {
|
||||||
|
id: 201,
|
||||||
|
createdAt: "2024-06-01T04:00:00Z",
|
||||||
|
updatedAt: "2024-06-10T10:00:00Z",
|
||||||
|
date: "2024-06-01T05:00:00Z",
|
||||||
|
location: "Fuego Ridge",
|
||||||
|
latitude: "14.23",
|
||||||
|
longitude: "-90.88",
|
||||||
|
magnitude: 7.2,
|
||||||
|
depth: 10.1,
|
||||||
|
creatorId: 101,
|
||||||
|
creator: null,
|
||||||
|
artefacts: [],
|
||||||
|
observatories: [],
|
||||||
|
},
|
||||||
|
creatorId: 101,
|
||||||
|
creator: null,
|
||||||
|
required: true,
|
||||||
|
shopPrice: 500.0,
|
||||||
|
purchasedById: null,
|
||||||
|
purchasedBy: null,
|
||||||
|
pickedUp: false,
|
||||||
|
}
|
||||||
|
],
|
||||||
|
},
|
||||||
|
purchasedArtefacts: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 2,
|
||||||
|
createdAt: "2024-06-21T08:00:00Z",
|
||||||
|
name: "Dr. Bob Lava",
|
||||||
|
email: "bob.lava@example.com",
|
||||||
|
passwordHash: "hashed_password_2",
|
||||||
|
role: "SCIENTIST",
|
||||||
|
scientist: {
|
||||||
|
id: 102,
|
||||||
|
createdAt: "2024-06-19T13:00:00Z",
|
||||||
|
name: "Dr. Bob Lava",
|
||||||
|
level: "JUNIOR",
|
||||||
|
userId: 2,
|
||||||
|
user: null as any, // Populated after object creation if circular needed
|
||||||
|
superiorId: 101,
|
||||||
|
superior: null,
|
||||||
|
subordinates: [],
|
||||||
|
earthquakes: [],
|
||||||
|
observatories: [],
|
||||||
|
artefacts: [],
|
||||||
|
},
|
||||||
|
purchasedArtefacts: [],
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
// Optionally, populate the scientist.user field with the parent user (avoiding circular reference on first creation):
|
||||||
|
if (initialUsers[0].scientist && initialUsers[1].scientist){
|
||||||
|
initialUsers[0].scientist.user = initialUsers[0];
|
||||||
|
initialUsers[1].scientist.user = initialUsers[1];
|
||||||
|
initialUsers[1].scientist.superior = initialUsers[1].scientist;}
|
||||||
|
|
||||||
|
const allArtefacts: Artefact[] = [
|
||||||
|
{
|
||||||
|
id: 401,
|
||||||
|
createdAt: "2024-06-11T09:00:00Z",
|
||||||
|
updatedAt: "2024-06-12T10:45:00Z",
|
||||||
|
type: "Lava",
|
||||||
|
warehouseArea: "ZoneA-Shelf1",
|
||||||
|
earthquakeId: 201,
|
||||||
|
earthquake: {
|
||||||
|
id: 201,
|
||||||
|
createdAt: "2024-06-01T04:00:00Z",
|
||||||
|
updatedAt: "2024-06-10T10:00:00Z",
|
||||||
|
date: "2024-06-01T05:00:00Z",
|
||||||
|
location: "Fuego Ridge",
|
||||||
|
latitude: "14.23",
|
||||||
|
longitude: "-90.88",
|
||||||
|
magnitude: 7.2,
|
||||||
|
depth: 10.1,
|
||||||
|
creatorId: 101,
|
||||||
|
creator: null,
|
||||||
|
artefacts: [],
|
||||||
|
observatories: [],
|
||||||
|
},
|
||||||
|
creatorId: 101,
|
||||||
|
creator: null,
|
||||||
|
required: true,
|
||||||
|
shopPrice: 500.0,
|
||||||
|
purchasedById: null,
|
||||||
|
purchasedBy: null,
|
||||||
|
pickedUp: false,
|
||||||
|
}
|
||||||
|
];
|
||||||
|
const allEarthquakes: Earthquake[] = [
|
||||||
|
{
|
||||||
|
id: 201,
|
||||||
|
createdAt: "2024-06-01T04:00:00Z",
|
||||||
|
updatedAt: "2024-06-10T10:00:00Z",
|
||||||
|
date: "2024-06-01T05:00:00Z",
|
||||||
|
location: "Fuego Ridge",
|
||||||
|
latitude: "14.23",
|
||||||
|
longitude: "-90.88",
|
||||||
|
magnitude: 7.2,
|
||||||
|
depth: 10.1,
|
||||||
|
creatorId: 101,
|
||||||
|
creator: null,
|
||||||
|
artefacts: [],
|
||||||
|
observatories: [],
|
||||||
|
}
|
||||||
|
];
|
||||||
|
const allScientists: Scientist[] = initialUsers.map(u => u.scientist as Scientist).filter(Boolean);
|
||||||
|
|
||||||
|
// Human readable role labels
|
||||||
|
const roleLabels: Record<Role, string> = {
|
||||||
|
ADMIN: "Admin",
|
||||||
|
GUEST: "Guest",
|
||||||
|
SCIENTIST: "Scientist"
|
||||||
|
};
|
||||||
|
const allRoles: Role[] = ["ADMIN", "GUEST", "SCIENTIST"];
|
||||||
|
|
||||||
|
const sortFields = [
|
||||||
|
{ label: "Name", value: "name" },
|
||||||
|
{ label: "Email", value: "email" }
|
||||||
|
] as const;
|
||||||
|
type SortField = (typeof sortFields)[number]["value"];
|
||||||
type SortDir = "asc" | "desc";
|
type SortDir = "asc" | "desc";
|
||||||
const dirLabels: Record<SortDir, string> = { asc: "ascending", desc: "descending" };
|
const dirLabels: Record<SortDir, string> = { asc: "ascending", desc: "descending" };
|
||||||
const fieldLabels: Record<SortField, string> = { name: "Name", email: "Email" };
|
const fieldLabels: Record<SortField, string> = { name: "Name", email: "Email" };
|
||||||
|
|
||||||
export default function AdminPage() {
|
export default function AdminPage() {
|
||||||
const [users, setUsers] = useState<User[]>(initialUsers);
|
const [users, setUsers] = useState<User[]>(initialUsers);
|
||||||
const [selectedEmail, setSelectedEmail] = useState<string | null>(null);
|
const [selectedEmail, setSelectedEmail] = useState<string | null>(null);
|
||||||
|
const [editUser, setEditUser] = useState<User & { password?: string } | null>(null);
|
||||||
|
const [showPassword, setShowPassword] = useState(false);
|
||||||
|
const [showAssignmentPanel, setShowAssignmentPanel] = useState<null | 'artefact' | 'earthquake' | 'scientist'>(null);
|
||||||
|
|
||||||
// Local edit state for editor form
|
|
||||||
const [editUser, setEditUser] = useState<User | null>(null);
|
|
||||||
// Reset editUser when the selected user changes
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (!selectedEmail) setEditUser(null);
|
if (!selectedEmail) setEditUser(null);
|
||||||
else {
|
else {
|
||||||
const user = users.find(u => u.email === selectedEmail);
|
const user = users.find(u => u.email === selectedEmail);
|
||||||
setEditUser(user ? { ...user } : null);
|
// Add a dummy password field just for edit box (not stored)
|
||||||
|
setEditUser(user ? { ...user, password: "" } : null);
|
||||||
}
|
}
|
||||||
}, [selectedEmail, users]);
|
}, [selectedEmail, users]);
|
||||||
|
|
||||||
// Search/filter/sort state
|
// Search/filter/sort state
|
||||||
const [searchField, setSearchField] = useState<"name" | "email">("name");
|
const [searchField, setSearchField] = useState<SortField>("name");
|
||||||
const [searchText, setSearchText] = useState("");
|
const [searchText, setSearchText] = useState("");
|
||||||
|
// The filter UI must use ALL CAPS for roles in UserSeed
|
||||||
const [roleFilter, setRoleFilter] = useState<Role | "all">("all");
|
const [roleFilter, setRoleFilter] = useState<Role | "all">("all");
|
||||||
const [sortField, setSortField] = useState<SortField>("name");
|
const [sortField, setSortField] = useState<SortField>("name");
|
||||||
const [sortDir, setSortDir] = useState<SortDir>("asc");
|
const [sortDir, setSortDir] = useState<SortDir>("asc");
|
||||||
// Dropdown states
|
|
||||||
const [filterDropdownOpen, setFilterDropdownOpen] = useState(false);
|
const [filterDropdownOpen, setFilterDropdownOpen] = useState(false);
|
||||||
const [sortDropdownOpen, setSortDropdownOpen] = useState(false);
|
const [sortDropdownOpen, setSortDropdownOpen] = useState(false);
|
||||||
const filterDropdownRef = useRef<HTMLDivElement>(null);
|
const filterDropdownRef = useRef<HTMLDivElement>(null);
|
||||||
const sortDropdownRef = useRef<HTMLDivElement>(null);
|
const sortDropdownRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
// Dropdown auto-close
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
const handleClick = (e: MouseEvent) => {
|
const handleClick = (e: MouseEvent) => {
|
||||||
if (
|
if (
|
||||||
@ -73,7 +310,17 @@ export default function AdminPage() {
|
|||||||
return () => document.removeEventListener("mousedown", handleClick);
|
return () => document.removeEventListener("mousedown", handleClick);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Filtering, searching, sorting logic
|
// Handler: assign/unassign scientist
|
||||||
|
const handleAssignScientist = (scientistId: number) => {
|
||||||
|
setEditUser(prev =>
|
||||||
|
prev ? {
|
||||||
|
...prev,
|
||||||
|
scientist: allScientists.find(s => s.id === scientistId) || null,
|
||||||
|
} : null
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Filter, search, sort logic
|
||||||
const filteredUsers = users.filter(
|
const filteredUsers = users.filter(
|
||||||
(user) => roleFilter === "all" || user.role === roleFilter
|
(user) => roleFilter === "all" || user.role === roleFilter
|
||||||
);
|
);
|
||||||
@ -85,7 +332,7 @@ export default function AdminPage() {
|
|||||||
return sortDir === "asc" ? cmp : -cmp;
|
return sortDir === "asc" ? cmp : -cmp;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Form input change handler
|
// Edit form handler (special-case password, don't modify passwordHash)
|
||||||
const handleEditChange = (e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>) => {
|
const handleEditChange = (e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>) => {
|
||||||
if (!editUser) return;
|
if (!editUser) return;
|
||||||
const { name, value } = e.target;
|
const { name, value } = e.target;
|
||||||
@ -94,15 +341,14 @@ export default function AdminPage() {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Update button logic (compare original selectedUser and editUser)
|
// Selected "actual" user object
|
||||||
const selectedUser = users.find((u) => u.email === selectedEmail);
|
const selectedUser = users.find(u => u.email === selectedEmail);
|
||||||
const isEditChanged = React.useMemo(() => {
|
const isEditChanged = React.useMemo(() => {
|
||||||
if (!editUser || !selectedUser) return false;
|
if (!editUser || !selectedUser) return false;
|
||||||
// Compare primitive fields
|
|
||||||
return (
|
return (
|
||||||
editUser.name !== selectedUser.name ||
|
editUser.name !== selectedUser.name ||
|
||||||
editUser.role !== selectedUser.role ||
|
editUser.role !== selectedUser.role ||
|
||||||
editUser.password !== selectedUser.password
|
(editUser.password && editUser.password !== "") // Only trigger update if changed
|
||||||
);
|
);
|
||||||
}, [editUser, selectedUser]);
|
}, [editUser, selectedUser]);
|
||||||
|
|
||||||
@ -112,14 +358,14 @@ export default function AdminPage() {
|
|||||||
if (!editUser) return;
|
if (!editUser) return;
|
||||||
setUsers(prev =>
|
setUsers(prev =>
|
||||||
prev.map(u =>
|
prev.map(u =>
|
||||||
u.email === editUser.email ? { ...editUser } : u
|
u.email === editUser.email
|
||||||
|
? { ...editUser } // Copies ALL user properties, including scientist, artefacts, etc.
|
||||||
|
: u
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
// After successful update, update selectedUser local state
|
|
||||||
// (editUser will auto-sync due to useEffect on users)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Delete user logic
|
// Delete user
|
||||||
const handleDelete = () => {
|
const handleDelete = () => {
|
||||||
if (!selectedUser) return;
|
if (!selectedUser) return;
|
||||||
if (!window.confirm(`Are you sure you want to delete "${selectedUser.name}"? This cannot be undone.`)) return;
|
if (!window.confirm(`Are you sure you want to delete "${selectedUser.name}"? This cannot be undone.`)) return;
|
||||||
@ -128,16 +374,13 @@ export default function AdminPage() {
|
|||||||
setEditUser(null);
|
setEditUser(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
const allRoles: Role[] = ["admin", "user", "editor"];
|
|
||||||
|
|
||||||
// Tooltip handling for email field
|
|
||||||
const [showEmailTooltip, setShowEmailTooltip] = useState(false);
|
const [showEmailTooltip, setShowEmailTooltip] = useState(false);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col h-full">
|
<div className="flex flex-col h-full">
|
||||||
<div className="flex h-full overflow-hidden bg-gray-50">
|
<div className="flex h-full overflow-hidden bg-gray-50">
|
||||||
{/* SIDEBAR */}
|
{/* SIDEBAR */}
|
||||||
<div className="w-80 h-full border-r border-neutral-200 bg-neutral-100 flex flex-col rounded-l-xl shadow-sm">
|
<div className=" w-80 h-full border-r border-neutral-200 bg-neutral-100 flex flex-col rounded-3xl shadow-sm">
|
||||||
<div className="p-4 flex flex-col h-full">
|
<div className="p-4 flex flex-col h-full">
|
||||||
{/* Search Bar */}
|
{/* Search Bar */}
|
||||||
<div className="mb-3 flex gap-2">
|
<div className="mb-3 flex gap-2">
|
||||||
@ -149,7 +392,7 @@ export default function AdminPage() {
|
|||||||
/>
|
/>
|
||||||
<select
|
<select
|
||||||
value={searchField}
|
value={searchField}
|
||||||
onChange={e => setSearchField(e.target.value as "name" | "email")}
|
onChange={e => setSearchField(e.target.value as SortField)}
|
||||||
className="border rounded-lg px-2 py-1 text-sm"
|
className="border rounded-lg px-2 py-1 text-sm"
|
||||||
>
|
>
|
||||||
<option value="name">Name</option>
|
<option value="name">Name</option>
|
||||||
@ -163,20 +406,21 @@ export default function AdminPage() {
|
|||||||
<button
|
<button
|
||||||
className={`px-3 py-1 rounded-lg border font-semibold flex items-center transition
|
className={`px-3 py-1 rounded-lg border font-semibold flex items-center transition
|
||||||
${roleFilter !== "all"
|
${roleFilter !== "all"
|
||||||
? "bg-blue-600 text-white border-blue-600 hover:bg-blue-700"
|
? "bg-blue-600 text-white border-blue-600 hover:bg-blue-700"
|
||||||
: "bg-white text-gray-700 border hover:bg-neutral-200"}
|
: "bg-white text-gray-700 border hover:bg-neutral-200"}
|
||||||
`}
|
`}
|
||||||
onClick={() => setFilterDropdownOpen(v => !v)}
|
onClick={() => setFilterDropdownOpen(v => !v)}
|
||||||
type="button"
|
type="button"
|
||||||
>
|
>
|
||||||
Filter <svg className="w-4 h-4 ml-1" fill="none" stroke="currentColor" strokeWidth={2} viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" d="M19 9l-7 7-7-7" /></svg>
|
Filter <svg className="w-4 h-4 ml-1" fill="none" stroke="currentColor" strokeWidth={2} viewBox="0 0 24 24">
|
||||||
|
<path strokeLinecap="round" strokeLinejoin="round" d="M19 9l-7 7-7-7" /></svg>
|
||||||
</button>
|
</button>
|
||||||
{filterDropdownOpen && (
|
{filterDropdownOpen && (
|
||||||
<div className="absolute z-10 mt-1 left-0 bg-white border rounded-lg shadow-sm w-28 py-1">
|
<div className="absolute z-10 mt-1 left-0 bg-white border rounded-lg shadow-sm w-28 py-1">
|
||||||
<button
|
<button
|
||||||
onClick={() => { setRoleFilter("all"); setFilterDropdownOpen(false); }}
|
onClick={() => { setRoleFilter("all"); setFilterDropdownOpen(false); }}
|
||||||
className={`w-full text-left px-3 py-1 hover:bg-blue-50 border-b border-gray-100 last:border-0
|
className={`w-full text-left px-3 py-1 hover:bg-blue-50 border-b border-gray-100 last:border-0
|
||||||
${roleFilter==="all" ? "font-bold text-blue-600" : ""}`}
|
${roleFilter === "all" ? "font-bold text-blue-600" : ""}`}
|
||||||
>
|
>
|
||||||
All
|
All
|
||||||
</button>
|
</button>
|
||||||
@ -185,9 +429,9 @@ export default function AdminPage() {
|
|||||||
key={role}
|
key={role}
|
||||||
onClick={() => { setRoleFilter(role); setFilterDropdownOpen(false); }}
|
onClick={() => { setRoleFilter(role); setFilterDropdownOpen(false); }}
|
||||||
className={`w-full text-left px-3 py-1 hover:bg-blue-50 border-b border-gray-100 last:border-0
|
className={`w-full text-left px-3 py-1 hover:bg-blue-50 border-b border-gray-100 last:border-0
|
||||||
${roleFilter===role ? "font-bold text-blue-600" : ""}`}
|
${roleFilter === role ? "font-bold text-blue-600" : ""}`}
|
||||||
>
|
>
|
||||||
{role}
|
{roleLabels[role]}
|
||||||
</button>
|
</button>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
@ -212,7 +456,7 @@ export default function AdminPage() {
|
|||||||
setSortDropdownOpen(false);
|
setSortDropdownOpen(false);
|
||||||
}}
|
}}
|
||||||
className={`w-full text-left px-3 py-2 hover:bg-blue-50 border-b border-gray-100 last:border-0
|
className={`w-full text-left px-3 py-2 hover:bg-blue-50 border-b border-gray-100 last:border-0
|
||||||
${sortField===opt.value ? "font-bold text-blue-600" : ""}`}
|
${sortField === opt.value ? "font-bold text-blue-600" : ""}`}
|
||||||
>
|
>
|
||||||
{opt.label}
|
{opt.label}
|
||||||
</button>
|
</button>
|
||||||
@ -246,7 +490,7 @@ export default function AdminPage() {
|
|||||||
>
|
>
|
||||||
<div className="flex items-center justify-between">
|
<div className="flex items-center justify-between">
|
||||||
<span className="text-sm font-medium truncate">{user.name}</span>
|
<span className="text-sm font-medium truncate">{user.name}</span>
|
||||||
<span className="ml-1 text-xs px-2 py-0.5 rounded-lg bg-gray-200 text-gray-700">{user.role}</span>
|
<span className="ml-1 text-xs px-2 py-0.5 rounded-lg bg-gray-200 text-gray-700">{roleLabels[user.role]}</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center justify-between mt-0.5">
|
<div className="flex items-center justify-between mt-0.5">
|
||||||
<span className="text-xs text-gray-600 truncate">{user.email}</span>
|
<span className="text-xs text-gray-600 truncate">{user.email}</span>
|
||||||
@ -262,9 +506,15 @@ export default function AdminPage() {
|
|||||||
{/* MAIN PANEL */}
|
{/* MAIN PANEL */}
|
||||||
<div className="flex-1 p-10 bg-white overflow-y-auto">
|
<div className="flex-1 p-10 bg-white overflow-y-auto">
|
||||||
{editUser ? (
|
{editUser ? (
|
||||||
<div className="max-w-lg mx-auto bg-white p-6 rounded-lg shadow">
|
<div className="max-w-2xl mx-auto bg-white p-6 rounded-lg shadow">
|
||||||
<h2 className="text-lg font-bold mb-6">Edit User</h2>
|
<h2 className="text-lg font-bold mb-6">Edit User</h2>
|
||||||
<form className="space-y-4" onSubmit={handleUpdate}>
|
<form className="space-y-4" onSubmit={e => { e.preventDefault(); }}>
|
||||||
|
{/* Creation Time */}
|
||||||
|
<div className="flex items-center gap-2 mb-2">
|
||||||
|
<label className="text-sm font-medium text-gray-700">Account Creation Time:</label>
|
||||||
|
<span className="text-sm text-gray-500">{editUser.createdAt}</span>
|
||||||
|
</div>
|
||||||
|
{/* Email */}
|
||||||
<div className="relative">
|
<div className="relative">
|
||||||
<label className="block text-sm font-medium text-gray-700 mb-1">
|
<label className="block text-sm font-medium text-gray-700 mb-1">
|
||||||
Email (unique):
|
Email (unique):
|
||||||
@ -275,17 +525,9 @@ export default function AdminPage() {
|
|||||||
name="email"
|
name="email"
|
||||||
value={editUser.email}
|
value={editUser.email}
|
||||||
readOnly
|
readOnly
|
||||||
onMouseEnter={() => setShowEmailTooltip(true)}
|
|
||||||
onMouseLeave={() => setShowEmailTooltip(false)}
|
|
||||||
/>
|
/>
|
||||||
{/* Custom tooltip */}
|
|
||||||
{showEmailTooltip && (
|
|
||||||
<div className="absolute left-0 top-full mt-1 z-10 w-max max-w-xs bg-gray-800 text-gray-100 text-xs px-3 py-2 rounded-md shadow-lg border border-gray-700">
|
|
||||||
This field cannot be changed. <br />
|
|
||||||
To change the email, delete and re-add the user.
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
|
{/* Name */}
|
||||||
<div>
|
<div>
|
||||||
<label className="block text-sm font-medium text-gray-700 mb-1">
|
<label className="block text-sm font-medium text-gray-700 mb-1">
|
||||||
Name:
|
Name:
|
||||||
@ -295,36 +537,118 @@ export default function AdminPage() {
|
|||||||
type="text"
|
type="text"
|
||||||
name="name"
|
name="name"
|
||||||
value={editUser.name}
|
value={editUser.name}
|
||||||
onChange={handleEditChange}
|
onChange={e => setEditUser(prev => prev ? { ...prev, name: e.target.value } : null)}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
{/* Role */}
|
||||||
<div>
|
<div>
|
||||||
<label className="block text-sm font-medium text-gray-700 mb-1">
|
<label className="block text-sm font-medium text-gray-700 mb-1">Role:</label>
|
||||||
Role:
|
|
||||||
</label>
|
|
||||||
<select
|
<select
|
||||||
className="w-full border px-3 py-2 rounded-lg outline-none focus:ring-2 focus:ring-blue-300"
|
className="w-full border px-3 py-2 rounded-lg outline-none focus:ring-2 focus:ring-blue-300"
|
||||||
name="role"
|
name="role"
|
||||||
value={editUser.role}
|
value={editUser.role}
|
||||||
onChange={handleEditChange}
|
onChange={e => setEditUser(prev => prev ? { ...prev, role: e.target.value as Role } : null)}
|
||||||
>
|
>
|
||||||
{allRoles.map((role) => (
|
{allRoles.map((role) => (
|
||||||
<option key={role} value={role}>{role}</option>
|
<option key={role} value={role}>{roleLabels[role]}</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
{/* Scientist assignment */}
|
||||||
<div>
|
<div>
|
||||||
<label className="block text-sm font-medium text-gray-700 mb-1">
|
<label className="block text-sm font-medium text-gray-700 mb-1">Scientist:</label>
|
||||||
Password:
|
<select
|
||||||
</label>
|
className="w-full border px-3 py-2 rounded-lg"
|
||||||
<input
|
value={editUser.scientist?.id ?? ""}
|
||||||
className="w-full border px-3 py-2 rounded-lg outline-none focus:ring-2 focus:ring-blue-300"
|
onChange={e => handleAssignScientist(Number(e.target.value))}
|
||||||
type="text"
|
>
|
||||||
name="password"
|
<option value={""}>None</option>
|
||||||
value={editUser.password}
|
{allScientists.map(sc => (
|
||||||
onChange={handleEditChange}
|
<option key={sc.id} value={sc.id}>{sc.name}</option>
|
||||||
/>
|
))}
|
||||||
|
</select>
|
||||||
|
{/* Show all scientist data */}
|
||||||
|
{editUser.scientist && (
|
||||||
|
<div className="flex items-center gap-2 py-6">
|
||||||
|
<label className="block text-sm font-medium text-gray-700">Scientist:</label>
|
||||||
|
<div className="flex-1">
|
||||||
|
{editUser.scientist
|
||||||
|
? `${editUser.scientist.name} (${editUser.scientist.level})`
|
||||||
|
: <span className="text-gray-400">None</span>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="px-3 py-1 bg-blue-500 text-white rounded hover:bg-blue-600 shadow text-sm"
|
||||||
|
onClick={() => setShowAssignmentPanel("scientist")}
|
||||||
|
>
|
||||||
|
Edit Scientist
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
{/* Password - eye toggle only (view, not edit) */}
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-700 mb-1">Password:</label>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<input
|
||||||
|
className="w-full border px-3 py-2 rounded-lg outline-none bg-gray-100"
|
||||||
|
type={showPassword ? "text" : "password"}
|
||||||
|
value={editUser.passwordHash || ""}
|
||||||
|
readOnly
|
||||||
|
autoComplete="off"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
title={showPassword ? "Hide password" : "Show password"}
|
||||||
|
className="text-gray-500 hover:text-gray-800"
|
||||||
|
onClick={() => setShowPassword(s => !s)}
|
||||||
|
>
|
||||||
|
{showPassword ? (
|
||||||
|
<svg className="h-5 w-5" fill="none" stroke="currentColor" strokeWidth={2} viewBox="0 0 24 24">
|
||||||
|
<path strokeLinecap="round" strokeLinejoin="round" d="M13.875 18.825A10.05 10.05 0 0112 19c-5.523 0-10-4.477-10-10a9.953 9.953 0 013.37-7.53M4.22 4.22l15.56 15.56M19.77 19.77L4.22 4.22M19.77 19.77l.01-.01M21 12a9.953 9.953 0 01-3.37 7.53M4.22 19.77l.01-.01" />
|
||||||
|
</svg>
|
||||||
|
) : (
|
||||||
|
<svg className="h-5 w-5" fill="none" stroke="currentColor" strokeWidth={2} viewBox="0 0 24 24">
|
||||||
|
<path strokeLinecap="round" strokeLinejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0zm6 0C21 7.03 16.97 3 12 3S3 7.03 3 12c0 4.97 4.03 9 9 9s9-4.03 9-9z" />
|
||||||
|
</svg>
|
||||||
|
)}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{/* Trigger artefact assignment panel */}
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-700 mb-1">Purchased Artefacts:</label>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="px-3 py-1 bg-blue-500 text-white rounded hover:bg-blue-600 shadow"
|
||||||
|
onClick={() => setShowAssignmentPanel('artefact')}
|
||||||
|
>
|
||||||
|
Assign Artefacts
|
||||||
|
</button>
|
||||||
|
<div className="mt-1 text-xs text-gray-500">
|
||||||
|
Currently assigned: {editUser.purchasedArtefacts.map(a => a.type).join(", ") || "None"}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Trigger earthquake assignment panel */}
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-700 mb-1">Scientist's Earthquakes:</label>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="px-3 py-1 bg-blue-500 text-white rounded hover:bg-blue-600 shadow"
|
||||||
|
onClick={() => setShowAssignmentPanel('earthquake')}
|
||||||
|
disabled={!editUser.scientist}
|
||||||
|
>
|
||||||
|
Assign Earthquakes
|
||||||
|
</button>
|
||||||
|
<div className="mt-1 text-xs text-gray-500">
|
||||||
|
{editUser.scientist
|
||||||
|
? "Current: " + (editUser.scientist.earthquakes.map(e => e.location).join(", ") || "None")
|
||||||
|
: "Assign a scientist to manage earthquakes"}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className="flex gap-2 justify-end pt-6">
|
<div className="flex gap-2 justify-end pt-6">
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
@ -336,11 +660,9 @@ export default function AdminPage() {
|
|||||||
<button
|
<button
|
||||||
type="submit"
|
type="submit"
|
||||||
className={`px-4 py-2 rounded-lg font-semibold transition
|
className={`px-4 py-2 rounded-lg font-semibold transition
|
||||||
${isEditChanged
|
bg-blue-600 hover:bg-blue-700 text-white shadow
|
||||||
? "bg-blue-600 hover:bg-blue-700 text-white shadow"
|
`}
|
||||||
: "bg-gray-300 text-gray-500 cursor-not-allowed"
|
onClick={handleUpdate}
|
||||||
}`}
|
|
||||||
disabled={!isEditChanged}
|
|
||||||
>
|
>
|
||||||
Update
|
Update
|
||||||
</button>
|
</button>
|
||||||
@ -352,8 +674,168 @@ export default function AdminPage() {
|
|||||||
Select a user...
|
Select a user...
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{/* Right-side assignment panel */}
|
||||||
|
{showAssignmentPanel && editUser &&(
|
||||||
|
<div className="fixed z-50 right-6 top-6 bottom-6 w-96 rounded-3xl bg-white shadow-2xl border border-gray-200 flex flex-col">
|
||||||
|
<div className="flex justify-between items-center p-4 border-b rounded-t-3xl">
|
||||||
|
<span className="font-bold text-lg">
|
||||||
|
{showAssignmentPanel === 'artefact' ? 'Assign Artefacts' : 'Assign Earthquakes'}
|
||||||
|
</span>
|
||||||
|
<button
|
||||||
|
onClick={() => setShowAssignmentPanel(null)}
|
||||||
|
className="text-gray-500 hover:text-black text-xl px-3 py-1"
|
||||||
|
title="Close"
|
||||||
|
>
|
||||||
|
×
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div className="flex-1 overflow-y-auto p-4">
|
||||||
|
{showAssignmentPanel === 'artefact' && (
|
||||||
|
<>
|
||||||
|
{allArtefacts.map(a => (
|
||||||
|
<label key={a.id} className="flex items-center gap-2 mb-2">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={!!editUser.purchasedArtefacts.find(b => b.id === a.id)}
|
||||||
|
onChange={e => {
|
||||||
|
const has = !!editUser.purchasedArtefacts.find(b => b.id === a.id)
|
||||||
|
setEditUser(prev => prev ? {
|
||||||
|
...prev,
|
||||||
|
purchasedArtefacts: has
|
||||||
|
? prev.purchasedArtefacts.filter(b => b.id !== a.id)
|
||||||
|
: [...prev.purchasedArtefacts, a]
|
||||||
|
} : null)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<span>
|
||||||
|
{a.type} <span className="text-xs text-gray-400">({a.warehouseArea})</span>
|
||||||
|
</span>
|
||||||
|
</label>
|
||||||
|
))}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
{showAssignmentPanel === 'earthquake' && editUser &&(
|
||||||
|
<>
|
||||||
|
{!editUser.scientist
|
||||||
|
? <div className="text-sm text-gray-400 mt-8">Assign a scientist first.</div>
|
||||||
|
: allEarthquakes.map(eq => (
|
||||||
|
<label key={eq.id} className="flex items-center gap-2 mb-2">
|
||||||
|
{editUser.scientist &&
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={!!editUser.scientist.earthquakes.find(e => e.id === eq.id)}
|
||||||
|
onChange={e => {
|
||||||
|
if (!editUser.scientist) return;
|
||||||
|
const has = !!editUser.scientist.earthquakes.find(x => x.id === eq.id);
|
||||||
|
setEditUser(prev => prev ? {
|
||||||
|
...prev,
|
||||||
|
scientist: {
|
||||||
|
...prev.scientist!,
|
||||||
|
earthquakes: has
|
||||||
|
? prev.scientist!.earthquakes.filter(x => x.id !== eq.id)
|
||||||
|
: [...prev.scientist!.earthquakes, eq]
|
||||||
|
}
|
||||||
|
} : null)
|
||||||
|
}}
|
||||||
|
disabled={!editUser.scientist}
|
||||||
|
/>}
|
||||||
|
<span>
|
||||||
|
{eq.location} <span className="text-xs text-gray-400">(Mag {eq.magnitude})</span>
|
||||||
|
</span>
|
||||||
|
</label>
|
||||||
|
))}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
{showAssignmentPanel === 'scientist' && editUser.scientist &&(
|
||||||
|
<>
|
||||||
|
{/* Scientist edit form */}
|
||||||
|
<div className="space-y-5">
|
||||||
|
{/* Name */}
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-700 mb-1">Name:</label>
|
||||||
|
<input
|
||||||
|
className="w-full border px-3 py-2 rounded-lg outline-none"
|
||||||
|
type="text"
|
||||||
|
value={editUser.scientist.name}
|
||||||
|
onChange={e =>
|
||||||
|
setEditUser(prev => prev && prev.scientist ? {
|
||||||
|
...prev,
|
||||||
|
scientist: { ...prev.scientist, name: e.target.value }
|
||||||
|
} : prev)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{/* Level */}
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-700 mb-1">Level:</label>
|
||||||
|
<select
|
||||||
|
className="w-full border px-3 py-2 rounded-lg outline-none"
|
||||||
|
value={editUser.scientist.level}
|
||||||
|
onChange={e =>
|
||||||
|
setEditUser(prev => prev && prev.scientist ? {
|
||||||
|
...prev,
|
||||||
|
scientist: { ...prev.scientist, level: e.target.value }
|
||||||
|
} : prev)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<option value="SENIOR">Senior</option>
|
||||||
|
<option value="JUNIOR">Junior</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
{/* Assign/Remove Superior */}
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-700 mb-1">Senior Scientist:</label>
|
||||||
|
<select
|
||||||
|
className="w-full border px-3 py-2 rounded-lg outline-none"
|
||||||
|
value={editUser.scientist.superiorId || ""}
|
||||||
|
onChange={e => {
|
||||||
|
const val = e.target.value ? Number(e.target.value) : null;
|
||||||
|
setEditUser(prev => prev && prev.scientist ? {
|
||||||
|
...prev,
|
||||||
|
scientist: {
|
||||||
|
...prev.scientist,
|
||||||
|
superiorId: val,
|
||||||
|
superior: val ? allScientists.find(sc => sc.id === val) || null : null
|
||||||
|
}
|
||||||
|
} : prev)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<option value="">None</option>
|
||||||
|
{allScientists
|
||||||
|
.filter(sc => sc.id !== editUser.scientist!.id)
|
||||||
|
.map(sc => (
|
||||||
|
<option key={sc.id} value={sc.id}>
|
||||||
|
{sc.name} ({sc.level})
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
{/* Subordinates readonly */}
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-700 mb-1">Subordinates:</label>
|
||||||
|
<div className="text-sm text-gray-700 pl-2">
|
||||||
|
{editUser.scientist.subordinates.length
|
||||||
|
? editUser.scientist.subordinates.map(sc => sc.name).join(", ")
|
||||||
|
: <span className="text-gray-400">None</span>}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<div className="p-4 border-t flex justify-end rounded-b-3xl">
|
||||||
|
<button
|
||||||
|
className="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700"
|
||||||
|
onClick={() => setShowAssignmentPanel(null)}
|
||||||
|
>
|
||||||
|
Done
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user