276 lines
10 KiB
TypeScript
276 lines
10 KiB
TypeScript
import {
|
|
Button,
|
|
Card,
|
|
Form,
|
|
Input,
|
|
Modal,
|
|
Select,
|
|
Space,
|
|
Table,
|
|
Tag,
|
|
Typography,
|
|
message,
|
|
} from "antd";
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import type { Offering } from "@personal-brand/api-client";
|
|
import { useState } from "react";
|
|
import { createApiClient } from "../../lib/api.js";
|
|
import { AdminPageHeader } from "../../components/AdminPageHeader.js";
|
|
import { MutationError, QueryState } from "../../components/QueryState.js";
|
|
|
|
export function ServicesPage() {
|
|
const client = createApiClient();
|
|
const queryClient = useQueryClient();
|
|
const [offeringForm] = Form.useForm();
|
|
const [offeringOpen, setOfferingOpen] = useState(false);
|
|
const [editingOffering, setEditingOffering] = useState<Offering | null>(null);
|
|
const offerings = useQuery({
|
|
queryKey: ["admin-offerings"],
|
|
queryFn: () => client.listAdminOfferings(),
|
|
});
|
|
const saveOffering = useMutation({
|
|
mutationFn: (values: Record<string, unknown>) =>
|
|
editingOffering
|
|
? client.updateAdminOffering(editingOffering.id, values)
|
|
: client.createAdminOffering(values as Omit<Offering, "id" | "brandId">),
|
|
onSuccess: () => {
|
|
message.success("业务提供项已保存");
|
|
setOfferingOpen(false);
|
|
setEditingOffering(null);
|
|
offeringForm.resetFields();
|
|
void queryClient.invalidateQueries({ queryKey: ["admin-offerings"] });
|
|
},
|
|
});
|
|
const deleteOffering = useMutation({
|
|
mutationFn: (id: string) => client.deleteAdminOffering(id),
|
|
onSuccess: () => {
|
|
message.success("业务提供项已删除");
|
|
void queryClient.invalidateQueries({ queryKey: ["admin-offerings"] });
|
|
},
|
|
});
|
|
const toggleOffering = useMutation({
|
|
mutationFn: ({ id, isEnabled }: { id: string; isEnabled: boolean }) =>
|
|
client.updateAdminOffering(id, { isEnabled }),
|
|
onSuccess: () => void queryClient.invalidateQueries({ queryKey: ["admin-offerings"] }),
|
|
});
|
|
const beginOffering = (item?: Offering) => {
|
|
setEditingOffering(item ?? null);
|
|
setOfferingOpen(true);
|
|
const config = item?.config ?? {};
|
|
offeringForm.setFieldsValue(
|
|
item
|
|
? {
|
|
...item,
|
|
sections: config.sections ?? [],
|
|
media: config.media ?? [],
|
|
cta: config.cta ?? {},
|
|
}
|
|
: { kind: "service", isEnabled: true, sortOrder: 0, sections: [], media: [], cta: {} },
|
|
);
|
|
};
|
|
return (
|
|
<QueryState
|
|
loading={offerings.isLoading}
|
|
error={offerings.error}
|
|
onRetry={() => {
|
|
void offerings.refetch();
|
|
}}
|
|
>
|
|
<AdminPageHeader
|
|
title="业务提供项"
|
|
description="管理你向外提供的服务、产品、套餐和合作项目,让访客清楚知道如何与你合作。"
|
|
extra={
|
|
<Button type="primary" onClick={() => beginOffering()}>
|
|
新增业务提供项
|
|
</Button>
|
|
}
|
|
/>
|
|
<Card title="公开业务提供项" extra={<Tag color="blue">小程序优先展示</Tag>}>
|
|
<Table
|
|
rowKey="id"
|
|
loading={offerings.isLoading}
|
|
dataSource={offerings.data ?? []}
|
|
columns={[
|
|
{ title: "名称", dataIndex: "name" },
|
|
{
|
|
title: "业务类型",
|
|
dataIndex: "kind",
|
|
render: (value) => (
|
|
<Tag>
|
|
{(
|
|
{
|
|
service: "服务",
|
|
product: "定制产品",
|
|
package: "套餐",
|
|
subscription: "订阅",
|
|
} as Record<string, string>
|
|
)[String(value)] ?? String(value)}
|
|
</Tag>
|
|
),
|
|
},
|
|
{ title: "价格", dataIndex: "priceText" },
|
|
{
|
|
title: "状态",
|
|
dataIndex: "isEnabled",
|
|
render: (value) => (value ? "已启用" : "已停用"),
|
|
},
|
|
{
|
|
title: "操作",
|
|
render: (_: unknown, item: Offering) => (
|
|
<Space>
|
|
<Button size="small" onClick={() => beginOffering(item)}>
|
|
编辑详情
|
|
</Button>
|
|
<Button
|
|
size="small"
|
|
onClick={() =>
|
|
toggleOffering.mutate({ id: item.id, isEnabled: !item.isEnabled })
|
|
}
|
|
>
|
|
{item.isEnabled ? "禁用" : "启用"}
|
|
</Button>
|
|
<Button danger size="small" onClick={() => deleteOffering.mutate(item.id)}>
|
|
删除
|
|
</Button>
|
|
</Space>
|
|
),
|
|
},
|
|
]}
|
|
/>
|
|
{offerings.data?.length === 0 && (
|
|
<Typography.Paragraph type="secondary">
|
|
还没有公开业务提供项。创建后,小程序服务页会优先读取这些内容。
|
|
</Typography.Paragraph>
|
|
)}
|
|
<MutationError error={saveOffering.error} />
|
|
</Card>
|
|
<Modal
|
|
open={offeringOpen}
|
|
title={editingOffering ? "编辑业务提供项" : "新增业务提供项"}
|
|
onCancel={() => {
|
|
setOfferingOpen(false);
|
|
setEditingOffering(null);
|
|
offeringForm.resetFields();
|
|
}}
|
|
onOk={() => offeringForm.submit()}
|
|
confirmLoading={saveOffering.isPending}
|
|
>
|
|
<Form
|
|
form={offeringForm}
|
|
layout="vertical"
|
|
onFinish={(values) => saveOffering.mutate(values as Record<string, unknown>)}
|
|
>
|
|
<Form.Item name="kind" label="类型" rules={[{ required: true }]}>
|
|
<Select
|
|
options={[
|
|
{ value: "service", label: "服务" },
|
|
{ value: "product", label: "定制产品" },
|
|
{ value: "package", label: "套餐" },
|
|
{ value: "subscription", label: "订阅" },
|
|
]}
|
|
/>
|
|
</Form.Item>
|
|
<Form.Item name="name" label="名称" rules={[{ required: true }]}>
|
|
<Input />
|
|
</Form.Item>
|
|
<Form.Item name="summary" label="一句话说明">
|
|
<Input />
|
|
</Form.Item>
|
|
<Form.Item name="description" label="详细介绍">
|
|
<Input.TextArea rows={5} />
|
|
</Form.Item>
|
|
<Form.Item name="priceText" label="价格展示">
|
|
<Input />
|
|
</Form.Item>
|
|
<Form.Item name="coverUrl" label="封面图片 URL">
|
|
<Input />
|
|
</Form.Item>
|
|
<Form.List name="sections">
|
|
{(fields, { add, remove }) => (
|
|
<>
|
|
<Form.Item label="内容分区">
|
|
<Button onClick={() => add({ type: "text" })}>新增分区</Button>
|
|
</Form.Item>
|
|
{fields.map(({ key, name, ...restField }) => (
|
|
<Space key={key} align="start" style={{ display: "flex", marginBottom: 8 }}>
|
|
<Form.Item {...restField} name={[name, "type"]}>
|
|
<Select
|
|
style={{ width: 120 }}
|
|
options={[
|
|
{ value: "text", label: "文本" },
|
|
{ value: "image-list", label: "图片列表" },
|
|
{ value: "timeline", label: "时间线" },
|
|
{ value: "faq", label: "常见问题" },
|
|
]}
|
|
/>
|
|
</Form.Item>
|
|
<Form.Item {...restField} name={[name, "title"]}>
|
|
<Input placeholder="分区标题" />
|
|
</Form.Item>
|
|
<Form.Item {...restField} name={[name, "content"]}>
|
|
<Input placeholder="分区内容" />
|
|
</Form.Item>
|
|
<Button danger onClick={() => remove(name)}>
|
|
删除
|
|
</Button>
|
|
</Space>
|
|
))}
|
|
</>
|
|
)}
|
|
</Form.List>
|
|
<Form.List name="media">
|
|
{(fields, { add, remove }) => (
|
|
<>
|
|
<Form.Item label="媒体列表">
|
|
<Button onClick={() => add({ type: "image" })}>新增媒体</Button>
|
|
</Form.Item>
|
|
{fields.map(({ key, name, ...restField }) => (
|
|
<Space key={key} align="start" style={{ display: "flex", marginBottom: 8 }}>
|
|
<Form.Item {...restField} name={[name, "url"]}>
|
|
<Input placeholder="图片 URL" />
|
|
</Form.Item>
|
|
<Form.Item {...restField} name={[name, "alt"]}>
|
|
<Input placeholder="替代文本" />
|
|
</Form.Item>
|
|
<Button danger onClick={() => remove(name)}>
|
|
删除
|
|
</Button>
|
|
</Space>
|
|
))}
|
|
</>
|
|
)}
|
|
</Form.List>
|
|
<Form.Item label="行动按钮">
|
|
<Space>
|
|
<Form.Item name={["cta", "type"]} noStyle>
|
|
<Select
|
|
style={{ width: 150 }}
|
|
allowClear
|
|
placeholder="按钮行为"
|
|
options={[
|
|
{ value: "inquiry", label: "提交咨询" },
|
|
{ value: "booking", label: "预约服务" },
|
|
{ value: "contact", label: "联系品牌" },
|
|
{ value: "external-link", label: "外部链接" },
|
|
]}
|
|
/>
|
|
</Form.Item>
|
|
<Form.Item name={["cta", "label"]} noStyle>
|
|
<Input placeholder="按钮文案" />
|
|
</Form.Item>
|
|
</Space>
|
|
</Form.Item>
|
|
<Form.Item name="isEnabled" label="公开状态">
|
|
<Select
|
|
options={[
|
|
{ value: true, label: "已启用" },
|
|
{ value: false, label: "已停用" },
|
|
]}
|
|
/>
|
|
</Form.Item>
|
|
</Form>
|
|
</Modal>
|
|
</QueryState>
|
|
);
|
|
}
|