Files
cultureat-bak/wp-content/themes/ccat/app/composables/useNodeByUri.ts

31 lines
1.3 KiB
TypeScript

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) {
throw createError({ statusCode: 500, statusMessage: "Erreur interne", message: error.value.message });
}
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." });
}
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 };
}