-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleList.tsx
More file actions
151 lines (140 loc) · 4.8 KB
/
ExampleList.tsx
File metadata and controls
151 lines (140 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use client';
import { useState, useEffect } from 'react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { api, ApiError } from '@/lib/api-client';
import { toast } from 'sonner';
interface Example {
id: number;
name: string;
description?: string;
created_at?: string;
}
export default function ExampleList() {
const [examples, setExamples] = useState<Example[]>([]);
const [loading, setLoading] = useState(false);
const [newExample, setNewExample] = useState({ name: '', description: '' });
// 获取示例数据
const fetchExamples = async () => {
setLoading(true);
try {
const data = await api.get<Example[]>('/example');
setExamples(data);
} catch (error) {
if (error instanceof ApiError) {
toast.error(`Failed to fetch examples: ${error.message}`);
} else {
toast.error('An unexpected error occurred');
}
} finally {
setLoading(false);
}
};
// 创建新示例
const createExample = async () => {
if (!newExample.name.trim()) {
toast.error('Name is required');
return;
}
try {
const data = await api.post<Example>('/example', newExample);
setExamples(prev => [...prev, data]);
setNewExample({ name: '', description: '' });
toast.success('Example created successfully');
} catch (error) {
if (error instanceof ApiError) {
toast.error(`Failed to create example: ${error.message}`);
} else {
toast.error('An unexpected error occurred');
}
}
};
// 删除示例
const deleteExample = async (id: number) => {
try {
await api.delete(`/example?id=${id}`);
setExamples(prev => prev.filter(item => item.id !== id));
toast.success('Example deleted successfully');
} catch (error) {
if (error instanceof ApiError) {
toast.error(`Failed to delete example: ${error.message}`);
} else {
toast.error('An unexpected error occurred');
}
}
};
useEffect(() => {
fetchExamples();
}, []);
return (
<div className="w-full max-w-4xl mx-auto p-6 space-y-6">
<Card>
<CardHeader>
<CardTitle>Examples Management</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
{/* 创建新示例 */}
<div className="flex gap-2 flex-wrap">
<Input
placeholder="Name"
value={newExample.name}
onChange={(e) => setNewExample(prev => ({ ...prev, name: e.target.value }))}
className="flex-1 min-w-0"
/>
<Input
placeholder="Description (optional)"
value={newExample.description}
onChange={(e) => setNewExample(prev => ({ ...prev, description: e.target.value }))}
className="flex-1 min-w-0"
/>
<Button onClick={createExample} className="whitespace-nowrap">
Add Example
</Button>
</div>
{/* 刷新按钮 */}
<div className="flex justify-between items-center">
<Button variant="outline" onClick={fetchExamples} disabled={loading}>
{loading ? 'Loading...' : 'Refresh'}
</Button>
</div>
{/* 示例列表 */}
<div className="space-y-2">
{examples.length === 0 ? (
<p className="text-muted-foreground text-center py-8">
{loading ? 'Loading examples...' : 'No examples found. Create one above.'}
</p>
) : (
examples.map((example) => (
<Card key={example.id} className="p-4">
<div className="flex justify-between items-start">
<div className="flex-1">
<h3 className="font-semibold">{example.name}</h3>
{example.description && (
<p className="text-sm text-muted-foreground mt-1">
{example.description}
</p>
)}
{example.created_at && (
<p className="text-xs text-muted-foreground mt-2">
Created: {new Date(example.created_at).toLocaleDateString()}
</p>
)}
</div>
<Button
variant="destructive"
size="sm"
onClick={() => deleteExample(example.id)}
>
Delete
</Button>
</div>
</Card>
))
)}
</div>
</CardContent>
</Card>
</div>
);
}