refactor: 后台统一使用业务提供项管理
This commit is contained in:
1 parent
40ea7d245b
commit
dd12fd170e
1 file changed
+2
-119
@@ -10,7 +10,6 @@ import {
|
|||||||
Tag,
|
Tag,
|
||||||
Typography,
|
Typography,
|
||||||
message,
|
message,
|
||||||
Divider,
|
|
||||||
} from "antd";
|
} from "antd";
|
||||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||||
import type { Offering } from "@personal-brand/api-client";
|
import type { Offering } from "@personal-brand/api-client";
|
||||||
@@ -22,16 +21,9 @@ import { MutationError, QueryState } from "../../components/QueryState.js";
|
|||||||
export function ServicesPage() {
|
export function ServicesPage() {
|
||||||
const client = createApiClient();
|
const client = createApiClient();
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const [editing, setEditing] = useState<Offering | null>(null);
|
|
||||||
const [open, setOpen] = useState(false);
|
|
||||||
const [form] = Form.useForm();
|
|
||||||
const [offeringForm] = Form.useForm();
|
const [offeringForm] = Form.useForm();
|
||||||
const [offeringOpen, setOfferingOpen] = useState(false);
|
const [offeringOpen, setOfferingOpen] = useState(false);
|
||||||
const [editingOffering, setEditingOffering] = useState<Offering | null>(null);
|
const [editingOffering, setEditingOffering] = useState<Offering | null>(null);
|
||||||
const query = useQuery({
|
|
||||||
queryKey: ["admin-offerings-compat"],
|
|
||||||
queryFn: () => client.listAdminOfferings(),
|
|
||||||
});
|
|
||||||
const offerings = useQuery({
|
const offerings = useQuery({
|
||||||
queryKey: ["admin-offerings"],
|
queryKey: ["admin-offerings"],
|
||||||
queryFn: () => client.listAdminOfferings(),
|
queryFn: () => client.listAdminOfferings(),
|
||||||
@@ -49,34 +41,6 @@ export function ServicesPage() {
|
|||||||
void queryClient.invalidateQueries({ queryKey: ["admin-offerings"] });
|
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) => {
|
const beginOffering = (item?: Offering) => {
|
||||||
setEditingOffering(item ?? null);
|
setEditingOffering(item ?? null);
|
||||||
setOfferingOpen(true);
|
setOfferingOpen(true);
|
||||||
@@ -94,10 +58,9 @@ export function ServicesPage() {
|
|||||||
};
|
};
|
||||||
return (
|
return (
|
||||||
<QueryState
|
<QueryState
|
||||||
loading={query.isLoading || offerings.isLoading}
|
loading={offerings.isLoading}
|
||||||
error={query.error ?? offerings.error}
|
error={offerings.error}
|
||||||
onRetry={() => {
|
onRetry={() => {
|
||||||
void query.refetch();
|
|
||||||
void offerings.refetch();
|
void offerings.refetch();
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
@@ -154,38 +117,7 @@ export function ServicesPage() {
|
|||||||
还没有公开业务提供项。创建后,小程序服务页会优先读取这些内容。
|
还没有公开业务提供项。创建后,小程序服务页会优先读取这些内容。
|
||||||
</Typography.Paragraph>
|
</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={saveOffering.error} />
|
||||||
<MutationError error={save.error} />
|
|
||||||
<MutationError error={toggle.error} />
|
|
||||||
</Card>
|
</Card>
|
||||||
<Modal
|
<Modal
|
||||||
open={offeringOpen}
|
open={offeringOpen}
|
||||||
@@ -313,55 +245,6 @@ export function ServicesPage() {
|
|||||||
</Form.Item>
|
</Form.Item>
|
||||||
</Form>
|
</Form>
|
||||||
</Modal>
|
</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>
|
</QueryState>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user