diff --git a/apps/admin/src/components/RecordContentEditor.tsx b/apps/admin/src/components/RecordContentEditor.tsx new file mode 100644 index 0000000..c430845 --- /dev/null +++ b/apps/admin/src/components/RecordContentEditor.tsx @@ -0,0 +1,192 @@ +import { + Alert, + Button, + Col, + Form, + Input, + InputNumber, + Row, + Select, + Space, + Switch, + Typography, + message, +} from "antd"; +import { useMutation, useQueryClient } from "@tanstack/react-query"; +import { useEffect, useMemo } from "react"; +import type { + Offering, + TemplateAdminField, + TemplateAdminSection, + TrustContent, +} from "@personal-brand/api-client"; +import { createApiClient } from "../lib/api.js"; +import { ImageUrlField } from "./ImageUrlField.js"; +import { TemplateListField } from "./TemplateListField.js"; +import { MutationError } from "./QueryState.js"; +import { + applyPresentationValues, + applyStandardEntityValues, + presentationFieldName, + readPresentationValue, + type PresentationValues, +} from "../features/brand/presentation-bindings.js"; + +export type RecordContentSource = "offering" | "trust"; + +export interface RecordContentEditorProps { + source: RecordContentSource; + record: Offering | TrustContent; + section: TemplateAdminSection; + context: "visual" | "services"; + onDraft?: (values: PresentationValues) => void; + onSaved?: () => void; +} + +function renderField(field: TemplateAdminField) { + if (field.input === "textarea") + return ( + + ); + if (field.input === "image") + return ; + if (field.input === "phone") return ; + if (field.input === "number") return ; + if (field.input === "boolean") return ; + if (field.input === "select") + return ({ ...item }))} + placeholder={field.placeholder} + /> + ); + return ; +} + +export function RecordContentFields({ section }: { section: TemplateAdminSection }) { + return ( + + {section.fields.map((field) => ( + + {field.input === "list" ? ( + + ) : ( + + {renderField(field)} + + )} + + ))} + + ); +} + +export function RecordContentEditor({ + source, + record, + section, + context, + onDraft, + onSaved, +}: RecordContentEditorProps) { + const [form] = Form.useForm(); + const queryClient = useQueryClient(); + const recordName = "name" in record ? record.name : record.title; + const initialValues = useMemo( + () => + Object.fromEntries( + section.fields.map((field) => [ + presentationFieldName(section.key, field.key), + readPresentationValue( + record.config, + field.binding, + field, + record as unknown as Record, + ), + ]), + ), + [record, section], + ); + useEffect(() => form.setFieldsValue(initialValues), [form, initialValues]); + const save = useMutation({ + mutationFn: async (values: PresentationValues): Promise => { + const config = applyPresentationValues(record.config, [section], values); + const standard = applyStandardEntityValues( + record as unknown as Record, + source, + [section], + values, + ); + const changed = Object.fromEntries( + Object.entries(standard).filter( + ([key, value]) => + key !== "config" && value !== (record as unknown as Record)[key], + ), + ); + const client = createApiClient(); + if (source === "offering") + await client.updateAdminOffering(record.id, { ...changed, config }); + else await client.updateAdminTrustContent(record.id, { ...changed, config }); + }, + onSuccess: () => { + message.success("已保存内容草稿,发布后访客才会看到更新"); + onSaved?.(); + void queryClient.invalidateQueries({ + queryKey: [source === "offering" ? "admin-offerings" : "trust-content"], + }); + void queryClient.invalidateQueries({ queryKey: ["admin-workspace"] }); + void queryClient.invalidateQueries({ queryKey: ["admin-preview-workspace"] }); + }, + }); + return ( +
onDraft?.(values)} + onFinish={(values) => save.mutate(values)} + style={{ minHeight: 0, display: "flex", flexDirection: "column" }} + > + + {save.isPending ? 正在保存内容… : null} + {section.description} +
+ +
+ + + + + + ); +} diff --git a/apps/admin/src/features/brand/PresentationEditor.tsx b/apps/admin/src/features/brand/PresentationEditor.tsx index d1d2feb..9cade3d 100644 --- a/apps/admin/src/features/brand/PresentationEditor.tsx +++ b/apps/admin/src/features/brand/PresentationEditor.tsx @@ -23,8 +23,6 @@ import type { RuntimeWorkspace, TemplateAdminField, TemplateAdminSection, - Offering, - TrustContent, } from "@personal-brand/api-client"; import type { BrandProfile } from "@personal-brand/api-client"; import { projectBrandAliases, projectTrustPresentation } from "@personal-brand/api-client"; @@ -32,6 +30,7 @@ import { createApiClient } from "../../lib/api.js"; import { ImageUrlField } from "../../components/ImageUrlField.js"; import { TemplateListField } from "../../components/TemplateListField.js"; import { MutationError } from "../../components/QueryState.js"; +import { RecordContentEditor } from "../../components/RecordContentEditor.js"; import { TemplatePreview } from "./TemplatePreview.js"; import { InquiryPreviewEditor } from "./InquiryPreviewEditor.js"; import { @@ -809,11 +808,12 @@ export function PresentationEditor({ /> ) : selectedRecordSource && selection?.recordId && selectedSection ? ( selectedRecord ? ( - setRecordDraft({ source: selectedRecordSource, @@ -1016,113 +1016,6 @@ export function PresentationEditor({ ); } -function RecordEditor({ - source, - record, - section, - onDraft, - onSaved, -}: { - source: "offering" | "trust"; - record: Offering | TrustContent; - section: TemplateAdminSection; - onDraft: (values: PresentationValues) => void; - onSaved: () => void; -}) { - const [form] = Form.useForm(); - const queryClient = useQueryClient(); - const initialValues = useMemo( - () => - Object.fromEntries( - section.fields.map((field) => [ - presentationFieldName(section.key, field.key), - readPresentationValue( - record.config, - field.binding, - field, - record as unknown as Record, - ), - ]), - ), - [record, section], - ); - useEffect(() => form.setFieldsValue(initialValues), [form, initialValues]); - const save = useMutation({ - mutationFn: async (values: PresentationValues): Promise => { - const config = applyPresentationValues(record.config, [section], values); - const standard = applyStandardEntityValues( - record as unknown as Record, - source, - [section], - values, - ); - const changed = Object.fromEntries( - Object.entries(standard).filter( - ([key, value]) => - key !== "config" && value !== (record as unknown as Record)[key], - ), - ); - const client = createApiClient(); - if (source === "offering") - await client.updateAdminOffering(record.id, { ...changed, config }); - else await client.updateAdminTrustContent(record.id, { ...changed, config }); - }, - onSuccess: () => { - message.success("内容已保存为草稿,发布后访客才会看到更新"); - onSaved(); - void queryClient.invalidateQueries({ - queryKey: [source === "offering" ? "admin-offerings" : "trust-content"], - }); - void queryClient.invalidateQueries({ queryKey: ["admin-workspace"] }); - void queryClient.invalidateQueries({ queryKey: ["admin-preview-workspace"] }); - }, - }); - return ( -
onDraft(values)} - onFinish={(values) => save.mutate(values)} - style={{ minHeight: 0, display: "flex", flexDirection: "column" }} - > - {save.isPending ? 正在保存内容… : null} - {section.description} -
- - {section.fields.map((field) => ( - - {field.input === "list" ? ( - - ) : ( - - {renderField(field)} - - )} - - ))} - -
- - - - -
- ); -} - function pageTitle(page: string): string { return ( ( diff --git a/apps/admin/src/features/services/ServicesPage.tsx b/apps/admin/src/features/services/ServicesPage.tsx index 36e4fae..6353dbf 100644 --- a/apps/admin/src/features/services/ServicesPage.tsx +++ b/apps/admin/src/features/services/ServicesPage.tsx @@ -12,12 +12,16 @@ import { message, } from "antd"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; -import type { Offering, TemplateAdminField } from "@personal-brand/api-client"; -import { useState } from "react"; +import type { + Offering, + TemplateAdminField, + TemplateAdminSection, +} from "@personal-brand/api-client"; +import { useMemo, useState } from "react"; import { createApiClient } from "../../lib/api.js"; import { AdminPageHeader } from "../../components/AdminPageHeader.js"; import { ImageUrlField } from "../../components/ImageUrlField.js"; -import { TemplateListField } from "../../components/TemplateListField.js"; +import { RecordContentFields } from "../../components/RecordContentEditor.js"; import { applyPresentationValues, presentationFieldName, @@ -46,6 +50,17 @@ export function ServicesPage() { .flatMap((section) => section.fields.filter((field) => field.binding.startsWith("offering.config.")), ); + const templateFieldSection = useMemo( + () => ({ + key: "offering", + title: "模板内容", + description: "", + page: "detail", + target: "offering", + fields: templateFields, + }), + [templateFields], + ); const offeringPageLabel = workspace.data?.templateContent?.navigation.find((item) => item.page === "offerings")?.label ?? "业务展示页"; @@ -299,30 +314,14 @@ export function ServicesPage() { ]} /> - {templateFields.map((field) => - field.input === "list" ? ( - - ) : ( - - {field.input === "textarea" ? ( - - ) : field.input === "image" ? ( - - ) : ( - - )} - - ), - )} + {templateFields.length ? ( + <> + + 以下模板字段与小程序页面编辑的是同一份业务数据;可在此集中修改,也可到页面编辑中点选对应卡片。 + + + + ) : null}