Files
asle/frontend/app/admin/users/page.tsx
defiQUG 507d9a35b1 Add initial project structure and documentation files
- Created .gitignore to exclude sensitive files and directories.
- Added API documentation in API_DOCUMENTATION.md.
- Included deployment instructions in DEPLOYMENT.md.
- Established project structure documentation in PROJECT_STRUCTURE.md.
- Updated README.md with project status and team information.
- Added recommendations and status tracking documents.
- Introduced testing guidelines in TESTING.md.
- Set up CI workflow in .github/workflows/ci.yml.
- Created Dockerfile for backend and frontend setups.
- Added various service and utility files for backend functionality.
- Implemented frontend components and pages for user interface.
- Included mobile app structure and services.
- Established scripts for deployment across multiple chains.
2025-12-03 21:22:31 -08:00

183 lines
5.9 KiB
TypeScript

'use client';
import { useEffect, useState } from 'react';
export default function AdminUsersPage() {
const [users, setUsers] = useState<any[]>([]);
const [loading, setLoading] = useState(true);
const [showCreateModal, setShowCreateModal] = useState(false);
const [formData, setFormData] = useState({
email: '',
password: '',
role: 'admin',
permissions: [] as string[],
});
useEffect(() => {
fetchUsers();
}, []);
const fetchUsers = async () => {
const token = localStorage.getItem('admin_token');
try {
const res = await fetch('/api/admin/users', {
headers: {
Authorization: `Bearer ${token}`,
},
});
const data = await res.json();
setUsers(data);
} catch (error) {
console.error('Failed to fetch users:', error);
} finally {
setLoading(false);
}
};
const handleCreate = async (e: React.FormEvent) => {
e.preventDefault();
const token = localStorage.getItem('admin_token');
try {
const res = await fetch('/api/admin/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(formData),
});
if (res.ok) {
setShowCreateModal(false);
setFormData({ email: '', password: '', role: 'admin', permissions: [] });
fetchUsers();
}
} catch (error) {
console.error('Failed to create user:', error);
}
};
const handleDelete = async (id: string) => {
if (!confirm('Are you sure you want to delete this user?')) return;
const token = localStorage.getItem('admin_token');
try {
const res = await fetch(`/api/admin/users/${id}`, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${token}`,
},
});
if (res.ok) {
fetchUsers();
}
} catch (error) {
console.error('Failed to delete user:', error);
}
};
if (loading) {
return <div className="text-center py-12">Loading...</div>;
}
return (
<div className="px-4 py-6 sm:px-0">
<div className="flex justify-between items-center mb-6">
<h1 className="text-3xl font-bold text-gray-900">Admin Users</h1>
<button
onClick={() => setShowCreateModal(true)}
className="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700"
>
Create User
</button>
</div>
<div className="bg-white shadow overflow-hidden sm:rounded-md">
<ul className="divide-y divide-gray-200">
{users.map((user) => (
<li key={user.id}>
<div className="px-4 py-4 sm:px-6 flex justify-between items-center">
<div>
<p className="text-sm font-medium text-gray-900">{user.email}</p>
<p className="text-sm text-gray-500">
Role: {user.role} | Permissions: {user.permissions.length}
</p>
</div>
<button
onClick={() => handleDelete(user.id)}
className="text-red-600 hover:text-red-900 text-sm"
>
Delete
</button>
</div>
</li>
))}
</ul>
</div>
{showCreateModal && (
<div className="fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full z-50">
<div className="relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white">
<h3 className="text-lg font-bold mb-4">Create Admin User</h3>
<form onSubmit={handleCreate}>
<div className="mb-4">
<label className="block text-sm font-medium text-gray-700 mb-1">
Email
</label>
<input
type="email"
required
className="w-full px-3 py-2 border border-gray-300 rounded-md"
value={formData.email}
onChange={(e) => setFormData({ ...formData, email: e.target.value })}
/>
</div>
<div className="mb-4">
<label className="block text-sm font-medium text-gray-700 mb-1">
Password
</label>
<input
type="password"
required
className="w-full px-3 py-2 border border-gray-300 rounded-md"
value={formData.password}
onChange={(e) => setFormData({ ...formData, password: e.target.value })}
/>
</div>
<div className="mb-4">
<label className="block text-sm font-medium text-gray-700 mb-1">
Role
</label>
<select
className="w-full px-3 py-2 border border-gray-300 rounded-md"
value={formData.role}
onChange={(e) => setFormData({ ...formData, role: e.target.value })}
>
<option value="admin">Admin</option>
<option value="super_admin">Super Admin</option>
<option value="operator">Operator</option>
</select>
</div>
<div className="flex justify-end space-x-3">
<button
type="button"
onClick={() => setShowCreateModal(false)}
className="px-4 py-2 border border-gray-300 rounded-md text-gray-700 hover:bg-gray-50"
>
Cancel
</button>
<button
type="submit"
className="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700"
>
Create
</button>
</div>
</form>
</div>
</div>
)}
</div>
);
}