feat: 完成模板区域与咨询字段编辑闭环
This commit is contained in:
1 parent
c5db30f817
commit
66dbba5748
6 files changed
+211
-24
No files matched your search
@@ -0,0 +1,135 @@
|
||||
import { Alert, Button, Form, Input, Select, Space, Switch, Typography, message } from "antd";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useEffect } from "react";
|
||||
import type { RuntimeWorkspace } from "@personal-brand/api-client";
|
||||
import { createApiClient } from "../../lib/api.js";
|
||||
import { MutationError } from "../../components/QueryState.js";
|
||||
|
||||
type RuntimeInquiryForm = RuntimeWorkspace["inquiryForms"][number];
|
||||
|
||||
export function InquiryPreviewEditor({
|
||||
runtime,
|
||||
fieldKey,
|
||||
onDraft,
|
||||
}: {
|
||||
runtime: RuntimeInquiryForm;
|
||||
fieldKey: string;
|
||||
onDraft: (fields: RuntimeInquiryForm["fields"]) => void;
|
||||
}) {
|
||||
const [form] = Form.useForm();
|
||||
const cache = useQueryClient();
|
||||
const query = useQuery({
|
||||
queryKey: ["admin-inquiry-forms"],
|
||||
queryFn: () => createApiClient().listAdminInquiryForms(),
|
||||
});
|
||||
const record = query.data?.find((item) => item.id === runtime.key);
|
||||
const field = runtime.fields.find((item) => item.key === fieldKey);
|
||||
useEffect(() => {
|
||||
if (field) form.setFieldsValue(field);
|
||||
}, [fieldKey, runtime.key, form]);
|
||||
const update = (values: Record<string, unknown>) =>
|
||||
runtime.fields.map((item) =>
|
||||
item.key === fieldKey ? ({ ...item, ...values } as typeof item) : item,
|
||||
);
|
||||
const save = useMutation({
|
||||
mutationFn: async (values: Record<string, unknown>) => {
|
||||
if (!record || !field) throw new Error("表单已不存在,请刷新后重试");
|
||||
const fieldsSnapshot = record.fieldsSnapshot.map((item) => {
|
||||
if (!item || typeof item !== "object" || (item as { key?: string }).key !== fieldKey)
|
||||
return item;
|
||||
const type =
|
||||
values.type === "tel"
|
||||
? "phone"
|
||||
: values.type === "checkbox"
|
||||
? "multi-select"
|
||||
: values.type;
|
||||
return { ...item, ...values, type };
|
||||
});
|
||||
return createApiClient().updateAdminInquiryForm(record.id, { fieldsSnapshot });
|
||||
},
|
||||
onSuccess: () => {
|
||||
message.success("表单字段已保存为草稿");
|
||||
void cache.invalidateQueries({ queryKey: ["admin-inquiry-forms"] });
|
||||
void cache.invalidateQueries({ queryKey: ["admin-preview-workspace"] });
|
||||
void cache.invalidateQueries({ queryKey: ["publication-readiness"] });
|
||||
},
|
||||
});
|
||||
if (query.isError)
|
||||
return (
|
||||
<Alert
|
||||
type="error"
|
||||
message="表单读取失败"
|
||||
action={<Button onClick={() => void query.refetch()}>重试</Button>}
|
||||
/>
|
||||
);
|
||||
if (!record)
|
||||
return (
|
||||
<Alert
|
||||
type="info"
|
||||
message={query.isPending ? "正在读取表单" : "未找到对应表单,请刷新后重试"}
|
||||
/>
|
||||
);
|
||||
if (!field) return <Alert type="warning" message="字段已不存在" />;
|
||||
return (
|
||||
<Form
|
||||
form={form}
|
||||
layout="vertical"
|
||||
initialValues={field}
|
||||
onValuesChange={(_, values) => onDraft(update(values))}
|
||||
onFinish={(values) => save.mutate(values)}
|
||||
>
|
||||
<Typography.Paragraph type="secondary">
|
||||
{record.name} · 编辑字段:{field.label}
|
||||
</Typography.Paragraph>
|
||||
<Form.Item
|
||||
name="label"
|
||||
label="字段标题"
|
||||
rules={[{ required: true, message: "请填写字段标题" }]}
|
||||
>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.Item name="type" label="输入方式">
|
||||
<Select
|
||||
options={[
|
||||
{ value: "text", label: "简短文字" },
|
||||
{ value: "textarea", label: "详细说明" },
|
||||
{ value: "tel", label: "联系电话" },
|
||||
{ value: "number", label: "数字" },
|
||||
{ value: "date", label: "日期" },
|
||||
{ value: "select", label: "单项选择" },
|
||||
{ value: "radio", label: "单选按钮" },
|
||||
{ value: "checkbox", label: "多项选择" },
|
||||
]}
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item name="required" label="必填" valuePropName="checked">
|
||||
<Switch />
|
||||
</Form.Item>
|
||||
<Form.List name="options">
|
||||
{(fields, { add, remove }) => (
|
||||
<div>
|
||||
{fields.length ? <Typography.Title level={5}>选项</Typography.Title> : null}
|
||||
{fields.map(({ key, name }) => (
|
||||
<Space key={key} align="start">
|
||||
<Form.Item name={[name, "label"]} label="选项名称" rules={[{ required: true }]}>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.Item name={[name, "value"]} label="选项标识" rules={[{ required: true }]}>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Button onClick={() => remove(name)}>删除</Button>
|
||||
</Space>
|
||||
))}
|
||||
<Button onClick={() => add({ label: "新选项", value: crypto.randomUUID() })}>
|
||||
添加选项
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</Form.List>
|
||||
<MutationError error={save.error} />
|
||||
<Button style={{ marginTop: 24 }} type="primary" htmlType="submit" loading={save.isPending}>
|
||||
保存表单字段
|
||||
</Button>
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
@@ -34,6 +34,7 @@ import { ImageUrlField } from "../../components/ImageUrlField.js";
|
||||
import { TemplateListField } from "../../components/TemplateListField.js";
|
||||
import { MutationError } from "../../components/QueryState.js";
|
||||
import { TemplatePreview } from "./TemplatePreview.js";
|
||||
import { InquiryPreviewEditor } from "./InquiryPreviewEditor.js";
|
||||
import { resolvePreviewSelection, type PreviewSelection } from "./preview-selection.js";
|
||||
import {
|
||||
applyBrandPreviewValues,
|
||||
@@ -135,6 +136,17 @@ function renderField(field: TemplateAdminField) {
|
||||
);
|
||||
}
|
||||
|
||||
function previewTargetSource(
|
||||
section: TemplateAdminSection,
|
||||
targetKey: string,
|
||||
): "brand" | "offering" | "trust" | "inquiry" {
|
||||
const fields = section.fields.filter((field) => field.previewTarget === targetKey);
|
||||
if (fields.some((field) => field.binding.startsWith("offering."))) return "offering";
|
||||
if (fields.some((field) => field.binding.startsWith("trust."))) return "trust";
|
||||
if (fields.some((field) => field.binding.startsWith("brand."))) return "brand";
|
||||
return section.target;
|
||||
}
|
||||
|
||||
export function PresentationEditor({
|
||||
templateContent,
|
||||
previewWorkspace,
|
||||
@@ -157,6 +169,9 @@ export function PresentationEditor({
|
||||
}>();
|
||||
const [previewConfig, setPreviewConfig] = useState(config ?? {});
|
||||
const [previewBrandValues, setPreviewBrandValues] = useState<Record<string, unknown>>({});
|
||||
const [inquiryDraft, setInquiryDraft] =
|
||||
useState<RuntimeWorkspace["inquiryForms"][number]["fields"]>();
|
||||
const inquiryRuntime = previewWorkspace?.inquiryForms.find((item) => item.target === "brand");
|
||||
const sections = useMemo(() => {
|
||||
const declaredTemplateFields = (templateContent?.templateFields ?? []).map((field) => ({
|
||||
...field,
|
||||
@@ -220,10 +235,20 @@ export function PresentationEditor({
|
||||
(section.previewTargets ?? []).map((target) => ({
|
||||
...target,
|
||||
page: section.page,
|
||||
source: section.target,
|
||||
source: previewTargetSource(section, target.key),
|
||||
})),
|
||||
)
|
||||
.concat(
|
||||
selectedPage === "inquiry" && inquiryRuntime
|
||||
? inquiryRuntime.fields.map((field) => ({
|
||||
key: `inquiry-field:${field.key}`,
|
||||
label: field.label,
|
||||
page: "inquiry",
|
||||
source: "inquiry" as const,
|
||||
}))
|
||||
: [],
|
||||
),
|
||||
[templateContent, selectedPage, recordSections],
|
||||
[templateContent, selectedPage, recordSections, inquiryRuntime],
|
||||
);
|
||||
const activeTarget = selection?.targetKey;
|
||||
const selectedTarget = visibleTargets.find((target) => target.key === activeTarget);
|
||||
@@ -253,7 +278,9 @@ export function PresentationEditor({
|
||||
? trustQuery.data?.find((item) => item.id === selection?.recordId)
|
||||
: undefined;
|
||||
const editableSections = filterSectionsForPreviewTarget(
|
||||
visibleSections.filter((section) => section.target === "brand" || section.target === "inquiry"),
|
||||
visibleSections.filter((section) =>
|
||||
section.fields.some((field) => field.binding.startsWith("brand.")),
|
||||
),
|
||||
selectedTarget?.key,
|
||||
);
|
||||
const recordSection = useMemo(
|
||||
@@ -403,6 +430,9 @@ export function PresentationEditor({
|
||||
if (!previewWorkspace) return undefined;
|
||||
const next: RuntimeWorkspace = {
|
||||
...previewWorkspace,
|
||||
inquiryForms: previewWorkspace.inquiryForms.map((item) =>
|
||||
item.key === inquiryRuntime?.key && inquiryDraft ? { ...item, fields: inquiryDraft } : item,
|
||||
),
|
||||
brand: projectBrandAliases(
|
||||
{
|
||||
...previewWorkspace.brand,
|
||||
@@ -484,6 +514,8 @@ export function PresentationEditor({
|
||||
selectedRecord,
|
||||
recordSection,
|
||||
templateContent,
|
||||
inquiryDraft,
|
||||
inquiryRuntime,
|
||||
]);
|
||||
const selectedDetailOfferingId =
|
||||
detailOfferingId && draftWorkspace?.offerings.some((item) => item.id === detailOfferingId)
|
||||
@@ -589,7 +621,16 @@ export function PresentationEditor({
|
||||
<Typography.Title level={5}>
|
||||
{selectedTarget?.label ?? pageTitle(selectedPage ?? "home")}
|
||||
</Typography.Title>
|
||||
{selectedRecordSource && selection?.recordId && selectedSection ? (
|
||||
{activeTarget?.startsWith("inquiry-field:") && inquiryRuntime ? (
|
||||
<InquiryPreviewEditor
|
||||
runtime={
|
||||
draftWorkspace?.inquiryForms.find((item) => item.key === inquiryRuntime.key) ??
|
||||
inquiryRuntime
|
||||
}
|
||||
fieldKey={activeTarget.slice("inquiry-field:".length)}
|
||||
onDraft={setInquiryDraft}
|
||||
/>
|
||||
) : selectedRecordSource && selection?.recordId && selectedSection ? (
|
||||
selectedRecord ? (
|
||||
<RecordEditor
|
||||
key={`${selectedRecordSource}:${selectedRecord.id}:${selectedSection.key}`}
|
||||
@@ -660,7 +701,7 @@ export function PresentationEditor({
|
||||
initialValues={initialValues}
|
||||
onValuesChange={handleValuesChange}
|
||||
onFinish={(values) => save.mutate(values)}
|
||||
style={{ minHeight: 540, display: "flex", flexDirection: "column" }}
|
||||
style={{ minHeight: 0, display: "flex", flexDirection: "column" }}
|
||||
>
|
||||
<div style={{ flex: 1 }}>
|
||||
{editableSections.map((section, index) => (
|
||||
@@ -678,11 +719,7 @@ export function PresentationEditor({
|
||||
) : null}
|
||||
<Row gutter={16}>
|
||||
{section.fields.map((field) => (
|
||||
<Col
|
||||
key={field.key}
|
||||
xs={24}
|
||||
md={field.input === "textarea" || field.input === "list" ? 24 : 12}
|
||||
>
|
||||
<Col key={field.key} xs={24} md={24}>
|
||||
{field.input === "list" ? (
|
||||
<TemplateListField
|
||||
field={field}
|
||||
@@ -841,17 +878,13 @@ function RecordEditor({
|
||||
initialValues={initialValues}
|
||||
onValuesChange={(_, values) => onDraft(values)}
|
||||
onFinish={(values) => save.mutate(values)}
|
||||
style={{ minHeight: 540, display: "flex", flexDirection: "column" }}
|
||||
style={{ minHeight: 0, display: "flex", flexDirection: "column" }}
|
||||
>
|
||||
<Typography.Paragraph type="secondary">{section.description}</Typography.Paragraph>
|
||||
<div style={{ flex: 1 }}>
|
||||
<Row gutter={16}>
|
||||
{section.fields.map((field) => (
|
||||
<Col
|
||||
key={field.key}
|
||||
xs={24}
|
||||
md={field.input === "textarea" || field.input === "list" ? 24 : 12}
|
||||
>
|
||||
<Col key={field.key} xs={24} md={24}>
|
||||
{field.input === "list" ? (
|
||||
<TemplateListField
|
||||
field={field}
|
||||
|
||||
Reference in new issue
Block a user