336 lines
12 KiB
TypeScript
336 lines
12 KiB
TypeScript
import {
|
|
Button,
|
|
Card,
|
|
Form,
|
|
Input,
|
|
Modal,
|
|
Select,
|
|
Space,
|
|
Table,
|
|
Tag,
|
|
message,
|
|
Divider,
|
|
} from "antd";
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import type { Offering, PublicService } from "@personal-brand/api-client";
|
|
import { useState } from "react";
|
|
import { createApiClient } from "../../lib/api.js";
|
|
|
|
export function ServicesPage() {
|
|
const client = createApiClient();
|
|
const queryClient = useQueryClient();
|
|
const [editing, setEditing] = useState<PublicService | 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-services"],
|
|
queryFn: () => client.listAdminServices(),
|
|
});
|
|
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 save = useMutation({
|
|
mutationFn: (values: Record<string, unknown>) =>
|
|
editing
|
|
? client.updateAdminService(editing.id, values)
|
|
: client.createAdminService(values as { name: string; description: string }),
|
|
onSuccess: () => {
|
|
message.success("服务已保存");
|
|
setOpen(false);
|
|
setEditing(null);
|
|
form.resetFields();
|
|
void queryClient.invalidateQueries({ queryKey: ["admin-services"] });
|
|
},
|
|
});
|
|
const toggle = useMutation({
|
|
mutationFn: (item: PublicService) =>
|
|
client.updateAdminService(item.id, { isEnabled: !item.isEnabled }),
|
|
onSuccess: () => {
|
|
message.success("服务状态已更新");
|
|
void queryClient.invalidateQueries({ queryKey: ["admin-services"] });
|
|
},
|
|
});
|
|
const begin = (item?: PublicService) => {
|
|
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);
|
|
const config = (item?.config ?? {}) as Record<string, unknown>;
|
|
offeringForm.setFieldsValue(
|
|
item
|
|
? {
|
|
...item,
|
|
sections: config.sections ?? [],
|
|
media: config.media ?? [],
|
|
cta: config.cta ?? {},
|
|
}
|
|
: { kind: "service", isEnabled: true, sortOrder: 0, sections: [], media: [], cta: {} },
|
|
);
|
|
};
|
|
return (
|
|
<Card
|
|
title="服务与合作"
|
|
extra={
|
|
<Button type="primary" onClick={() => begin()}>
|
|
新增服务
|
|
</Button>
|
|
}
|
|
>
|
|
<Table
|
|
rowKey="id"
|
|
loading={query.isLoading}
|
|
dataSource={query.data ?? []}
|
|
columns={[
|
|
{ title: "服务名称", dataIndex: "name" },
|
|
{ title: "分类", dataIndex: "categoryKey", render: (value) => <Tag>{value}</Tag> },
|
|
{ title: "价格展示", dataIndex: "priceText" },
|
|
{
|
|
title: "状态",
|
|
dataIndex: "isEnabled",
|
|
render: (value) => (value ? "已上架" : "已下架"),
|
|
},
|
|
{
|
|
title: "操作",
|
|
render: (_: unknown, item: PublicService) => (
|
|
<Space>
|
|
<Button size="small" onClick={() => begin(item)}>
|
|
编辑
|
|
</Button>
|
|
<Button size="small" onClick={() => toggle.mutate(item)}>
|
|
{item.isEnabled ? "下架" : "上架"}
|
|
</Button>
|
|
</Space>
|
|
),
|
|
},
|
|
]}
|
|
/>
|
|
<Divider orientation="left">通用业务提供项</Divider>
|
|
<Card
|
|
size="small"
|
|
extra={
|
|
<Button type="primary" onClick={() => beginOffering()}>
|
|
新增提供项
|
|
</Button>
|
|
}
|
|
>
|
|
<Table
|
|
rowKey="id"
|
|
loading={offerings.isLoading}
|
|
dataSource={offerings.data ?? []}
|
|
columns={[
|
|
{ title: "名称", dataIndex: "name" },
|
|
{ title: "类型", dataIndex: "kind", render: (value) => <Tag>{value}</Tag> },
|
|
{ title: "价格", dataIndex: "priceText" },
|
|
{
|
|
title: "状态",
|
|
dataIndex: "isEnabled",
|
|
render: (value) => (value ? "已启用" : "已停用"),
|
|
},
|
|
{
|
|
title: "操作",
|
|
render: (_: unknown, item: Offering) => (
|
|
<Button size="small" onClick={() => beginOffering(item)}>
|
|
编辑详情
|
|
</Button>
|
|
),
|
|
},
|
|
]}
|
|
/>
|
|
</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)}
|
|
>
|
|
<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>
|
|
<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="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>
|
|
</Card>
|
|
);
|
|
}
|