generated from pascalmartineau/wp-skeleton
refactor: sections-map
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 1m6s
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 1m6s
This commit is contained in:
@@ -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>
|
||||||
|
|||||||
@@ -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}` });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -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>`;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
15
wp-content/themes/ccat/shared/utils/sections-map.ts
Normal file
15
wp-content/themes/ccat/shared/utils/sections-map.ts
Normal 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 };
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user