refactor: sections-map
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 1m6s

This commit is contained in:
2025-09-24 14:05:47 -04:00
parent 0bffe2768d
commit 5ce55b5c33
5 changed files with 20 additions and 20 deletions

View File

@@ -7,6 +7,5 @@ defineProps<SectionTextBlockFragment>();
<template> <template>
<section data-section-type="text-block"> <section data-section-type="text-block">
<div :class="layoutSettings?.contentWidth" v-html="content" /> <div :class="layoutSettings?.contentWidth" v-html="content" />
<pre>{{ layoutSettings }}</pre>
</section> </section>
</template> </template>

View File

@@ -1,12 +0,0 @@
import type { SectionTextBlockFragment, TheSectionFragment } from "#graphql-operations";
import { SectionTextBlock } from "#components";
export function useSection(section: TheSectionFragment) {
const { fieldGroupName, ...attrs } = section;
switch (fieldGroupName) {
case "GroupAbstractBuilderSectionsTextBlockLayout":
return { component: SectionTextBlock, attrs: attrs as SectionTextBlockFragment };
default:
throw createError({ statusCode: 500, statusMessage: "Erreur interne", message: `Type de section invalide: ${fieldGroupName}` });
}
}

View File

@@ -51,9 +51,8 @@ function ccat_acf_init() {
// Helper: Get GraphQL field group name for flexible content layout // Helper: Get GraphQL field group name for flexible content layout
function ccat_get_graphql_field_group_name( $field_group_key, $layout_name ) { function ccat_get_graphql_field_group_name( $field_group_key, $layout_name ) {
$field_group_name = \WPGraphQL\Utils\Utils::format_type_name( $field_group_key );
$layout_type_name = \WPGraphQL\Utils\Utils::format_type_name( $layout_name ); $layout_type_name = \WPGraphQL\Utils\Utils::format_type_name( $layout_name );
return "{$field_group_name}{$layout_type_name}Layout"; return "GroupAbstractBuilderSections{$layout_type_name}Layout";
} }
// Helper: Convert field name to GraphQL format // Helper: Convert field name to GraphQL format

View File

@@ -1,14 +1,13 @@
import { renderToString } from "vue/server-renderer"; import { renderToString } from "vue/server-renderer";
import { createSSRApp, h } from "vue"; import { createSSRApp, h } from "vue";
import type { TheSectionFragment } from "#graphql-operations";
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
const { fieldGroupName, ...attrs } = await readBody(event); const { fieldGroupName, ...attrs } = await readBody<TheSectionFragment>(event);
const componentName = fieldGroupName.replace(/^GroupAbstractBuilder(.*)Layout$/, "Section$1");
try { try {
const componentModule = await import(`../../app/components/sections/${componentName}.vue`);
const app = createSSRApp({ const app = createSSRApp({
render() { render() {
return h(componentModule.default, attrs); return h(sectionsMap[fieldGroupName as FieldGroupName], attrs);
}, },
}); });
const html = await renderToString(app); const html = await renderToString(app);
@@ -16,6 +15,6 @@ export default defineEventHandler(async (event) => {
} }
catch (error) { catch (error) {
const message = error instanceof Error ? error.message : "Une erreur est survenue."; const message = error instanceof Error ? error.message : "Une erreur est survenue.";
throw createError({ statusCode: 500, statusMessage: "Erreur interne", message }); return `<div style="color: red">${message}</div>`;
} }
}); });

View File

@@ -0,0 +1,15 @@
import SectionTextBlock from "~/components/sections/SectionTextBlock.vue";
import type { TheSectionFragment } from "#graphql-operations";
export const sectionsMap = {
GroupAbstractBuilderSectionsTextBlockLayout: SectionTextBlock,
} as const;
export type FieldGroupName = keyof typeof sectionsMap;
export function useSection(section: TheSectionFragment) {
const { fieldGroupName, ...attrs } = section;
const component = sectionsMap[fieldGroupName as FieldGroupName];
if (!component) throw createError({ statusCode: 500, statusMessage: "Erreur interne", message: "Section invalide." });
return { component, attrs };
}