Skip to content

Commit

Permalink
Project filters: server side
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarrenechea committed Nov 16, 2023
1 parent e712eb1 commit f49e93e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion client/src/app/(app)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { PropsWithChildren } from "react";

import { headers } from "next/headers";

import { Hydrate, dehydrate } from "@tanstack/react-query";
import { parseAsArrayOf, parseAsInteger } from "next-usequerystate";

import getQueryClient from "@/lib/react-query/getQueryClient";

Expand All @@ -20,6 +23,11 @@ import Sidebar from "@/containers/sidebar";
import LayoutProviders from "./layout-providers";

export default async function AppLayout({ children }: PropsWithChildren) {
const url = new URL(headers().get("x-url")!);
const searchParams = url.searchParams;

const pillarsParser = parseAsArrayOf(parseAsInteger).withDefault([]);

const queryClient = getQueryClient();

// Prefetch countries
Expand Down Expand Up @@ -66,7 +74,7 @@ export default async function AppLayout({ children }: PropsWithChildren) {
await queryClient.prefetchQuery({
...getGetProjectsQueryOptions(
GET_PROJECTS_OPTIONS("", {
pillars: [],
pillars: pillarsParser.parseServerSide(searchParams.get("pillars") || []),
}),
),
});
Expand Down
1 change: 1 addition & 0 deletions client/src/app/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const useSyncCountriesComparison = () => {
export const useSyncProject = () => {
return useQueryState("project", parseAsInteger);
};

const pillarsParser = parseAsArrayOf(parseAsInteger).withDefault([]);
export const useSyncPillars = () => {
return useQueryState("pillars", pillarsParser);
Expand Down
15 changes: 15 additions & 0 deletions client/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// /middleware.ts
import { NextResponse } from "next/server";

export function middleware(request: Request) {
// Store current request url in a custom header, which you can read later
const requestHeaders = new Headers(request.headers);
requestHeaders.set("x-url", request.url);

return NextResponse.next({
request: {
// Apply new request headers
headers: requestHeaders,
},
});
}

0 comments on commit f49e93e

Please sign in to comment.