183 lines
5.6 KiB
TypeScript
183 lines
5.6 KiB
TypeScript
|
|
import { A, useNavigate, useParams } from '@solidjs/router';
|
||
|
|
import { createMemo, createResource, createSignal, Show } from 'solid-js';
|
||
|
|
import AdminShell from '~/components/AdminShell';
|
||
|
|
|
||
|
|
const API = '/api/gateway';
|
||
|
|
|
||
|
|
type Role = {
|
||
|
|
id: string;
|
||
|
|
name: string;
|
||
|
|
};
|
||
|
|
|
||
|
|
type User = {
|
||
|
|
id: string;
|
||
|
|
name?: string;
|
||
|
|
full_name?: string;
|
||
|
|
email: string;
|
||
|
|
roleId?: string;
|
||
|
|
role_id?: string;
|
||
|
|
role?: Role;
|
||
|
|
status?: 'ACTIVE' | 'INACTIVE' | 'PENDING';
|
||
|
|
createdAt?: string;
|
||
|
|
created_at?: string;
|
||
|
|
};
|
||
|
|
|
||
|
|
async function fetchRoles(): Promise<Role[]> {
|
||
|
|
try {
|
||
|
|
const res = await fetch(`${API}/api/admin/roles?audience=INTERNAL`);
|
||
|
|
if (!res.ok) return [];
|
||
|
|
const data = await res.json();
|
||
|
|
const rows = Array.isArray(data) ? data : (data.roles || []);
|
||
|
|
return rows.map((r: any) => ({ id: r.id, name: r.name }));
|
||
|
|
} catch {
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function fetchUser(id: string): Promise<User | null> {
|
||
|
|
try {
|
||
|
|
const adminRes = await fetch(`${API}/api/admin/users/${id}`);
|
||
|
|
if (adminRes.ok) return adminRes.json();
|
||
|
|
|
||
|
|
const fallback = await fetch(`${API}/api/users/${id}`);
|
||
|
|
if (!fallback.ok) return null;
|
||
|
|
return fallback.json();
|
||
|
|
} catch {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export default function EditUserPage() {
|
||
|
|
const navigate = useNavigate();
|
||
|
|
const params = useParams();
|
||
|
|
const [user] = createResource(() => params.id, fetchUser);
|
||
|
|
const [roles] = createResource(fetchRoles);
|
||
|
|
|
||
|
|
const [name, setName] = createSignal('');
|
||
|
|
const [email, setEmail] = createSignal('');
|
||
|
|
const [roleId, setRoleId] = createSignal('');
|
||
|
|
const [status, setStatus] = createSignal<'ACTIVE' | 'INACTIVE' | 'PENDING'>('ACTIVE');
|
||
|
|
const [submitting, setSubmitting] = createSignal(false);
|
||
|
|
const [error, setError] = createSignal('');
|
||
|
|
|
||
|
|
createMemo(() => {
|
||
|
|
const u = user();
|
||
|
|
if (!u) return null;
|
||
|
|
setName(u.name || u.full_name || '');
|
||
|
|
setEmail(u.email || '');
|
||
|
|
setRoleId(u.roleId || u.role_id || u.role?.id || '');
|
||
|
|
setStatus((u.status || 'ACTIVE').toUpperCase() as 'ACTIVE' | 'INACTIVE' | 'PENDING');
|
||
|
|
return null;
|
||
|
|
});
|
||
|
|
|
||
|
|
const save = async () => {
|
||
|
|
if (!name().trim() || !email().trim() || !roleId()) {
|
||
|
|
setError('Please fill in name, email, and role.');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
setSubmitting(true);
|
||
|
|
setError('');
|
||
|
|
const body = {
|
||
|
|
name: name().trim(),
|
||
|
|
email: email().trim(),
|
||
|
|
roleId: roleId(),
|
||
|
|
status: status().toLowerCase(),
|
||
|
|
};
|
||
|
|
|
||
|
|
let res = await fetch(`${API}/api/admin/users/${params.id}`, {
|
||
|
|
method: 'PATCH',
|
||
|
|
headers: { 'Content-Type': 'application/json' },
|
||
|
|
body: JSON.stringify(body),
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!res.ok) {
|
||
|
|
res = await fetch(`${API}/api/users/${params.id}`, {
|
||
|
|
method: 'PATCH',
|
||
|
|
headers: { 'Content-Type': 'application/json' },
|
||
|
|
body: JSON.stringify(body),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!res.ok) {
|
||
|
|
const payload = await res.json().catch(() => ({}));
|
||
|
|
throw new Error(payload.message || 'Failed to update user');
|
||
|
|
}
|
||
|
|
navigate('/admin/users');
|
||
|
|
} catch (err: any) {
|
||
|
|
setError(err.message || 'Failed to update user');
|
||
|
|
} finally {
|
||
|
|
setSubmitting(false);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
return (
|
||
|
|
<AdminShell>
|
||
|
|
<div class="page-hero-card page-actions">
|
||
|
|
<div>
|
||
|
|
<h1 class="page-title">Edit User</h1>
|
||
|
|
<p class="page-subtitle">Update user profile, role assignment, and account status.</p>
|
||
|
|
</div>
|
||
|
|
<div class="page-actions-right">
|
||
|
|
<A class="btn" href={`/admin/users/details/${params.id}`}>View Details</A>
|
||
|
|
<A class="btn" href="/admin/users">Back to Users</A>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<Show when={error()}>
|
||
|
|
<div class="error-box">{error()}</div>
|
||
|
|
</Show>
|
||
|
|
|
||
|
|
<Show when={user.loading}>
|
||
|
|
<div class="card"><p class="notice">Loading user...</p></div>
|
||
|
|
</Show>
|
||
|
|
|
||
|
|
<Show when={!user.loading && !user()}>
|
||
|
|
<div class="card"><p class="notice">User not found.</p></div>
|
||
|
|
</Show>
|
||
|
|
|
||
|
|
<Show when={user()}>
|
||
|
|
<section class="card" style="max-width:900px">
|
||
|
|
<div class="field-grid-2">
|
||
|
|
<div class="field">
|
||
|
|
<label>Full Name</label>
|
||
|
|
<input value={name()} onInput={(e) => setName(e.currentTarget.value)} />
|
||
|
|
</div>
|
||
|
|
<div class="field">
|
||
|
|
<label>Email</label>
|
||
|
|
<input type="email" value={email()} onInput={(e) => setEmail(e.currentTarget.value)} />
|
||
|
|
</div>
|
||
|
|
<div class="field">
|
||
|
|
<label>Role</label>
|
||
|
|
<select value={roleId()} onChange={(e) => setRoleId(e.currentTarget.value)}>
|
||
|
|
<option value="">Select role</option>
|
||
|
|
<Show when={!roles.loading}>
|
||
|
|
{roles()?.map((r) => (
|
||
|
|
<option value={r.id}>{r.name}</option>
|
||
|
|
))}
|
||
|
|
</Show>
|
||
|
|
</select>
|
||
|
|
</div>
|
||
|
|
<div class="field">
|
||
|
|
<label>Status</label>
|
||
|
|
<select value={status()} onChange={(e) => setStatus(e.currentTarget.value as 'ACTIVE' | 'INACTIVE' | 'PENDING')}>
|
||
|
|
<option value="ACTIVE">Active</option>
|
||
|
|
<option value="PENDING">Pending</option>
|
||
|
|
<option value="INACTIVE">Inactive</option>
|
||
|
|
</select>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div class="actions" style="justify-content:flex-end">
|
||
|
|
<button class="btn" type="button" onClick={() => navigate('/admin/users')}>Cancel</button>
|
||
|
|
<button class="btn primary" type="button" onClick={save} disabled={submitting()}>
|
||
|
|
{submitting() ? 'Saving...' : 'Save Changes'}
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
</section>
|
||
|
|
</Show>
|
||
|
|
</AdminShell>
|
||
|
|
);
|
||
|
|
}
|