oauth2
This commit is contained in:
182
frontend/src/pages/NoFamily.tsx
Normal file
182
frontend/src/pages/NoFamily.tsx
Normal file
@@ -0,0 +1,182 @@
|
||||
import { useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useStore } from '../store/useStore';
|
||||
import { authApi, familyApi } from '../api/client';
|
||||
import { Users, LogOut, Settings, Plus, Loader2, Eye, EyeOff } from 'lucide-react';
|
||||
|
||||
export default function NoFamily() {
|
||||
const navigate = useNavigate();
|
||||
const { user, logout, setUser } = useStore();
|
||||
|
||||
const [familyName, setFamilyName] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [showPassword, setShowPassword] = useState(false);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
|
||||
const handleLogout = async () => {
|
||||
try {
|
||||
await authApi.logout();
|
||||
logout();
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
logout();
|
||||
}
|
||||
};
|
||||
|
||||
const handleGoToAdmin = () => {
|
||||
navigate('/adminpanel');
|
||||
};
|
||||
|
||||
const handleCreateFamily = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (!familyName.trim()) {
|
||||
setError('Введите название семьи');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setLoading(true);
|
||||
setError('');
|
||||
|
||||
const response = await familyApi.createMyFamily({
|
||||
name: familyName.trim(),
|
||||
password: password || undefined,
|
||||
});
|
||||
|
||||
setUser({
|
||||
...user!,
|
||||
family_id: response.data.family_id,
|
||||
});
|
||||
|
||||
navigate(`/family/${response.data.family_id}`);
|
||||
} catch (err: unknown) {
|
||||
if (err && typeof err === 'object' && 'response' in err) {
|
||||
const axiosError = err as { response?: { status?: number } };
|
||||
if (axiosError.response?.status === 409) {
|
||||
setError('Вы уже состоите в семье');
|
||||
} else {
|
||||
setError('Ошибка при создании семьи');
|
||||
}
|
||||
} else {
|
||||
setError('Ошибка при создании семьи');
|
||||
}
|
||||
console.error(err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="min-h-screen gradient-bg flex items-center justify-center px-4">
|
||||
<div className="w-full max-w-md">
|
||||
<div className="glass-effect rounded-3xl shadow-2xl p-8 sm:p-10">
|
||||
<div className="flex flex-col items-center text-center mb-6">
|
||||
<div className="p-4 bg-linear-to-br from-purple-500 to-blue-500 text-white rounded-2xl mb-4 shadow-lg">
|
||||
<Users className="w-12 h-12" />
|
||||
</div>
|
||||
<h1 className="text-2xl sm:text-3xl font-bold text-gray-900 mb-2">
|
||||
Добро пожаловать!
|
||||
</h1>
|
||||
<p className="text-gray-600">
|
||||
<span className="font-semibold text-purple-600">{user?.email || user?.username}</span>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<form onSubmit={handleCreateFamily} className="mb-6">
|
||||
<h2 className="text-lg font-semibold text-gray-800 mb-4 text-center">
|
||||
Создайте свою семью
|
||||
</h2>
|
||||
|
||||
{error && (
|
||||
<div className="mb-4 p-3 bg-red-100 border-l-4 border-red-500 text-red-700 rounded-lg">
|
||||
<span className="text-sm font-medium">{error}</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="space-y-4">
|
||||
<div>
|
||||
<label htmlFor="familyName" className="block text-sm font-medium text-gray-700 mb-1">
|
||||
Название семьи *
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
id="familyName"
|
||||
value={familyName}
|
||||
onChange={(e) => setFamilyName(e.target.value)}
|
||||
placeholder="Например: Семья Ивановых"
|
||||
className="w-full px-4 py-3 border-2 border-gray-300 rounded-xl focus:border-purple-500 focus:outline-none transition-colors"
|
||||
disabled={loading}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label htmlFor="password" className="block text-sm font-medium text-gray-700 mb-1">
|
||||
Пароль <span className="text-gray-400">(необязательно)</span>
|
||||
</label>
|
||||
<div className="relative">
|
||||
<input
|
||||
type={showPassword ? 'text' : 'password'}
|
||||
id="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
placeholder="Для защиты доступа"
|
||||
className="w-full px-4 py-3 pr-12 border-2 border-gray-300 rounded-xl focus:border-purple-500 focus:outline-none transition-colors"
|
||||
disabled={loading}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowPassword(!showPassword)}
|
||||
className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-500 hover:text-gray-700"
|
||||
>
|
||||
{showPassword ? <EyeOff className="w-5 h-5" /> : <Eye className="w-5 h-5" />}
|
||||
</button>
|
||||
</div>
|
||||
<p className="mt-1 text-xs text-gray-500">
|
||||
Пароль понадобится для доступа к бюджету семьи
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading || !familyName.trim()}
|
||||
className="w-full flex items-center justify-center gap-2 px-6 py-4 bg-linear-to-r from-purple-600 to-blue-600 text-white rounded-2xl hover:shadow-xl transition-all duration-300 font-semibold disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
>
|
||||
{loading ? (
|
||||
<Loader2 className="w-5 h-5 animate-spin" />
|
||||
) : (
|
||||
<>
|
||||
<Plus className="w-5 h-5" />
|
||||
Создать семью
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div className="border-t border-gray-200 pt-6">
|
||||
<div className="flex flex-col gap-3">
|
||||
{user?.is_admin && (
|
||||
<button
|
||||
onClick={handleGoToAdmin}
|
||||
className="w-full flex items-center justify-center gap-2 px-6 py-3 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-2xl transition-all duration-300 font-medium"
|
||||
>
|
||||
<Settings className="w-5 h-5" />
|
||||
Админ панель
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
onClick={handleLogout}
|
||||
className="w-full flex items-center justify-center gap-2 px-6 py-3 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-2xl transition-all duration-300 font-medium"
|
||||
>
|
||||
<LogOut className="w-5 h-5" />
|
||||
Выйти
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user