refactor: 拆分后台信任内容模块
This commit is contained in:
1 parent
bff6e4e9d6
commit
54224d4110
2 files changed
+141
-1
No files matched your search
@@ -0,0 +1,139 @@
|
||||
import {
|
||||
Button,
|
||||
Card,
|
||||
Form,
|
||||
Input,
|
||||
Modal,
|
||||
Popconfirm,
|
||||
Select,
|
||||
Space,
|
||||
Table,
|
||||
Tag,
|
||||
message,
|
||||
} from "antd";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import type { TrustContent, TrustContentInput } from "@personal-brand/api-client";
|
||||
import { useState } from "react";
|
||||
import { createApiClient } from "../../lib/api.js";
|
||||
|
||||
const types = ["profile", "experience", "credential", "portfolio", "process", "testimonial"];
|
||||
export function TrustContentPage() {
|
||||
const client = createApiClient();
|
||||
const queryClient = useQueryClient();
|
||||
const [editing, setEditing] = useState<TrustContent | null>(null);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [form] = Form.useForm<TrustContentInput>();
|
||||
const query = useQuery({
|
||||
queryKey: ["trust-content"],
|
||||
queryFn: () => client.listAdminTrustContent(),
|
||||
});
|
||||
const save = useMutation({
|
||||
mutationFn: (values: TrustContentInput) =>
|
||||
editing
|
||||
? client.updateAdminTrustContent(editing.id, values)
|
||||
: client.createAdminTrustContent(values),
|
||||
onSuccess: () => {
|
||||
message.success("信任内容已保存");
|
||||
setOpen(false);
|
||||
setEditing(null);
|
||||
form.resetFields();
|
||||
void queryClient.invalidateQueries({ queryKey: ["trust-content"] });
|
||||
},
|
||||
});
|
||||
const remove = useMutation({
|
||||
mutationFn: (id: string) => client.deleteAdminTrustContent(id),
|
||||
onSuccess: () => {
|
||||
message.success("内容已删除");
|
||||
void queryClient.invalidateQueries({ queryKey: ["trust-content"] });
|
||||
},
|
||||
});
|
||||
const begin = (item?: TrustContent) => {
|
||||
setEditing(item ?? null);
|
||||
setOpen(true);
|
||||
form.setFieldsValue(
|
||||
item
|
||||
? {
|
||||
type: item.type,
|
||||
title: item.title,
|
||||
summary: item.summary ?? undefined,
|
||||
content: item.content,
|
||||
isPublished: item.isPublished,
|
||||
}
|
||||
: { type: "profile", isPublished: true },
|
||||
);
|
||||
};
|
||||
return (
|
||||
<Card
|
||||
title="信任内容"
|
||||
extra={
|
||||
<Button type="primary" onClick={() => begin()}>
|
||||
新增内容
|
||||
</Button>
|
||||
}
|
||||
>
|
||||
<Table
|
||||
rowKey="id"
|
||||
loading={query.isLoading}
|
||||
dataSource={query.data ?? []}
|
||||
columns={[
|
||||
{ title: "类型", dataIndex: "type", render: (value) => <Tag>{value}</Tag> },
|
||||
{ title: "标题", dataIndex: "title" },
|
||||
{
|
||||
title: "状态",
|
||||
dataIndex: "isPublished",
|
||||
render: (value) => (value ? "已发布" : "草稿"),
|
||||
},
|
||||
{
|
||||
title: "操作",
|
||||
render: (_: unknown, item: TrustContent) => (
|
||||
<Space>
|
||||
<Button size="small" onClick={() => begin(item)}>
|
||||
编辑
|
||||
</Button>
|
||||
<Popconfirm title="确认删除?" onConfirm={() => remove.mutate(item.id)}>
|
||||
<Button size="small" danger>
|
||||
删除
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
]}
|
||||
/>
|
||||
<Modal
|
||||
open={open}
|
||||
title={editing ? "编辑信任内容" : "新增信任内容"}
|
||||
onCancel={() => {
|
||||
setOpen(false);
|
||||
setEditing(null);
|
||||
form.resetFields();
|
||||
}}
|
||||
onOk={() => form.submit()}
|
||||
confirmLoading={save.isPending}
|
||||
>
|
||||
<Form form={form} layout="vertical" onFinish={(values) => save.mutate(values)}>
|
||||
<Form.Item name="type" label="内容类型" rules={[{ required: true }]}>
|
||||
<Select options={types.map((value) => ({ value, label: value }))} />
|
||||
</Form.Item>
|
||||
<Form.Item name="title" label="标题" rules={[{ required: true }]}>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.Item name="summary" label="摘要">
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.Item name="content" label="内容" rules={[{ required: true }]}>
|
||||
<Input.TextArea rows={7} />
|
||||
</Form.Item>
|
||||
<Form.Item name="isPublished" label="发布状态">
|
||||
<Select
|
||||
options={[
|
||||
{ value: true, label: "已发布" },
|
||||
{ value: false, label: "草稿" },
|
||||
]}
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
} from "antd";
|
||||
import { createApiClient } from "./lib/api.js";
|
||||
import { InquiriesPage as FeatureInquiriesPage } from "./features/inquiries/InquiriesPage.js";
|
||||
import { TrustContentPage as FeatureTrustContentPage } from "./features/trust-content/TrustContentPage.js";
|
||||
import { BrowserRouter, Navigate, Route, Routes, useLocation, useNavigate } from "react-router-dom";
|
||||
import type {
|
||||
BrandProfile,
|
||||
@@ -543,7 +544,7 @@ function Dashboard({ onLogout }: { onLogout: () => void }) {
|
||||
<Layout.Content style={{ padding: 24 }}>
|
||||
<Routes>
|
||||
<Route path="brand" element={<BrandPage />} />
|
||||
<Route path="trust" element={<TrustContentPage />} />
|
||||
<Route path="trust" element={<FeatureTrustContentPage />} />
|
||||
<Route path="services" element={<ServicesPage />} />
|
||||
<Route path="inquiries" element={<FeatureInquiriesPage />} />
|
||||
<Route path="*" element={<Navigate to="/admin/brand" replace />} />
|
||||
|
||||
Reference in new issue
Block a user