Skip to content

Commit ce3009f

Browse files
author
Uttam Singh
committed
Replace AdminUsers with invite-only flow (removed password)
1 parent 14262a3 commit ce3009f

File tree

1 file changed

+43
-83
lines changed

1 file changed

+43
-83
lines changed

frontend/src/pages/AdminUsers.jsx

Lines changed: 43 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -11,118 +11,78 @@ export default function AdminUsers() {
1111
manager_email: "",
1212
});
1313

14-
// Load existing users
1514
const loadUsers = async () => {
16-
const res = await fetch(`${API}/users`);
17-
const data = await res.json();
18-
setUsers(data);
15+
try {
16+
const res = await fetch(`${API}/users`);
17+
const data = await res.json();
18+
setUsers(data);
19+
} catch (err) {
20+
console.error("Failed to load users", err);
21+
}
1922
};
2023

21-
useEffect(() => {
22-
loadUsers();
23-
}, []);
24+
useEffect(() => { loadUsers(); }, []);
2425

25-
// NEW: Invite user instead of creating with password
2626
const createUser = async (e) => {
2727
e.preventDefault();
28-
29-
const res = await fetch(`${API}/users/invite`, {
30-
method: "POST",
31-
headers: { "Content-Type": "application/json" },
32-
body: JSON.stringify(form),
33-
});
34-
35-
if (res.ok) {
36-
alert("Invite sent to user email!");
37-
setForm({
38-
name: "",
39-
email: "",
40-
department: "",
41-
role: "auditee",
42-
manager_email: "",
28+
try {
29+
const res = await fetch(`${API}/users/invite`, {
30+
method: "POST",
31+
headers: { "Content-Type": "application/json" },
32+
body: JSON.stringify(form),
4333
});
44-
loadUsers();
45-
} else {
46-
alert("Error sending invite");
34+
if (res.ok) {
35+
alert("Invite email sent to user!");
36+
setForm({ name: "", email: "", department: "", role: "auditee", manager_email: "" });
37+
loadUsers();
38+
} else {
39+
const err = await res.json().catch(()=>({detail:res.statusText}));
40+
alert("Failed to send invite: " + (err.detail || res.statusText));
41+
}
42+
} catch (err) {
43+
console.error(err); alert("Network error sending invite");
4744
}
4845
};
4946

5047
const deleteUser = async (id) => {
51-
if (!window.confirm("Are you sure?")) return;
48+
if (!window.confirm("Delete this user?")) return;
5249
await fetch(`${API}/users/${id}`, { method: "DELETE" });
5350
loadUsers();
5451
};
5552

5653
return (
57-
<div style={{ maxWidth: "800px", margin: "40px auto" }}>
58-
<h2>Admin – User Management</h2>
59-
60-
<form onSubmit={createUser} style={{ display: "grid", gap: 10 }}>
61-
<input
62-
placeholder="Name"
63-
value={form.name}
64-
onChange={(e) => setForm({ ...form, name: e.target.value })}
65-
/>
66-
67-
<input
68-
placeholder="Email"
69-
value={form.email}
70-
onChange={(e) => setForm({ ...form, email: e.target.value })}
71-
/>
72-
73-
<input
74-
placeholder="Department"
75-
value={form.department}
76-
onChange={(e) => setForm({ ...form, department: e.target.value })}
77-
/>
78-
79-
<input
80-
placeholder="Manager Email"
81-
value={form.manager_email}
82-
onChange={(e) => setForm({ ...form, manager_email: e.target.value })}
83-
/>
84-
85-
<select
86-
value={form.role}
87-
onChange={(e) => setForm({ ...form, role: e.target.value })}
88-
>
54+
<div style={{ maxWidth: 900, margin: "40px auto" }}>
55+
<h2>Users</h2>
56+
<p>Create and manage users.</p>
57+
58+
<form onSubmit={createUser} style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 12, marginBottom: 24 }}>
59+
<input placeholder="Full Name" value={form.name} onChange={(e)=>setForm({...form,name:e.target.value})}/>
60+
<input placeholder="Email" value={form.email} onChange={(e)=>setForm({...form,email:e.target.value})}/>
61+
<input placeholder="Department" value={form.department} onChange={(e)=>setForm({...form,department:e.target.value})}/>
62+
<input placeholder="Manager Email" value={form.manager_email} onChange={(e)=>setForm({...form,manager_email:e.target.value})}/>
63+
<select value={form.role} onChange={(e)=>setForm({...form,role:e.target.value})}>
8964
<option value="auditee">Auditee</option>
9065
<option value="auditor">Auditor</option>
9166
<option value="manager">Manager</option>
9267
<option value="admin">Admin</option>
9368
</select>
9469

95-
<button>Create User (Send Invite)</button>
70+
<button type="submit" style={{ gridColumn:"span 2", padding:12, background:"#004aad", color:"white", borderRadius:8, border:"none" }}>
71+
Create User (Send Invite)
72+
</button>
9673
</form>
9774

98-
<h3 style={{ marginTop: "20px" }}>User List</h3>
99-
<table border="1" cellPadding="8" width="100%">
100-
<thead>
101-
<tr>
102-
<th>Name</th>
103-
<th>Email</th>
104-
<th>Dept</th>
105-
<th>Role</th>
106-
<th>Manager</th>
107-
<th>Delete</th>
108-
</tr>
109-
</thead>
110-
75+
<h3>Registered Users</h3>
76+
<table border="1" cellPadding={8} width="100%">
77+
<thead><tr><th>Name</th><th>Email</th><th>Department</th><th>Role</th><th>Manager</th><th>Actions</th></tr></thead>
11178
<tbody>
112-
{users.map((u) => (
79+
{users.map(u=>(
11380
<tr key={u.id}>
114-
<td>{u.name}</td>
115-
<td>{u.email}</td>
116-
<td>{u.department}</td>
117-
<td>{u.role}</td>
118-
<td>{u.manager_email}</td>
119-
<td>
120-
<button onClick={() => deleteUser(u.id)}></button>
121-
</td>
81+
<td>{u.name}</td><td>{u.email}</td><td>{u.department}</td><td>{u.role}</td><td>{u.manager_email}</td>
82+
<td><button onClick={()=>deleteUser(u.id)}>❌ Delete</button></td>
12283
</tr>
12384
))}
12485
</tbody>
125-
12686
</table>
12787
</div>
12888
);

0 commit comments

Comments
 (0)