Skip to content

Commit

Permalink
♻️ refactor: move project sorting to sanity query
Browse files Browse the repository at this point in the history
The sorting of projects by featureOrder has been moved from client-side JavaScript
to the Sanity GROQ query, improving code maintainability and performance
  • Loading branch information
w3bdesign committed Dec 8, 2024
1 parent 4c24f2d commit 9d022fd
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 9 deletions.
9 changes: 1 addition & 8 deletions src/app/prosjekter/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ export const metadata: Metadata = {
export default async function Prosjekter() {
const posts: Project[] = await client.fetch(projectsQuery);

// Sort by featureOrder, putting null/undefined values last
const sortedProjects = [...posts].sort((a, b) => {
if (a.featureOrder === null || a.featureOrder === undefined) return 1;
if (b.featureOrder === null || b.featureOrder === undefined) return -1;
return a.featureOrder - b.featureOrder;
});

return (
<RootLayout>
<main
Expand All @@ -33,7 +26,7 @@ export default async function Prosjekter() {
<PageHeader>Prosjekter</PageHeader>
<div className="container mx-auto">
<div className="grid grid-cols-1 sm:grid-cols-1 md:grid-cols-1 xl:grid-cols-2 gap-8">
{sortedProjects.map((project) => (
{posts.map((project) => (
<ProsjektCard key={project.id} {...project} />
))}
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/sanity/queries.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { groq } from "next-sanity";

export const projectsQuery = groq`
*[_type == "project"]{
*[_type == "project"] | order(featureOrder asc) {
id,
name,
description,
Expand Down

0 comments on commit 9d022fd

Please sign in to comment.