feat: add portfolio list page
This commit is contained in:
parent
7edc152491
commit
cbf30c6bb4
126
frontend/src/app/portfolio/page.tsx
Normal file
126
frontend/src/app/portfolio/page.tsx
Normal file
@ -0,0 +1,126 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import Link from 'next/link';
|
||||
import Sidebar from '@/components/layout/Sidebar';
|
||||
import Header from '@/components/layout/Header';
|
||||
import { api } from '@/lib/api';
|
||||
|
||||
interface Portfolio {
|
||||
id: number;
|
||||
name: string;
|
||||
portfolio_type: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
interface User {
|
||||
id: number;
|
||||
username: string;
|
||||
email: string;
|
||||
}
|
||||
|
||||
export default function PortfolioListPage() {
|
||||
const router = useRouter();
|
||||
const [user, setUser] = useState<User | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [portfolios, setPortfolios] = useState<Portfolio[]>([]);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const init = async () => {
|
||||
try {
|
||||
const userData = await api.getCurrentUser() as User;
|
||||
setUser(userData);
|
||||
await fetchPortfolios();
|
||||
} catch {
|
||||
router.push('/login');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
init();
|
||||
}, [router]);
|
||||
|
||||
const fetchPortfolios = async () => {
|
||||
try {
|
||||
setError(null);
|
||||
const data = await api.get<Portfolio[]>('/api/portfolios');
|
||||
setPortfolios(data);
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'Failed to fetch portfolios';
|
||||
setError(message);
|
||||
}
|
||||
};
|
||||
|
||||
const getTypeLabel = (type: string) => {
|
||||
return type === 'pension' ? '퇴직연금' : '일반';
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center">
|
||||
<div className="text-gray-500">Loading...</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex min-h-screen">
|
||||
<Sidebar />
|
||||
<div className="flex-1">
|
||||
<Header username={user?.username} />
|
||||
<main className="p-6">
|
||||
<div className="flex justify-between items-center mb-6">
|
||||
<h1 className="text-2xl font-bold text-gray-800">포트폴리오</h1>
|
||||
<Link
|
||||
href="/portfolio/new"
|
||||
className="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 transition-colors"
|
||||
>
|
||||
새 포트폴리오
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
{portfolios.map((portfolio) => (
|
||||
<Link
|
||||
key={portfolio.id}
|
||||
href={`/portfolio/${portfolio.id}`}
|
||||
className="bg-white rounded-lg shadow p-6 hover:shadow-lg transition-shadow"
|
||||
>
|
||||
<div className="flex justify-between items-start mb-2">
|
||||
<h2 className="text-lg font-semibold text-gray-800">
|
||||
{portfolio.name}
|
||||
</h2>
|
||||
<span className={`text-xs px-2 py-1 rounded ${
|
||||
portfolio.portfolio_type === 'pension'
|
||||
? 'bg-purple-100 text-purple-800'
|
||||
: 'bg-gray-100 text-gray-800'
|
||||
}`}>
|
||||
{getTypeLabel(portfolio.portfolio_type)}
|
||||
</span>
|
||||
</div>
|
||||
<p className="text-sm text-gray-500">
|
||||
생성일: {new Date(portfolio.created_at).toLocaleDateString('ko-KR')}
|
||||
</p>
|
||||
</Link>
|
||||
))}
|
||||
|
||||
{portfolios.length === 0 && !error && (
|
||||
<div className="col-span-full text-center py-12 text-gray-500">
|
||||
아직 포트폴리오가 없습니다. 새 포트폴리오를 생성해보세요.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user