generated from pascalmartineau/wp-skeleton
refactor: data fetching for page navigation
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 1m1s
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 1m1s
This commit is contained in:
@@ -1,32 +1,30 @@
|
|||||||
import type { ThePageFragment, TheArticleFragment, TheEventFragment, TheLocationFragment, TheMembershipFragment, TheProjectFragment, TheResourceFragment } from "#graphql-operations";
|
import type { ThePageFragment, TheArticleFragment, TheEventFragment, TheLocationFragment, TheMembershipFragment, TheProjectFragment, TheResourceFragment } from "#graphql-operations";
|
||||||
import { ThePage, TheArticle, TheEvent, TheLocation, TheMembership, TheProject, TheResource } from "#components";
|
import { ThePage, TheArticle, TheEvent, TheLocation, TheMembership, TheProject, TheResource } from "#components";
|
||||||
|
|
||||||
export async function useNodeByUri() {
|
export async function useNodeByUri(uri: string) {
|
||||||
const route = useRoute();
|
const { data, error } = await useAsyncGraphqlQuery("nodeByUri", { uri }, { graphqlCaching: { client: true } });
|
||||||
const uri = computed(() => route.path);
|
|
||||||
const { data } = await useAsyncGraphqlQuery("nodeByUri", { uri: uri.value }, { graphqlCaching: { client: true } });
|
|
||||||
if (data.value?.errors?.length) {
|
|
||||||
throw createError({ statusCode: 500, message: "Erreur lors de la récupération du contenu", fatal: true });
|
|
||||||
}
|
|
||||||
const node = data.value?.data.nodeByUri;
|
const node = data.value?.data.nodeByUri;
|
||||||
const breadcrumbs = node?.breadcrumbs?.map(({ label, to }) => ({ label, to: to || undefined })) || [];
|
if (error.value || !node) {
|
||||||
|
const message = error.value?.message || "Erreur lors de la récupération du contenu";
|
||||||
switch (node?.__typename) {
|
throw createError({ statusCode: 500, message });
|
||||||
case "Page":
|
}
|
||||||
return { component: ThePage, node: node as ThePageFragment, breadcrumbs };
|
const breadcrumbs = node.breadcrumbs?.map(({ label, to }) => ({ label, to: to || undefined })) || [];
|
||||||
case "Post":
|
switch (node.__typename) {
|
||||||
return { component: TheArticle, node: node as TheArticleFragment, breadcrumbs };
|
|
||||||
case "Event":
|
case "Event":
|
||||||
return { component: TheEvent, node: node as TheEventFragment, breadcrumbs };
|
return { component: TheEvent, node: node as TheEventFragment, breadcrumbs };
|
||||||
case "Location":
|
case "Location":
|
||||||
return { component: TheLocation, node: node as TheLocationFragment, breadcrumbs };
|
return { component: TheLocation, node: node as TheLocationFragment, breadcrumbs };
|
||||||
case "Membership":
|
case "Membership":
|
||||||
return { component: TheMembership, node: node as TheMembershipFragment, breadcrumbs };
|
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":
|
case "Project":
|
||||||
return { component: TheProject, node: node as TheProjectFragment, breadcrumbs };
|
return { component: TheProject, node: node as TheProjectFragment, breadcrumbs };
|
||||||
case "Resource":
|
case "Resource":
|
||||||
return { component: TheResource, node: node as TheResourceFragment, breadcrumbs };
|
return { component: TheResource, node: node as TheResourceFragment, breadcrumbs };
|
||||||
default:
|
default:
|
||||||
throw createError({ statusCode: 404, message: "Page non trouvée", fatal: true });
|
throw createError({ statusCode: 404, message: "Page non trouvée" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
const { node, component, breadcrumbs } = await useNodeByUri();
|
const { path } = useRoute();
|
||||||
|
const { node, component, breadcrumbs } = await useNodeByUri(path);
|
||||||
useSeoMeta({ title: node?.title });
|
useSeoMeta({ title: node?.title });
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user