refactor: 后台统一使用业务提供项管理
This commit is contained in:
1 parent
40ea7d245b
commit
dd12fd170e
1 file changed
+2
-119
@@ -10,7 +10,6 @@ import {
|
||||
Tag,
|
||||
Typography,
|
||||
message,
|
||||
Divider,
|
||||
} from "antd";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import type { Offering } from "@personal-brand/api-client";
|
||||
@@ -22,16 +21,9 @@ import { MutationError, QueryState } from "../../components/QueryState.js";
|
||||
export function ServicesPage() {
|
||||
const client = createApiClient();
|
||||
const queryClient = useQueryClient();
|
||||
const [editing, setEditing] = useState<Offering | null>(null);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [form] = Form.useForm();
|
||||
const [offeringForm] = Form.useForm();
|
||||
const [offeringOpen, setOfferingOpen] = useState(false);
|
||||
const [editingOffering, setEditingOffering] = useState<Offering | null>(null);
|
||||
const query = useQuery({
|
||||
queryKey: ["admin-offerings-compat"],
|
||||
queryFn: () => client.listAdminOfferings(),
|
||||
});
|
||||
const offerings = useQuery({
|
||||
queryKey: ["admin-offerings"],
|
||||
queryFn: () => client.listAdminOfferings(),
|
||||
@@ -49,34 +41,6 @@ export function ServicesPage() {
|
||||
void queryClient.invalidateQueries({ queryKey: ["admin-offerings"] });
|
||||
},
|
||||
});
|
||||
const save = useMutation({
|
||||
mutationFn: (values: Record<string, unknown>) =>
|
||||
editing
|
||||
? client.updateAdminOffering(editing.id, values)
|
||||
: client.createAdminOffering(values as Omit<Offering, "id" | "brandId">),
|
||||
onSuccess: () => {
|
||||
message.success("服务已保存");
|
||||
setOpen(false);
|
||||
setEditing(null);
|
||||
form.resetFields();
|
||||
void queryClient.invalidateQueries({ queryKey: ["admin-offerings-compat"] });
|
||||
},
|
||||
});
|
||||
const toggle = useMutation({
|
||||
mutationFn: (item: Offering) =>
|
||||
client.updateAdminOffering(item.id, { isEnabled: !item.isEnabled }),
|
||||
onSuccess: () => {
|
||||
message.success("服务状态已更新");
|
||||
void queryClient.invalidateQueries({ queryKey: ["admin-offerings-compat"] });
|
||||
},
|
||||
});
|
||||
const begin = (item?: Offering) => {
|
||||
setEditing(item ?? null);
|
||||
setOpen(true);
|
||||
form.setFieldsValue(
|
||||
item ?? { categoryKey: "standard", bookingEnabled: true, isEnabled: true, sortOrder: 0 },
|
||||
);
|
||||
};
|
||||
const beginOffering = (item?: Offering) => {
|
||||
setEditingOffering(item ?? null);
|
||||
setOfferingOpen(true);
|
||||
@@ -94,10 +58,9 @@ export function ServicesPage() {
|
||||
};
|
||||
return (
|
||||
<QueryState
|
||||
loading={query.isLoading || offerings.isLoading}
|
||||
error={query.error ?? offerings.error}
|
||||
loading={offerings.isLoading}
|
||||
error={offerings.error}
|
||||
onRetry={() => {
|
||||
void query.refetch();
|
||||
void offerings.refetch();
|
||||
}}
|
||||
>
|
||||
@@ -154,38 +117,7 @@ export function ServicesPage() {
|
||||
还没有公开业务提供项。创建后,小程序服务页会优先读取这些内容。
|
||||
</Typography.Paragraph>
|
||||
)}
|
||||
<Divider>兼容旧版服务</Divider>
|
||||
<Table
|
||||
rowKey="id"
|
||||
size="small"
|
||||
dataSource={query.data ?? []}
|
||||
pagination={false}
|
||||
columns={[
|
||||
{ title: "名称", dataIndex: "name" },
|
||||
{ title: "分类", dataIndex: "categoryKey" },
|
||||
{
|
||||
title: "状态",
|
||||
dataIndex: "isEnabled",
|
||||
render: (value) => (value ? "已公开" : "已下架"),
|
||||
},
|
||||
{
|
||||
title: "操作",
|
||||
render: (_: unknown, item: Offering) => (
|
||||
<Space>
|
||||
<Button size="small" onClick={() => begin(item)}>
|
||||
编辑
|
||||
</Button>
|
||||
<Button size="small" onClick={() => toggle.mutate(item)}>
|
||||
{item.isEnabled ? "下架" : "上架"}
|
||||
</Button>
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
]}
|
||||
/>
|
||||
<MutationError error={saveOffering.error} />
|
||||
<MutationError error={save.error} />
|
||||
<MutationError error={toggle.error} />
|
||||
</Card>
|
||||
<Modal
|
||||
open={offeringOpen}
|
||||
@@ -313,55 +245,6 @@ export function ServicesPage() {
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
<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 as Record<string, unknown>)}
|
||||
>
|
||||
<Form.Item name="name" label="服务名称" rules={[{ required: true }]}>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.Item name="description" label="服务描述" rules={[{ required: true }]}>
|
||||
<Input.TextArea rows={4} />
|
||||
</Form.Item>
|
||||
<Form.Item name="categoryKey" label="服务分类" rules={[{ required: true }]}>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.Item name="priceText" label="价格展示">
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.Item name="sortOrder" label="排序">
|
||||
<Input type="number" />
|
||||
</Form.Item>
|
||||
<Form.Item name="bookingEnabled" label="允许提交需求">
|
||||
<Select
|
||||
options={[
|
||||
{ value: true, label: "允许" },
|
||||
{ value: false, label: "暂不接受" },
|
||||
]}
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item name="isEnabled" label="公开状态">
|
||||
<Select
|
||||
options={[
|
||||
{ value: true, label: "已上架" },
|
||||
{ value: false, label: "已下架" },
|
||||
]}
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</QueryState>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user