Files
cultureat-bak/wp-content/themes/ccat/app/composables/useNodeByUri.ts
Pascal Martineau 299213d5e9
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 1m6s
refactor: membership => profile
2025-09-24 16:01:46 -04:00

32 lines
1.3 KiB
TypeScript

import { ThePage, TheArticle, TheEvent, TheListing, TheLocation, TheProfile, TheProject, TheResource } from "#components";
const nodes = {
Event: TheEvent,
Location: TheLocation,
Listing: TheListing,
Profile: TheProfile,
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 };
}