Skip to content
Recipes/A list under a route param

A list under a route param

/projects/42/tasks?status=open: the status and the page are the list’s state, in the query; the project is in the path, so it is the list’s input. This recipe is for the Next.js App Router, set up as in Next.js.

src/projects/task-list.ts
import { defineList, enumParam, integerParam, STRAPI_DRIVER } from '@qubeejs/core';
import { API_URL } from './article-list';
/** A task as the Strapi API returns it. */
export type Task = {
id: number;
status: 'done' | 'open';
title: string;
};
/**
* The tasks of one project. The project is not in the query — it comes from the route path,
* /projects/42/tasks — so the list declares it as its input, by annotating the third parameter
* of `apply`.
*/
export const taskList = defineList({
apply: (builder, { status }, { projectId }: { projectId: string }) => {
builder.addFilter('project', projectId);
if (status) {
builder.addFilter('status', status);
}
},
params: {
page: integerParam('page', { default: 1, min: 1 }),
status: enumParam('status', ['done', 'open'] as const),
},
qubee: { baseUrl: API_URL, driver: STRAPI_DRIVER },
resource: 'tasks',
});

The Server Component reads the state from searchParams and the project from params, builds the request with both, and fetches it:

app/projects/[id]/tasks/page.tsx
import { buildListRequest, readListState } from '@qubeejs/core';
import { fetchQubeePage } from '@qubeejs/react/fetch';
import { type Task, taskList } from '@/projects/task-list';
import { ProjectTasksView } from './project-tasks-view';
type ProjectTasksPageProps = {
params: Promise<{ id: string }>;
searchParams: Promise<Record<string, string | string[] | undefined>>;
};
/** /projects/[id]/tasks, rendered on the server: the state from the query, the project from the path. */
export default async function ProjectTasksPage({ params, searchParams }: ProjectTasksPageProps) {
const request = buildListRequest(taskList, readListState(taskList, await searchParams), {
projectId: (await params).id,
});
const result = await fetchQubeePage<Task>(request);
return <ProjectTasksView result={result} />;
}

buildListRequest and readListState come from the core and fetchQubeePage from @qubeejs/react/fetch; none of them imports React.

The Client Component passes the same project to useQubeeList, taken from the route with useParams():

app/projects/[id]/tasks/project-tasks-view.tsx
'use client';
import type { PaginatedResult } from '@qubeejs/core';
import { useQubeeList } from '@qubeejs/react';
import { useParams } from 'next/navigation';
import { type Task, taskList } from '@/projects/task-list';
type ProjectTasksViewProps = {
result: PaginatedResult<Task>;
};
/**
* The interactive half of /projects/[id]/tasks: the status filter, and the page the server
* fetched. The project comes from the route path through `useParams()` and goes to the list as
* its input — never into the URL, which keeps only the page and the status.
*/
export function ProjectTasksView({ result }: ProjectTasksViewProps) {
const { id } = useParams<{ id: string }>();
const list = useQubeeList(taskList, { projectId: id });
return (
<section aria-busy={list.isPending}>
<button onClick={() => list.set({ status: 'open' })} type="button">
Open
</button>
<button onClick={() => list.set({ status: 'done' })} type="button">
Done
</button>
<button onClick={() => list.set({ status: undefined })} type="button">
All
</button>
<ul>
{result.data.map((task) => (
<li key={task.id}>{task.title}</li>
))}
</ul>
</section>
);
}

A status button changes the query, never the path: Next.js renders the Server Component again with the new searchParams and the same params. list.request carries the project too, if you fetch in the browser instead.

When the path holds a slug and the API wants the project’s id, the server awaits the lookup before it builds the request. In the browser, pass null until the id is known: the list has no request, and nothing is fetched.

src/projects/project-tasks.tsx
import { useQubeeList, useQubeeQuery } from '@qubeejs/react';
import { type Task, taskList } from './task-list';
type ProjectTasksProps = {
/** The project's id once your lookup of its slug has answered; `undefined` until then. */
projectId: string | undefined;
};
/**
* The tasks of a project whose id is still being looked up. Until it is known, the input is
* `null`: the list has no request, and nothing is fetched.
*/
export function ProjectTasks({ projectId }: ProjectTasksProps) {
const list = useQubeeList(taskList, projectId ? { projectId } : null);
const tasks = useQubeeQuery<Task>(list.request);
if (list.request === null) {
return <p role="status">Finding the project…</p>;
}
return (
<ul aria-busy={list.isPending || tasks.isFetching}>
{tasks.data?.data.map((task) => (
<li key={task.id}>{task.title}</li>
))}
</ul>
);
}