207 lines
8.3 KiB
TypeScript
207 lines
8.3 KiB
TypeScript
import { Alert, Button, Col, Form, Input, Radio, Row, Space } from "antd";
|
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
|
import { useNavigate } from "react-router-dom";
|
|
import type {
|
|
BrandCreationOptionBusiness,
|
|
BrandCreationOptionPageScheme,
|
|
CreateBrandInput,
|
|
} from "@personal-brand/api-client";
|
|
import { createApiClient } from "../../lib/api.js";
|
|
import { AdminPageHeader } from "../../components/AdminPageHeader.js";
|
|
import { AdminSection } from "../../components/AdminSection.js";
|
|
import { EmptyState } from "../../components/EmptyState.js";
|
|
import { MutationError, QueryState } from "../../components/QueryState.js";
|
|
import { StatusTag } from "../../components/StatusTag.js";
|
|
|
|
const client = createApiClient();
|
|
|
|
function compatibleSchemes(
|
|
schemes: BrandCreationOptionPageScheme[],
|
|
businessType?: CreateBrandInput["businessType"],
|
|
) {
|
|
return businessType
|
|
? schemes.filter((scheme) => scheme.supportedBusinessTypes.includes(businessType))
|
|
: [];
|
|
}
|
|
|
|
export function CreateBrandPage() {
|
|
const navigate = useNavigate();
|
|
const [form] = Form.useForm<CreateBrandInput>();
|
|
const businessType = Form.useWatch("businessType", form);
|
|
const selectedSchemeId = Form.useWatch("templatePackageVersionId", form);
|
|
const options = useQuery({
|
|
queryKey: ["admin-brand-creation-options"],
|
|
queryFn: () => client.getAdminBrandCreationOptions(),
|
|
});
|
|
const create = useMutation({
|
|
mutationFn: (values: CreateBrandInput) => client.createAdminBrand(values),
|
|
onSuccess: () => {
|
|
// 创建接口会切换品牌会话;完整导航确保所有查询重新绑定到新品牌。
|
|
window.location.assign("/admin/overview");
|
|
},
|
|
});
|
|
const schemes = compatibleSchemes(options.data?.pageSchemes ?? [], businessType);
|
|
|
|
return (
|
|
<QueryState
|
|
loading={options.isLoading}
|
|
error={options.error}
|
|
onRetry={() => void options.refetch()}
|
|
>
|
|
<AdminPageHeader
|
|
title="创建品牌"
|
|
description="先确定业务类型,再选择与该业务兼容的页面方案。业务类型创建后不可更改。"
|
|
/>
|
|
<Form
|
|
form={form}
|
|
layout="vertical"
|
|
onFinish={(values) => create.mutate(values)}
|
|
onValuesChange={(changed: Partial<CreateBrandInput>) => {
|
|
if (!changed.businessType) return;
|
|
const nextSchemes = compatibleSchemes(
|
|
options.data?.pageSchemes ?? [],
|
|
changed.businessType,
|
|
);
|
|
const selected = form.getFieldValue("templatePackageVersionId") as unknown;
|
|
if (!nextSchemes.some((scheme) => scheme.templatePackageVersionId === selected)) {
|
|
form.setFieldValue("templatePackageVersionId", undefined);
|
|
}
|
|
}}
|
|
>
|
|
<div className="admin-stack">
|
|
<AdminSection title="品牌身份" description="这些信息会展示给访客,之后仍可继续完善。">
|
|
<Row gutter={16}>
|
|
<Col xs={24} md={12}>
|
|
<Form.Item
|
|
name="name"
|
|
label="品牌名称"
|
|
rules={[{ required: true, message: "请输入品牌名称" }]}
|
|
>
|
|
<Input size="large" maxLength={80} placeholder="例如:林境建筑" />
|
|
</Form.Item>
|
|
</Col>
|
|
<Col xs={24} md={12}>
|
|
<Form.Item
|
|
name="slug"
|
|
label="小程序地址标识"
|
|
help="用于生成和识别公开品牌地址,只能使用小写字母、数字和连字符。"
|
|
rules={[
|
|
{ required: true, message: "请输入小程序地址标识" },
|
|
{
|
|
pattern: /^[a-z0-9]+(?:-[a-z0-9]+)*$/,
|
|
message: "只能使用小写字母、数字和连字符",
|
|
},
|
|
]}
|
|
>
|
|
<Input size="large" maxLength={80} placeholder="例如:linjing-architecture" />
|
|
</Form.Item>
|
|
</Col>
|
|
</Row>
|
|
</AdminSection>
|
|
|
|
<AdminSection
|
|
title="业务类型"
|
|
description="业务类型决定品牌需要填写哪些内容。不同业务类型的字段和能力不同,创建后不能修改。"
|
|
>
|
|
<Form.Item name="businessType" rules={[{ required: true, message: "请选择业务类型" }]}>
|
|
<Radio.Group className="admin-stack">
|
|
<Row gutter={[12, 12]}>
|
|
{(options.data?.businesses ?? []).map((business: BrandCreationOptionBusiness) => (
|
|
<Col key={business.key} xs={24} lg={8}>
|
|
<Radio.Button value={business.key} className="admin-choice-card">
|
|
<strong>{business.name}</strong>
|
|
<span>{business.description}</span>
|
|
</Radio.Button>
|
|
</Col>
|
|
))}
|
|
</Row>
|
|
</Radio.Group>
|
|
</Form.Item>
|
|
{businessType ? (
|
|
schemes.length ? null : (
|
|
<Alert
|
|
type="warning"
|
|
showIcon
|
|
title="当前业务类型还没有可用的页面方案"
|
|
description="请先联系平台管理员发布兼容的页面方案。"
|
|
/>
|
|
)
|
|
) : (
|
|
<Alert type="info" showIcon title="选择业务类型后,将只显示兼容的页面方案" />
|
|
)}
|
|
</AdminSection>
|
|
|
|
<AdminSection
|
|
title="页面方案"
|
|
description="页面方案决定页面结构、视觉和资源位。版本可在创建后继续升级或切换。"
|
|
>
|
|
{schemes.length ? (
|
|
<Form.Item
|
|
name="templatePackageVersionId"
|
|
rules={[{ required: true, message: "请选择页面方案" }]}
|
|
>
|
|
<Radio.Group className="admin-stack">
|
|
<Space orientation="vertical" size={12} className="admin-full-width">
|
|
{schemes.map((scheme) => (
|
|
<Radio.Button
|
|
key={scheme.templatePackageVersionId}
|
|
value={scheme.templatePackageVersionId}
|
|
className="admin-choice-card admin-choice-card--wide"
|
|
>
|
|
<span className="admin-row-line">
|
|
<strong>{scheme.name}</strong>
|
|
<StatusTag
|
|
tone={scheme.isLatestForPackage ? "success" : "neutral"}
|
|
label={
|
|
scheme.isLatestForPackage
|
|
? `最新版 ${scheme.version}`
|
|
: `版本 ${scheme.version}`
|
|
}
|
|
/>
|
|
</span>
|
|
<span>{scheme.description}</span>
|
|
</Radio.Button>
|
|
))}
|
|
</Space>
|
|
</Radio.Group>
|
|
</Form.Item>
|
|
) : (
|
|
<EmptyState
|
|
title={businessType ? "没有兼容的页面方案" : "先选择业务类型"}
|
|
description={
|
|
businessType
|
|
? "平台尚未发布适配该业务类型的页面方案。"
|
|
: "选择业务类型后,这里会列出可用的页面结构和视觉方案。"
|
|
}
|
|
/>
|
|
)}
|
|
</AdminSection>
|
|
|
|
<Alert
|
|
type="info"
|
|
showIcon
|
|
title="创建品牌不会自动导入示例内容"
|
|
description="新品牌会从空白内容开始,避免把示例品牌、案例或联系方式误当成真实信息发布。"
|
|
/>
|
|
<MutationError error={create.error} />
|
|
<Space>
|
|
<Button onClick={() => navigate("/admin/brands")}>取消</Button>
|
|
<Button
|
|
type="primary"
|
|
htmlType="submit"
|
|
size="large"
|
|
loading={create.isPending}
|
|
disabled={
|
|
!businessType ||
|
|
!schemes.some((scheme) => scheme.templatePackageVersionId === selectedSchemeId)
|
|
}
|
|
>
|
|
创建并进入品牌
|
|
</Button>
|
|
</Space>
|
|
</div>
|
|
</Form>
|
|
</QueryState>
|
|
);
|
|
}
|