feat(ui): 构建资产管理页面与添加资产表单,打通 Server Action 录入链路

This commit is contained in:
kennethcheng 2026-04-27 21:35:41 +08:00
parent f160c2bdcb
commit 19e23dc933
3 changed files with 227 additions and 12 deletions

View 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>
);
}

View File

@ -1,6 +1,13 @@
import { LayoutGrid, Wallet, ArrowLeftRight } from 'lucide-react';
import { ThemeToggle } from '@/components/theme-toggle';
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({
children,
@ -15,18 +22,17 @@ export default function DashboardLayout({
</div>
<nav className="flex flex-col gap-2 flex-1">
<Button variant="ghost" className="justify-start gap-2">
<LayoutGrid className="h-4 w-4" />
(Overview)
</Button>
<Button variant="ghost" className="justify-start gap-2">
<Wallet className="h-4 w-4" />
(Assets)
</Button>
<Button variant="ghost" className="justify-start gap-2">
<ArrowLeftRight className="h-4 w-4" />
(Transactions)
</Button>
{navItems.map(({ href, label, icon: Icon }) => (
<Link key={href} href={href}>
<Button
variant="ghost"
className="justify-start gap-2 w-full"
>
<Icon className="h-4 w-4" />
{label}
</Button>
</Link>
))}
</nav>
<div className="mt-auto flex justify-end">

View 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>
);
}