generated from pascalmartineau/wp-skeleton
wip: refactor sections
This commit is contained in:
@@ -1,37 +1,30 @@
|
||||
import type { ThePageFragment, TheArticleFragment, TheEventFragment, TheLocationFragment, TheMembershipFragment, TheProjectFragment, TheResourceFragment } from "#graphql-operations";
|
||||
import { ThePage, TheArticle, TheEvent, TheLocation, TheMembership, TheProject, TheResource } from "#components";
|
||||
|
||||
const nodes = {
|
||||
Event: TheEvent,
|
||||
Location: TheLocation,
|
||||
Membership: TheMembership,
|
||||
Page: ThePage,
|
||||
Post: TheArticle,
|
||||
Project: TheProject,
|
||||
Resource: TheResource,
|
||||
} as const;
|
||||
|
||||
export async function useNodeByUri(uri: string) {
|
||||
const { data, error } = await useAsyncGraphqlQuery("nodeByUri", { uri }, { graphqlCaching: { client: true } });
|
||||
if (error.value) {
|
||||
console.log(error.value);
|
||||
throw createError({ statusCode: 500, statusMessage: "Erreur interne", message: error.value.message });
|
||||
}
|
||||
if (!data.value) {
|
||||
if (data.value?.errors.length) {
|
||||
throw createError({ statusCode: 500, statusMessage: "Erreur interne", message: data.value.errors.map((error) => error.message).join("\n") });
|
||||
}
|
||||
if (!data.value?.data.nodeByUri) {
|
||||
throw createError({ statusCode: 500, statusMessage: "Erreur interne", message: "La page n'a retourné aucunes données." });
|
||||
}
|
||||
if (data.value.errors.length) {
|
||||
console.log(data.value.errors);
|
||||
throw createError({ statusCode: 500, statusMessage: "Erreur serveur", message: data.value.errors.map((error) => error.message).join("\n") });
|
||||
}
|
||||
const node = data.value.data.nodeByUri;
|
||||
const breadcrumbs = node?.breadcrumbs?.map(({ label, to }) => ({ label, to: to || undefined })) || [];
|
||||
switch (node?.__typename) {
|
||||
case "Event":
|
||||
return { component: TheEvent, node: node as TheEventFragment, breadcrumbs };
|
||||
case "Location":
|
||||
return { component: TheLocation, node: node as TheLocationFragment, breadcrumbs };
|
||||
case "Membership":
|
||||
return { component: TheMembership, node: node as TheMembershipFragment, breadcrumbs };
|
||||
case "Page":
|
||||
return { component: ThePage, node: node as ThePageFragment, breadcrumbs };
|
||||
case "Post":
|
||||
return { component: TheArticle, node: node as TheArticleFragment, breadcrumbs };
|
||||
case "Project":
|
||||
return { component: TheProject, node: node as TheProjectFragment, breadcrumbs };
|
||||
case "Resource":
|
||||
return { component: TheResource, node: node as TheResourceFragment, breadcrumbs };
|
||||
default:
|
||||
throw createError({ statusCode: 404, statusMessage: "Page non trouvée", message: "La page que vous cherchez n'existe pas." });
|
||||
const { __typename, breadcrumbs, ...attrs } = data.value.data.nodeByUri;
|
||||
const component = nodes[__typename as keyof typeof nodes] || null;
|
||||
if (!component) {
|
||||
throw createError({ statusCode: 500, statusMessage: "Erreur interne", message: "Le type de page est invalide." });
|
||||
}
|
||||
return { component, breadcrumbs: breadcrumbs || [], attrs };
|
||||
}
|
||||
|
||||
12
wp-content/themes/ccat/app/composables/useSection.ts
Normal file
12
wp-content/themes/ccat/app/composables/useSection.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import type { TheSectionFragment } from "#graphql-operations";
|
||||
import { SectionTextBlock } from "#components";
|
||||
|
||||
const sections = {
|
||||
GroupAbstractBuilderSectionsTextBlockLayout: SectionTextBlock,
|
||||
} as const;
|
||||
|
||||
export function useSection({ fieldGroupName, ...attrs }: TheSectionFragment) {
|
||||
const component = sections[fieldGroupName as keyof typeof sections];
|
||||
if (!component) throw createError({ statusCode: 500, statusMessage: "Erreur interne", message: "Type de section invalide." });
|
||||
return { component, attrs };
|
||||
}
|
||||
Reference in New Issue
Block a user