From 29ac52339a2cd64011e9da52cbe97d72c6e36c50 Mon Sep 17 00:00:00 2001 From: Daniel Fjeldstad <45217974+w3bdesign@users.noreply.github.com> Date: Fri, 6 Dec 2024 04:24:30 +0100 Subject: [PATCH] Refactor Groq queries Fixes #507 Refactor Groq queries to a separate file and update imports. * Add `pageContentQuery` and `navigationQuery` to `src/lib/sanity/queries.ts`. * Remove `pageContentQuery` from `src/app/page.tsx` and import it from `src/lib/sanity/queries.ts`. * Remove `projectsQuery` from `src/app/prosjekter/page.tsx` and import it from `src/lib/sanity/queries.ts`. * Remove `navigationQuery` from `src/app/RootLayout.tsx` and import it from `src/lib/sanity/queries.ts`. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/w3bdesign/dfweb-v4/issues/507?shareId=XXXX-XXXX-XXXX-XXXX). --- src/app/RootLayout.tsx | 15 +-------------- src/app/page.tsx | 11 +---------- src/app/prosjekter/page.tsx | 13 +------------ src/lib/sanity/queries.ts | 22 ++++++++++++++++++++++ 4 files changed, 25 insertions(+), 36 deletions(-) diff --git a/src/app/RootLayout.tsx b/src/app/RootLayout.tsx index 6186edf0..0ba817f9 100644 --- a/src/app/RootLayout.tsx +++ b/src/app/RootLayout.tsx @@ -1,20 +1,7 @@ -import { groq } from "next-sanity"; import { client } from "@/lib/sanity/client"; import Header from "@/components/Layout/Header.component"; import ErrorBoundary from "@/components/ErrorBoundary/ErrorBoundary"; - -const navigationQuery = groq` - *[_type == "navigation"][0] { - title, - links[] { - title, - name, - hash, - href, - externalLink - } - } -`; +import { navigationQuery } from "@/lib/sanity/queries"; export default async function RootLayout({ children, diff --git a/src/app/page.tsx b/src/app/page.tsx index 781e8e03..a7c8b5de 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,7 +1,7 @@ -import { groq } from "next-sanity"; import dynamic from "next/dynamic"; import RootLayout from "@/app/RootLayout"; import { client } from "@/lib/sanity/client"; +import { pageContentQuery } from "@/lib/sanity/queries"; const DynamicHero = dynamic(() => import("@/components/Index/Hero.component"), { loading: () =>
Loading hero...
, @@ -15,15 +15,6 @@ const DynamicIndexContent = dynamic( ); export default async function HomePage() { - const pageContentQuery = groq` - *[_type == 'page' && title match 'Hjem'][0]{ - "id": _id, - title, - hero, - content - } - `; - const pageContent = await client.fetch(pageContentQuery); return ( diff --git a/src/app/prosjekter/page.tsx b/src/app/prosjekter/page.tsx index df1c4007..c9bb5b93 100644 --- a/src/app/prosjekter/page.tsx +++ b/src/app/prosjekter/page.tsx @@ -3,6 +3,7 @@ import PageHeader from "@/components/UI/PageHeader.component"; import ProsjektCard from "@/components/Prosjekter/ProsjektCard.component"; import { client } from "@/lib/sanity/client"; +import { projectsQuery } from "@/lib/sanity/queries"; import type { Project } from "@/types/sanity.types"; import type { Metadata } from "next/types"; @@ -12,18 +13,6 @@ export const metadata: Metadata = { description: "Daniel Fjeldstad | Frontend Web Utvikler | Portefølje", }; -const projectsQuery = `*[_type == "project"] | order(featured desc, featureOrder asc, _createdAt desc) { - id, - name, - description, - subdescription, - projectimage, - urlwww, - urlgithub, - featured, - featureOrder -}`; - export default async function Prosjekter() { const posts: Project[] = await client.fetch(projectsQuery); diff --git a/src/lib/sanity/queries.ts b/src/lib/sanity/queries.ts index e9c0284a..4ef648a4 100644 --- a/src/lib/sanity/queries.ts +++ b/src/lib/sanity/queries.ts @@ -39,3 +39,25 @@ export const cvQuery = groq` } } `; + +export const pageContentQuery = groq` + *[_type == 'page' && title match 'Hjem'][0]{ + "id": _id, + title, + hero, + content + } +`; + +export const navigationQuery = groq` + *[_type == "navigation"][0] { + title, + links[] { + title, + name, + hash, + href, + externalLink + } + } +`;