refactor: 拆分后台服务管理模块
This commit is contained in:
1 parent
54224d4110
commit
10a2adbe87
2 files changed
+131
-1
No files matched your search
@@ -0,0 +1,129 @@
|
||||
import { Button, Card, Form, Input, Modal, Select, Space, Table, Tag, message } from "antd";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import type { 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 query = useQuery({
|
||||
queryKey: ["admin-services"],
|
||||
queryFn: () => client.listAdminServices(),
|
||||
});
|
||||
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 },
|
||||
);
|
||||
};
|
||||
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>
|
||||
),
|
||||
},
|
||||
]}
|
||||
/>
|
||||
<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>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user