Files
platform-project/request-module/frontend/src/pages/UserList.tsx
T
Арсентьев Александр 07dbde2a0e Initial monorepo structure
2026-08-06 11:23:37 +03:00

170 lines
7.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useState, useEffect } from 'react'
import { Link } from 'react-router-dom'
import { usersApi } from '../api/client'
import { User } from '../types'
import { Search, Plus, Edit, Trash2, Shield } from 'lucide-react'
export default function UserList() {
const [users, setUsers] = useState<User[]>([])
const [loading, setLoading] = useState(true)
const [search, setSearch] = useState('')
const [page, setPage] = useState(1)
const [total, setTotal] = useState(0)
const pageSize = 20
const fetchUsers = () => {
setLoading(true)
usersApi.list({
page,
page_size: pageSize,
search: search || undefined,
}).then(res => {
setUsers(res.data.users)
setTotal(res.data.total)
}).finally(() => setLoading(false))
}
useEffect(() => {
fetchUsers()
}, [page])
const handleSearch = (e: React.FormEvent) => {
e.preventDefault()
setPage(1)
fetchUsers()
}
const handleDelete = async (userId: number) => {
if (!confirm('Удалить пользователя? Это действие нельзя отменить.')) return
try {
await usersApi.delete(userId)
fetchUsers()
} catch (err: any) {
alert(err.response?.data?.detail || 'Ошибка')
}
}
return (
<div>
<div className="flex items-center justify-between mb-6">
<span className="text-sm text-gray-500">Локальные и платформенные пользователи</span>
<Link to="/users/new" className="flex items-center px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700">
<Plus className="w-4 h-4 mr-2" />Добавить
</Link>
</div>
<div className="bg-white rounded-lg shadow p-4 mb-6">
<form onSubmit={handleSearch} className="flex gap-4 items-end">
<div className="flex-1">
<label className="block text-sm font-medium text-gray-700 mb-1">Поиск</label>
<div className="relative">
<Search className="absolute left-3 top-2.5 w-4 h-4 text-gray-400" />
<input
type="text"
value={search}
onChange={(e) => setSearch(e.target.value)}
placeholder="Поиск пользователей..."
className="w-full pl-10 pr-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
</div>
<button type="submit" className="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700">
Найти
</button>
</form>
</div>
<div className="bg-white rounded-lg shadow overflow-hidden">
{loading ? (
<div className="p-8 text-center">Загрузка...</div>
) : (
<>
<table className="w-full">
<thead className="bg-gray-50">
<tr>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">ID</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Имя</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Email</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Роли</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Источник</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Статус</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Действия</th>
</tr>
</thead>
<tbody className="divide-y divide-gray-200">
{users.map((user) => (
<tr key={user.id} className="hover:bg-gray-50">
<td className="px-6 py-4 text-sm text-gray-500">#{user.id}</td>
<td className="px-6 py-4">
<div className="flex items-center">
<div className="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center text-white text-sm font-medium">
{user.first_name?.[0]}{user.last_name?.[0]}
</div>
<div className="ml-3">
<p className="text-sm font-medium text-gray-900">{user.first_name} {user.last_name}</p>
</div>
</div>
</td>
<td className="px-6 py-4 text-sm text-gray-500">{user.email}</td>
<td className="px-6 py-4">
{user.roles && user.roles.length > 0 ? (
<div className="flex gap-1 flex-wrap">
{user.roles.map(role => (
<span key={role} className="px-2 py-0.5 text-xs bg-blue-100 text-blue-800 rounded-full flex items-center gap-1">
<Shield className="w-3 h-3" />{role}
</span>
))}
</div>
) : (
<span className="text-xs text-gray-400">-</span>
)}
</td>
<td className="px-6 py-4">
<span className={`px-2 py-1 text-xs rounded-full ${user.source === 'platform' ? 'bg-purple-100 text-purple-800' : 'bg-gray-100 text-gray-800'}`}>
{user.source === 'platform' ? 'Платформа' : 'Локальный'}
</span>
</td>
<td className="px-6 py-4">
<span className={`px-2 py-1 text-xs rounded-full ${user.is_active ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'}`}>
{user.is_active ? 'Активен' : 'Неактивен'}
</span>
</td>
<td className="px-6 py-4">
{user.source === 'local' ? (
<div className="flex gap-2">
<Link to={`/users/${user.id}`} className="text-blue-500 hover:text-blue-700" title="Редактировать">
<Edit className="w-4 h-4" />
</Link>
<button onClick={() => handleDelete(user.id)} className="text-red-500 hover:text-red-700" title="Удалить">
<Trash2 className="w-4 h-4" />
</button>
</div>
) : (
<span className="text-xs text-gray-400">Только просмотр</span>
)}
</td>
</tr>
))}
</tbody>
</table>
{total > pageSize && (
<div className="px-6 py-4 border-t flex items-center justify-between">
<span className="text-sm text-gray-500">
Показано {(page - 1) * pageSize + 1}-{Math.min(page * pageSize, total)} из {total}
</span>
<div className="flex gap-2">
<button onClick={() => setPage(p => Math.max(1, p - 1))} disabled={page === 1}
className="px-3 py-1 border rounded disabled:opacity-50">Назад</button>
<button onClick={() => setPage(p => p + 1)} disabled={page * pageSize >= total}
className="px-3 py-1 border rounded disabled:opacity-50">Далее</button>
</div>
</div>
)}
</>
)}
</div>
</div>
)
}