This commit is contained in:
arrelin
2026-01-17 10:15:44 +03:00
parent 564adac629
commit a4b06fb057
26 changed files with 1542 additions and 346 deletions

View File

@@ -10,57 +10,25 @@ import {
Plus,
Trash2,
Lock,
User,
ArrowLeft,
X,
} from 'lucide-react';
export default function AdminPanel() {
const navigate = useNavigate();
const { isAdmin, setIsAdmin, logout: storeLogout } = useStore();
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [loginError, setLoginError] = useState('');
const { user, logout: storeLogout } = useStore();
const [newFamilyName, setNewFamilyName] = useState('');
const [newFamilyPassword, setNewFamilyPassword] = useState('');
const [families, setFamilies] = useState<Array<{ id: number; name: string }>>([]);
useEffect(() => {
if (isAdmin) {
setIsAuthenticated(true);
loadFamilies();
}
}, [isAdmin]);
const handleLogin = async (e: React.FormEvent) => {
e.preventDefault();
setLoginError('');
try {
const response = await authApi.login({ username, password });
if (response.data.success && response.data.is_admin) {
setIsAdmin(true);
setIsAuthenticated(true);
loadFamilies();
} else {
setLoginError('Доступ запрещен. Требуются права администратора.');
}
} catch (err) {
setLoginError('Неверные учетные данные');
console.error(err);
}
};
loadFamilies();
}, []);
const handleLogout = async () => {
try {
await authApi.logout();
storeLogout();
setIsAuthenticated(false);
setUsername('');
setPassword('');
navigate('/');
} catch (err) {
console.error('Logout error:', err);
@@ -105,78 +73,28 @@ export default function AdminPanel() {
}
};
if (!isAuthenticated) {
if (!user?.is_admin) {
return (
<div className="min-h-screen gradient-bg flex items-center justify-center py-8 sm:py-12 px-4">
<div className="max-w-md w-full">
<div className="glass-effect rounded-2xl shadow-2xl p-6 sm:p-8">
<div className="glass-effect rounded-2xl shadow-2xl p-6 sm:p-8 text-center">
<div className="flex items-center justify-center mb-6">
<div className="p-4 bg-linear-to-br from-purple-500 to-blue-500 text-white rounded-2xl">
<div className="p-4 bg-linear-to-br from-red-500 to-orange-500 text-white rounded-2xl">
<Shield className="w-10 h-10 sm:w-12 sm:h-12" />
</div>
</div>
<h1 className="text-2xl sm:text-3xl font-bold text-gray-900 mb-2 text-center">
Админ панель
<h1 className="text-2xl sm:text-3xl font-bold text-gray-900 mb-2">
Доступ запрещен
</h1>
<p className="text-gray-600 text-center mb-6 text-sm sm:text-base">
Войдите для управления системой
<p className="text-gray-600 mb-6 text-sm sm:text-base">
Требуются права администратора
</p>
{loginError && (
<div className="mb-4 p-3 bg-red-100 border-l-4 border-red-500 text-red-700 rounded">
<div className="flex items-center gap-2">
<X className="w-4 h-4 flex-shrink-0" />
<span className="text-sm">{loginError}</span>
</div>
</div>
)}
<form onSubmit={handleLogin} className="space-y-4">
<div>
<label className="flex items-center gap-2 text-sm font-medium text-gray-700 mb-2">
<User className="w-4 h-4" />
Логин
</label>
<input
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
className="w-full px-4 py-2.5 sm:py-3 border-2 border-gray-300 rounded-xl focus:ring-2 focus:ring-purple-500 focus:border-purple-500 transition-all text-sm sm:text-base"
placeholder="Введите логин"
required
/>
</div>
<div>
<label className="flex items-center gap-2 text-sm font-medium text-gray-700 mb-2">
<Lock className="w-4 h-4" />
Пароль
</label>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
className="w-full px-4 py-2.5 sm:py-3 border-2 border-gray-300 rounded-xl focus:ring-2 focus:ring-purple-500 focus:border-purple-500 transition-all text-sm sm:text-base"
placeholder="Введите пароль"
required
/>
</div>
<button
type="submit"
className="w-full flex items-center justify-center gap-2 px-4 py-2.5 sm:py-3 bg-linear-to-r from-purple-600 to-blue-600 text-white rounded-xl hover:shadow-lg transition-all duration-300 font-medium text-sm sm:text-base"
>
<Shield className="w-4 h-4 sm:w-5 sm:h-5" />
Войти
</button>
</form>
<button
onClick={() => navigate('/')}
className="w-full mt-4 flex items-center justify-center gap-2 px-4 py-2.5 sm:py-3 bg-gray-200 hover:bg-gray-300 text-gray-700 rounded-xl transition-all duration-300 font-medium text-sm sm:text-base"
className="w-full flex items-center justify-center gap-2 px-4 py-3 bg-gray-200 hover:bg-gray-300 text-gray-700 rounded-xl transition-all duration-300 font-medium"
>
<ArrowLeft className="w-4 h-4 sm:w-5 sm:h-5" />
На главную
<ArrowLeft className="w-5 h-5" />
Назад
</button>
</div>
</div>