refactor: 拆分业务、信任内容与公共工作台模块
This commit is contained in:
1 parent
357f271639
commit
466dc66907
33 files changed
+4950
-4500
No files matched your search
@@ -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<PresentationValues>();
|
||||
const queryClient = useQueryClient();
|
||||
const recordName = "name" in record ? record.name : record.title;
|
||||
const [draftValues, setDraftValues] = useState<PresentationValues>(() =>
|
||||
Object.fromEntries(
|
||||
section.fields.map((field) => [
|
||||
presentationFieldName(section.key, field.key),
|
||||
readPresentationValue(
|
||||
record.config,
|
||||
field.binding,
|
||||
field,
|
||||
record as unknown as Record<string, unknown>,
|
||||
),
|
||||
]),
|
||||
),
|
||||
);
|
||||
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<string, unknown>,
|
||||
),
|
||||
]),
|
||||
),
|
||||
[record, section],
|
||||
);
|
||||
useEffect(() => form.setFieldsValue(initialValues), [form, initialValues]);
|
||||
const save = useMutation({
|
||||
mutationFn: async (values: PresentationValues): Promise<void> => {
|
||||
const config = applyPresentationValues(record.config, [section], values);
|
||||
const standard = applyStandardEntityValues(
|
||||
record as unknown as Record<string, unknown>,
|
||||
source,
|
||||
[section],
|
||||
values,
|
||||
);
|
||||
const changed = Object.fromEntries(
|
||||
Object.entries(standard).filter(
|
||||
([key, value]) =>
|
||||
key !== "config" &&
|
||||
(key in (record as unknown as Record<string, unknown>) ||
|
||||
value !== (record as unknown as Record<string, unknown>)[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 (
|
||||
<Form
|
||||
form={form}
|
||||
layout="vertical"
|
||||
initialValues={initialValues}
|
||||
onValuesChange={(_, values) => {
|
||||
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" }}
|
||||
>
|
||||
<Alert
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import type { Offering, TemplateAdminSection, TrustContent } from "@personal-brand/api-client";
|
||||
import 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;
|
||||
visualSelection?: { targetKey: string; page: string };
|
||||
}
|
||||
@@ -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<string, unknown>,
|
||||
),
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
function createChangedStandardValues(
|
||||
record: Offering | TrustContent,
|
||||
source: RecordContentSource,
|
||||
section: TemplateAdminSection,
|
||||
values: PresentationValues,
|
||||
) {
|
||||
const entity = record as unknown as Record<string, unknown>;
|
||||
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<PresentationValues>();
|
||||
const queryClient = useQueryClient();
|
||||
const [draftValues, setDraftValues] = useState<PresentationValues>(() =>
|
||||
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<void> => {
|
||||
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,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user