diff --git a/apps/admin/src/components/RecordContentEditor.tsx b/apps/admin/src/components/RecordContentEditor.tsx index 66cfe8f..59dd01a 100644 --- a/apps/admin/src/components/RecordContentEditor.tsx +++ b/apps/admin/src/components/RecordContentEditor.tsx @@ -1,31 +1,14 @@ -import { Alert, Button, Col, Form, Row, Space, Typography, message } from "antd"; -import { useMutation, useQueryClient } from "@tanstack/react-query"; -import { useEffect, useMemo, useState } from "react"; +import { Alert, Button, Col, Form, Row, Space, Typography } from "antd"; import { useNavigate } from "react-router-dom"; -import type { Offering, TemplateAdminSection, TrustContent } from "@personal-brand/api-client"; -import { createApiClient } from "../lib/api.js"; +import type { TemplateAdminSection } from "@personal-brand/api-client"; import { TemplateListField } from "./TemplateListField.js"; import { TemplateFieldRenderer } from "./TemplateFieldRenderer.js"; import { MutationError } from "./QueryState.js"; -import { - applyPresentationValues, - applyStandardEntityValues, - presentationFieldName, - readPresentationValue, - type PresentationValues, -} from "../features/brand/presentation-bindings.js"; +import { presentationFieldName } from "../features/brand/presentation-bindings.js"; +import { useRecordContentEditorState } from "./record-content/useRecordContentEditorState.js"; +import type { RecordContentEditorProps, RecordContentSource } from "./record-content/types.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; - visualSelection?: { targetKey: string; page: string }; -} +export type { RecordContentEditorProps, RecordContentSource }; export function RecordContentFields({ section, @@ -72,95 +55,22 @@ export function RecordContentEditor({ visualSelection, }: RecordContentEditorProps) { const navigate = useNavigate(); - const [form] = Form.useForm(); - const queryClient = useQueryClient(); const recordName = "name" in record ? record.name : record.title; - const [draftValues, setDraftValues] = useState(() => - Object.fromEntries( - section.fields.map((field) => [ - presentationFieldName(section.key, field.key), - readPresentationValue( - record.config, - field.binding, - field, - record as unknown as Record, - ), - ]), - ), - ); - 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" && - (key in (record as unknown as Record) || - 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"] }); - if (source === "offering") - void queryClient.invalidateQueries({ queryKey: ["admin-workspace-offering-fields"] }); - }, + const { form, handleValuesChange, initialValues, save, submit } = useRecordContentEditorState({ + source, + record, + section, + ...(onDraft ? { onDraft } : {}), + ...(onSaved ? { onSaved } : {}), }); + return (
{ - const currentValues = form.getFieldsValue(true) as PresentationValues; - const next = { - ...draftValues, - ...values, - ...currentValues, - }; - setDraftValues(next); - onDraft?.(next); - }} - onFinish={(values) => { - const currentValues = form.getFieldsValue(true) as PresentationValues; - save.mutate({ - ...draftValues, - ...values, - ...currentValues, - }); - }} + onValuesChange={handleValuesChange} + onFinish={submit} style={{ minHeight: 0, display: "flex", flexDirection: "column" }} > void; + onSaved?: () => void; + visualSelection?: { targetKey: string; page: string }; +} diff --git a/apps/admin/src/components/record-content/useRecordContentEditorState.ts b/apps/admin/src/components/record-content/useRecordContentEditorState.ts new file mode 100644 index 0000000..b90a141 --- /dev/null +++ b/apps/admin/src/components/record-content/useRecordContentEditorState.ts @@ -0,0 +1,117 @@ +import { Form, message } from "antd"; +import { useMutation, useQueryClient } from "@tanstack/react-query"; +import { useEffect, useMemo, useState } from "react"; +import type { TemplateAdminSection } from "@personal-brand/api-client"; +import type { Offering, TrustContent } from "@personal-brand/api-client"; +import { createApiClient } from "../../lib/api.js"; +import { + applyPresentationValues, + applyStandardEntityValues, + presentationFieldName, + readPresentationValue, + type PresentationValues, +} from "../../features/brand/presentation-bindings.js"; +import type { RecordContentSource } from "./types.js"; + +function createInitialValues( + record: Offering | TrustContent, + section: TemplateAdminSection, +): PresentationValues { + return Object.fromEntries( + section.fields.map((field) => [ + presentationFieldName(section.key, field.key), + readPresentationValue( + record.config, + field.binding, + field, + record as unknown as Record, + ), + ]), + ); +} + +function createChangedStandardValues( + record: Offering | TrustContent, + source: RecordContentSource, + section: TemplateAdminSection, + values: PresentationValues, +) { + const entity = record as unknown as Record; + const standard = applyStandardEntityValues(entity, source, [section], values); + + return Object.fromEntries( + Object.entries(standard).filter( + ([key, value]) => key !== "config" && (key in entity || value !== entity[key]), + ), + ); +} + +export function useRecordContentEditorState({ + source, + record, + section, + onDraft, + onSaved, +}: { + source: RecordContentSource; + record: Offering | TrustContent; + section: TemplateAdminSection; + onDraft?: ((values: PresentationValues) => void) | undefined; + onSaved?: (() => void) | undefined; +}) { + const [form] = Form.useForm(); + const queryClient = useQueryClient(); + const [draftValues, setDraftValues] = useState(() => + createInitialValues(record, section), + ); + const initialValues = useMemo(() => createInitialValues(record, section), [record, section]); + + useEffect(() => form.setFieldsValue(initialValues), [form, initialValues]); + + const save = useMutation({ + mutationFn: async (values: PresentationValues): Promise => { + const config = applyPresentationValues(record.config, [section], values); + const changed = createChangedStandardValues(record, source, section, values); + 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"] }); + if (source === "offering") { + void queryClient.invalidateQueries({ queryKey: ["admin-workspace-offering-fields"] }); + } + }, + }); + + const submit = (values: PresentationValues) => { + const currentValues = form.getFieldsValue(true) as PresentationValues; + save.mutate({ ...draftValues, ...values, ...currentValues }); + }; + + const handleValuesChange = (_values: PresentationValues) => { + const currentValues = form.getFieldsValue(true) as PresentationValues; + const next = { ...draftValues, ..._values, ...currentValues }; + setDraftValues(next); + onDraft?.(next); + }; + + return { + draftValues, + form, + handleValuesChange, + initialValues, + save, + submit, + }; +} diff --git a/apps/admin/src/features/services/OfferingEditorModal.tsx b/apps/admin/src/features/services/OfferingEditorModal.tsx new file mode 100644 index 0000000..e0a5d17 --- /dev/null +++ b/apps/admin/src/features/services/OfferingEditorModal.tsx @@ -0,0 +1,181 @@ +import { Alert, Button, Form, Input, Modal, Segmented, Select, Typography } from "antd"; +import type { + Offering, + RuntimeWorkspace, + TemplateAdminField, + TemplateAdminSection, +} from "@personal-brand/api-client"; +import { ImageUrlField } from "../../components/ImageUrlField.js"; +import { RecordContentFields } from "../../components/RecordContentEditor.js"; +import { ComponentTemplatePreview } from "../brand/ComponentTemplatePreview.js"; +import type { PreviewTarget } from "../brand/preview-selection.js"; +import type { FormInstance } from "antd"; +import "./ServicesPage.css"; + +type OfferingEditorModalProps = { + open: boolean; + editingOffering: Offering | null; + form: FormInstance>; + previewPage: "offerings" | "detail"; + previewWorkspace: RuntimeWorkspace | undefined; + previewTargets: Record<"offerings" | "detail", PreviewTarget[]>; + activeTemplateFields: TemplateAdminField[]; + templateFieldSection: TemplateAdminSection; + hasTemplateDetailCopy: boolean; + listTarget: PreviewTarget; + detailTarget: PreviewTarget | undefined; + saving: boolean; + onClose: () => void; + onPreviewPageChange: (page: "offerings" | "detail") => void; + onValuesChange: (values: Record) => void; + onFinish: (values: Record) => void; + onEditInBrand: () => void; +}; + +export function OfferingEditorModal({ + open, + editingOffering, + form, + previewPage, + previewWorkspace, + previewTargets, + activeTemplateFields, + templateFieldSection, + hasTemplateDetailCopy, + listTarget, + detailTarget, + saving, + onClose, + onPreviewPageChange, + onValuesChange, + onFinish, + onEditInBrand, +}: OfferingEditorModalProps) { + return ( + form.submit()} + confirmLoading={saving} + > +
+ {editingOffering && previewWorkspace ? ( +
+ + + {}} + /> +
+ ) : null} + onValuesChange(allValues)} + onFinish={onFinish} + > +
+ 基础信息 + + + + + + + {!hasTemplateDetailCopy ? ( + + + + ) : null} + + + + + + + + + + + - - - - - - - - {!hasTemplateDetailCopy ? ( - - - - ) : null} - - - - - - - - - - - ({ value: item.type, label: item.label }))} - /> - - {selectedTypeMeta?.pages?.length ? ( - - 这类内容会显示在: - {selectedTypeMeta.pages.map(pageLabel).join("、")} - - ) : null} - - - - {!selectedTypeMeta?.hideStandardFields?.includes("summary") && ( - - - - )} - {!selectedTypeMeta?.hideStandardFields?.includes("content") && ( - - - - )} - {selectedTypeMeta?.configFields?.map((field) => - field.input === "list" ? ( - - ) : ( - - {field.input === "textarea" ? : } - - ), - )} - - - - - ({ value: item.type, label: item.label }))} + /> + + {selectedTypeMeta?.pages?.length ? ( + + 这类内容会显示在:{selectedTypeMeta.pages.map(pageLabel).join("、")} + + ) : null} + + + + {!selectedTypeMeta?.hideStandardFields?.includes("summary") && ( + + + + )} + {!selectedTypeMeta?.hideStandardFields?.includes("content") && ( + + + + )} + {selectedTypeMeta?.configFields?.map((field) => + field.input === "list" ? ( + + ) : ( + + {field.input === "textarea" ? : } + + ), + )} + + + + +