feat: 完善模板内容后台配置能力
This commit is contained in:
1 parent
2bc2193b44
commit
9266ec824a
37 files changed
+2273
-63
No files matched your search
@@ -13,11 +13,17 @@ import {
|
||||
message,
|
||||
} from "antd";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import type { Offering } from "@personal-brand/api-client";
|
||||
import type { Offering, TemplateAdminField } from "@personal-brand/api-client";
|
||||
import { 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 {
|
||||
applyPresentationValues,
|
||||
presentationFieldName,
|
||||
readPresentationValue,
|
||||
} from "../brand/presentation-bindings.js";
|
||||
import { MutationError, QueryState } from "../../components/QueryState.js";
|
||||
|
||||
export function ServicesPage() {
|
||||
@@ -30,11 +36,32 @@ export function ServicesPage() {
|
||||
queryKey: ["admin-offerings"],
|
||||
queryFn: () => client.listAdminOfferings(),
|
||||
});
|
||||
const workspace = useQuery({
|
||||
queryKey: ["admin-workspace-offering-fields"],
|
||||
queryFn: () => client.getAdminWorkspace(),
|
||||
});
|
||||
const templateFields: TemplateAdminField[] = (
|
||||
workspace.data?.templateContent?.admin.sections ?? []
|
||||
)
|
||||
.filter((section) => section.target === "offering")
|
||||
.flatMap((section) =>
|
||||
section.fields.filter((field) => field.binding.startsWith("offering.config.")),
|
||||
);
|
||||
const saveOffering = useMutation({
|
||||
mutationFn: (values: Record<string, unknown>) =>
|
||||
editingOffering
|
||||
? client.updateAdminOffering(editingOffering.id, values)
|
||||
: client.createAdminOffering(values as Omit<Offering, "id" | "brandId">),
|
||||
mutationFn: (values: Record<string, unknown>) => {
|
||||
const fields = Object.fromEntries(
|
||||
Object.entries(values).filter(([key]) => key !== "config" && !key.startsWith("offering:")),
|
||||
);
|
||||
const config = applyPresentationValues(
|
||||
editingOffering?.config,
|
||||
[{ key: "offering", fields: templateFields }],
|
||||
values,
|
||||
);
|
||||
const body = { ...fields, config };
|
||||
return editingOffering
|
||||
? client.updateAdminOffering(editingOffering.id, body)
|
||||
: client.createAdminOffering(body as Omit<Offering, "id" | "brandId">);
|
||||
},
|
||||
onSuccess: () => {
|
||||
message.success("业务提供项已保存");
|
||||
setOfferingOpen(false);
|
||||
@@ -72,16 +99,33 @@ export function ServicesPage() {
|
||||
coverUrl: item.coverUrl ?? "",
|
||||
sortOrder: item.sortOrder,
|
||||
isEnabled: item.isEnabled,
|
||||
...Object.fromEntries(
|
||||
templateFields.map((field) => [
|
||||
presentationFieldName("offering", field.key),
|
||||
readPresentationValue(item.config, field.binding, field),
|
||||
]),
|
||||
),
|
||||
}
|
||||
: { kind: "service", isEnabled: true, sortOrder: 0 },
|
||||
: {
|
||||
kind: "service",
|
||||
isEnabled: true,
|
||||
sortOrder: 0,
|
||||
...Object.fromEntries(
|
||||
templateFields.map((field) => [
|
||||
presentationFieldName("offering", field.key),
|
||||
field.input === "list" ? [] : undefined,
|
||||
]),
|
||||
),
|
||||
},
|
||||
);
|
||||
};
|
||||
return (
|
||||
<QueryState
|
||||
loading={offerings.isLoading}
|
||||
error={offerings.error}
|
||||
loading={offerings.isLoading || workspace.isLoading}
|
||||
error={offerings.error || workspace.error}
|
||||
onRetry={() => {
|
||||
void offerings.refetch();
|
||||
void workspace.refetch();
|
||||
}}
|
||||
>
|
||||
<AdminPageHeader
|
||||
@@ -229,6 +273,30 @@ export function ServicesPage() {
|
||||
]}
|
||||
/>
|
||||
</Form.Item>
|
||||
{templateFields.map((field) =>
|
||||
field.input === "list" ? (
|
||||
<TemplateListField
|
||||
key={field.key}
|
||||
field={field}
|
||||
name={presentationFieldName("offering", field.key)}
|
||||
/>
|
||||
) : (
|
||||
<Form.Item
|
||||
key={field.key}
|
||||
name={presentationFieldName("offering", field.key)}
|
||||
label={field.label}
|
||||
help={field.help}
|
||||
>
|
||||
{field.input === "textarea" ? (
|
||||
<Input.TextArea rows={3} />
|
||||
) : field.input === "image" ? (
|
||||
<ImageUrlField />
|
||||
) : (
|
||||
<Input />
|
||||
)}
|
||||
</Form.Item>
|
||||
),
|
||||
)}
|
||||
</Form>
|
||||
</Modal>
|
||||
</QueryState>
|
||||
|
||||
Reference in new issue
Block a user