Skip to content

Commit 77f8fdf

Browse files
authored
Merge pull request #2 from martinwq997/feature/api-route
route handler and antd-message
2 parents 64580ad + e699401 commit 77f8fdf

File tree

5 files changed

+35
-42
lines changed

5 files changed

+35
-42
lines changed

.env.example

Lines changed: 0 additions & 13 deletions
This file was deleted.

app/api/submit/route.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
import { NextRequest, NextResponse } from 'next/server';
2-
import pool from '../../lib/db'; // 导入数据库连接池
1+
import { NextResponse } from 'next/server';
32

4-
export async function POST(req: NextRequest) {
5-
try {
6-
const { content } = await req.json(); // 解析请求体中的 JSON 数据
3+
export async function POST(request: { json: () => any; }) {
4+
const apiUrl = process.env.API_URL; // 读取环境变量,获取后端服务的基础 URL
5+
const body = await request.json(); // 解析请求体
76

8-
if (!content) {
9-
return NextResponse.json({ error: '内容不能为空' }, { status: 400 });
7+
try {
8+
const response = await fetch(`${apiUrl}/submit`, { //向后端发送服务请求
9+
method: 'POST',
10+
headers: {
11+
'Content-Type': 'application/json',
12+
},
13+
body: JSON.stringify(body), // 将请求体发送到后端
14+
});
15+
//请求失败直接返回
16+
if (!response.ok) {
17+
return NextResponse.json({ error: 'Failed to submit data' }, { status: 500 });
1018
}
11-
12-
const client = await pool.connect(); // 获取数据库连接
13-
// 假设我们将内容插入到名为 'submissions' 的表中
14-
await client.query('INSERT INTO submissions (content) VALUES ($1)', [content]);
15-
client.release(); // 释放连接
16-
17-
return NextResponse.json({ message: '内容提交成功!' }, { status: 200 });
19+
//解析成功的响应
20+
const result = await response.json();
21+
return NextResponse.json({ message: 'Submission successful', data: result });
1822
} catch (error) {
19-
console.error('提交错误:', error);
20-
return NextResponse.json({ error: '内部服务器错误' }, { status: 500 });
23+
return NextResponse.json({ error: 'An error occurred while submitting data.' }, { status: 500 });
2124
}
2225
}

app/programs/[name]/[version]/page.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ import SecurityAdvisories from '../../../../components/SecurityAdvisories';
1212
import BenchmarkResults from '../../../../components/BenchmarkResults';
1313
import VersionsSelector from '../../../../components/VersionsSelector';
1414
import { CrateInfo } from '@/app/lib/crate_info';
15-
1615
//异步获取依赖树
1716
async function fetchDependencyTree(name: string, version: string) {
1817
const response = await fetch(`/api/crates/${name}/${version}`);
1918
const versionData = await response.json();
2019

2120
const dependencies = versionData.dependencies || [];
22-
2321
const dependenciesDetails = await Promise.all(dependencies.map(async (subDep: { name: string; version: string; }) => {
2422
return fetchDependencyTree(subDep.name, subDep.version);
2523
}));

app/ui/programs/nav-links.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,17 @@ const links = [
2525
// { name: 'Customers', href: '/dashboard/customers', icon: UserGroupIcon },
2626
];
2727

28-
export default function NavLinks() {
28+
const NavLinks: React.FC = () => {
2929
const pathname = usePathname();
3030
const [isModalOpen, setModalOpen] = useState(false);
3131
const [inputValue, setInputValue] = useState('');
32-
3332
const [file, setFile] = useState<File | null>(null);
3433
const [isGithubLink, setIsGithubLink] = useState(true); // 控制输入类型
34+
const [messageApi, contextHolder] = message.useMessage();//antd-message的hooks调用
3535

3636
//暂定上传数据类型为react表单类型
3737
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
3838
e.preventDefault();
39-
4039
const formData = new FormData();
4140
if (isGithubLink) {
4241
formData.append('githubLink', inputValue);
@@ -46,31 +45,33 @@ export default function NavLinks() {
4645

4746
try {
4847
//用fetch向服务器发声POST请求,提交用户输入的内容
49-
const response = await fetch('/api/submi', { // 待替换为服务器API
48+
const response = await fetch('/api/submit', { //待替换为后端服务API
5049
method: 'POST',
5150
//请求体
5251
body: formData,
5352
});
53+
//console.log('Response:', response); // 输出响应
54+
5455
//响应处理,根据响应结果显示提示信息,并重置输入框或关闭弹窗
5556
if (response.ok) {
56-
message.success('提交成功');//提交成功后重置输入框的值,并关闭弹窗
57+
messageApi.success('提交成功');//提交成功后重置输入框的值,并关闭弹窗
5758
console.log('提交成功');
5859
setInputValue('');
5960
setFile(null);
6061
setModalOpen(false);
6162
} else {
62-
message.warning('提交失败');
63-
console.log('提交失败');
63+
messageApi.error('提交失败,请重试', 2);
6464
}
6565
} catch (error) {
66-
message.error('提交失败,请检查网络设置');
67-
console.log('提交失败,请检查网络连接。');
66+
messageApi.error('提交失败,请检查网络连接');
67+
//console.log('提交失败,请检查网络连接。', error);
6868
}
6969
};
7070

7171
//渲染部分
7272
return (
7373
<>
74+
{contextHolder}
7475
{links.map((link) => {
7576
const LinkIcon = link.icon;
7677
return (
@@ -189,4 +190,6 @@ export default function NavLinks() {
189190
)}
190191
</>
191192
);
192-
}
193+
}
194+
195+
export default NavLinks;

components/MessageContext.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export const MessageProvider: React.FC<MessageProviderProps> = ({ children }) =>
4141
// 自定义 hook 使用上下文
4242
export const useMessage = () => {
4343
const context = useContext(MessageContext);
44-
44+
if (!context) {
45+
throw new Error('useMessage must be used within a MessageProvider');
46+
}
4547
return context;
4648
};

0 commit comments

Comments
 (0)