feat(ui): 构建资产管理页面与添加资产表单,打通 Server Action 录入链路
This commit is contained in:
parent
f160c2bdcb
commit
19e23dc933
66
app/dashboard/assets/page.tsx
Normal file
66
app/dashboard/assets/page.tsx
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import { getAssets } from '@/actions/asset';
|
||||||
|
import { AddAssetDialog } from '@/components/assets/add-asset-dialog';
|
||||||
|
import {
|
||||||
|
Table,
|
||||||
|
TableBody,
|
||||||
|
TableCaption,
|
||||||
|
TableCell,
|
||||||
|
TableHead,
|
||||||
|
TableHeader,
|
||||||
|
TableRow,
|
||||||
|
} from '@/components/ui/table';
|
||||||
|
|
||||||
|
export default async function AssetsPage() {
|
||||||
|
const assets = await getAssets();
|
||||||
|
|
||||||
|
const typeLabels: Record<string, string> = {
|
||||||
|
STOCK: '股票',
|
||||||
|
CRYPTO: '加密货币',
|
||||||
|
CASH: '现金',
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-6">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<h1 className="text-2xl font-bold">资产列表</h1>
|
||||||
|
<AddAssetDialog />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="rounded-md border">
|
||||||
|
<Table>
|
||||||
|
<TableCaption>数据库中所有已录入的资产</TableCaption>
|
||||||
|
<TableHeader>
|
||||||
|
<TableRow>
|
||||||
|
<TableHead>资产代码</TableHead>
|
||||||
|
<TableHead>类型</TableHead>
|
||||||
|
<TableHead>基础币种</TableHead>
|
||||||
|
<TableHead>创建时间</TableHead>
|
||||||
|
</TableRow>
|
||||||
|
</TableHeader>
|
||||||
|
<TableBody>
|
||||||
|
{assets.length === 0 ? (
|
||||||
|
<TableRow>
|
||||||
|
<TableCell colSpan={4} className="text-center text-muted-foreground">
|
||||||
|
暂无资产,点击"添加资产"按钮录入第一个资产
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
) : (
|
||||||
|
assets.map((asset) => (
|
||||||
|
<TableRow key={asset.id}>
|
||||||
|
<TableCell className="font-medium">{asset.symbol}</TableCell>
|
||||||
|
<TableCell>{typeLabels[asset.type] || asset.type}</TableCell>
|
||||||
|
<TableCell>{asset.baseCurrency}</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
{asset.createdAt
|
||||||
|
? new Date(asset.createdAt).toLocaleString('zh-CN')
|
||||||
|
: '-'}
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</TableBody>
|
||||||
|
</Table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@ -1,6 +1,13 @@
|
|||||||
import { LayoutGrid, Wallet, ArrowLeftRight } from 'lucide-react';
|
import { LayoutGrid, Wallet, ArrowLeftRight } from 'lucide-react';
|
||||||
import { ThemeToggle } from '@/components/theme-toggle';
|
import { ThemeToggle } from '@/components/theme-toggle';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
|
import Link from 'next/link';
|
||||||
|
|
||||||
|
const navItems = [
|
||||||
|
{ href: '/dashboard', label: '总览 (Overview)', icon: LayoutGrid },
|
||||||
|
{ href: '/dashboard/assets', label: '资产 (Assets)', icon: Wallet },
|
||||||
|
{ href: '/dashboard/transactions', label: '流水 (Transactions)', icon: ArrowLeftRight },
|
||||||
|
];
|
||||||
|
|
||||||
export default function DashboardLayout({
|
export default function DashboardLayout({
|
||||||
children,
|
children,
|
||||||
@ -15,18 +22,17 @@ export default function DashboardLayout({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<nav className="flex flex-col gap-2 flex-1">
|
<nav className="flex flex-col gap-2 flex-1">
|
||||||
<Button variant="ghost" className="justify-start gap-2">
|
{navItems.map(({ href, label, icon: Icon }) => (
|
||||||
<LayoutGrid className="h-4 w-4" />
|
<Link key={href} href={href}>
|
||||||
总览 (Overview)
|
<Button
|
||||||
</Button>
|
variant="ghost"
|
||||||
<Button variant="ghost" className="justify-start gap-2">
|
className="justify-start gap-2 w-full"
|
||||||
<Wallet className="h-4 w-4" />
|
>
|
||||||
资产 (Assets)
|
<Icon className="h-4 w-4" />
|
||||||
</Button>
|
{label}
|
||||||
<Button variant="ghost" className="justify-start gap-2">
|
|
||||||
<ArrowLeftRight className="h-4 w-4" />
|
|
||||||
流水 (Transactions)
|
|
||||||
</Button>
|
</Button>
|
||||||
|
</Link>
|
||||||
|
))}
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
<div className="mt-auto flex justify-end">
|
<div className="mt-auto flex justify-end">
|
||||||
|
|||||||
143
src/components/assets/add-asset-dialog.tsx
Normal file
143
src/components/assets/add-asset-dialog.tsx
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useState, useTransition } from 'react';
|
||||||
|
import { useForm } from 'react-hook-form';
|
||||||
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
|
import { z } from 'zod';
|
||||||
|
import { useRouter } from 'next/navigation';
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogContent,
|
||||||
|
DialogDescription,
|
||||||
|
DialogFooter,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle,
|
||||||
|
DialogTrigger,
|
||||||
|
} from '@/components/ui/dialog';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Input } from '@/components/ui/input';
|
||||||
|
import {
|
||||||
|
Form,
|
||||||
|
FormControl,
|
||||||
|
FormField,
|
||||||
|
FormItem,
|
||||||
|
FormLabel,
|
||||||
|
FormMessage,
|
||||||
|
} from '@/components/ui/form';
|
||||||
|
import {
|
||||||
|
Select,
|
||||||
|
SelectContent,
|
||||||
|
SelectItem,
|
||||||
|
SelectTrigger,
|
||||||
|
SelectValue,
|
||||||
|
} from '@/components/ui/select';
|
||||||
|
import { Plus } from 'lucide-react';
|
||||||
|
import { createAsset } from '@/actions/asset';
|
||||||
|
|
||||||
|
const addAssetSchema = z.object({
|
||||||
|
symbol: z.string().min(1, '资产代码不能为空'),
|
||||||
|
type: z.enum(['STOCK', 'CRYPTO', 'CASH']),
|
||||||
|
baseCurrency: z.string().min(2, '基础币种至少2个字符'),
|
||||||
|
});
|
||||||
|
|
||||||
|
type AddAssetForm = z.infer<typeof addAssetSchema>;
|
||||||
|
|
||||||
|
export function AddAssetDialog() {
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
const [isPending, startTransition] = useTransition();
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
const form = useForm<AddAssetForm>({
|
||||||
|
resolver: zodResolver(addAssetSchema),
|
||||||
|
defaultValues: {
|
||||||
|
symbol: '',
|
||||||
|
type: 'STOCK' as const,
|
||||||
|
baseCurrency: '',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
function onSubmit(values: AddAssetForm) {
|
||||||
|
startTransition(async () => {
|
||||||
|
const result = await createAsset(values);
|
||||||
|
if (result.success) {
|
||||||
|
setOpen(false);
|
||||||
|
form.reset();
|
||||||
|
router.refresh();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={open} onOpenChange={setOpen}>
|
||||||
|
<DialogTrigger asChild>
|
||||||
|
<Button>
|
||||||
|
<Plus className="h-4 w-4 mr-2" />
|
||||||
|
添加资产
|
||||||
|
</Button>
|
||||||
|
</DialogTrigger>
|
||||||
|
<DialogContent className="sm:max-w-[425px]">
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>添加新资产</DialogTitle>
|
||||||
|
<DialogDescription>录入一个新的资产到系统中</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
<Form {...form}>
|
||||||
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="symbol"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>资产代码</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input placeholder="例如: BTC" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="type"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>资产类型</FormLabel>
|
||||||
|
<Select onValueChange={field.onChange} defaultValue={field.value}>
|
||||||
|
<FormControl>
|
||||||
|
<SelectTrigger>
|
||||||
|
<SelectValue placeholder="选择资产类型" />
|
||||||
|
</SelectTrigger>
|
||||||
|
</FormControl>
|
||||||
|
<SelectContent>
|
||||||
|
<SelectItem value="STOCK">股票 (STOCK)</SelectItem>
|
||||||
|
<SelectItem value="CRYPTO">加密货币 (CRYPTO)</SelectItem>
|
||||||
|
<SelectItem value="CASH">现金 (CASH)</SelectItem>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="baseCurrency"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>基础币种</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input placeholder="例如: USD" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<DialogFooter>
|
||||||
|
<Button type="submit" disabled={isPending}>
|
||||||
|
{isPending ? '提交中...' : '确认添加'}
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</form>
|
||||||
|
</Form>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user