Skip to content

Commit

Permalink
feat(procedure): 🚀 add pagination
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Ruck <[email protected]>
  • Loading branch information
Manuel Ruck committed Sep 23, 2024
1 parent f9b4e3a commit 27aa003
Show file tree
Hide file tree
Showing 14 changed files with 368 additions and 174 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
"[ignore]": {
"editor.defaultFormatter": "foxundermoon.shell-format"
},
"conventionalCommits.scopes": ["push-send-queued"]
"conventionalCommits.scopes": ["push-send-queued"],
"CodeGPT.apiKey": "OpenAI"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Link from 'next/link';

export const PaginationNavigation = ({ currentPage, totalPages }) => (
<div>
<Link href={`?page=${currentPage - 1}`} passHref>
<button disabled={currentPage === 1}>Previous</button>
</Link>
<span>{`Page ${currentPage} of ${totalPages}`}</span>
<Link href={`?page=${currentPage + 1}`} passHref>
<button disabled={currentPage === totalPages}>Next</button>
</Link>
</div>
);
54 changes: 36 additions & 18 deletions bundestag.io/admin/src/app/list/past/page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
import Entry from '../_components/entry';
import { IProcedure } from '@democracy-deutschland/bundestagio-common';
import { unstable_noStore as noStore } from 'next/cache';
import Link from 'next/link';
import { PaginationNavigation } from '../_components/pagination-navigation';

const ITEMS_PER_PAGE = 10;

export const dynamic = 'force-dynamic';

export default async function Page() {
async function getData(page: number = 1): Promise<{ procedures: IProcedure[]; count: number }> {
const limit = ITEMS_PER_PAGE;

console.log(
'getData',
page,
`${process.env.PROCEDURES_SERVER_URL}/procedures/list/past?limit=${limit}&offset=${page}`,
);
const res = await fetch(`${process.env.PROCEDURES_SERVER_URL}/procedures/list/past?limit=${limit}&page=${page}`, {
headers: {
'Cache-Control': 'no-cache',
cache: 'no-store',
},
});

if (!res.ok) {
throw new Error('Fehler beim Abrufen der Daten');
}

return res.json();
}

export default async function Page({ searchParams }: { searchParams: { page?: string } }) {
noStore();
const data = await getData();
const currentPage = searchParams.page ? parseInt(searchParams.page, 10) : 1; // Standardwert ist Seite 1
const { procedures, count } = await getData(currentPage);
const totalPages = Math.ceil(count / ITEMS_PER_PAGE); // Berechne die Gesamtseitenzahl basierend auf der Anzahl der Elemente

return (
<>
<h1>Past procedures</h1>
{data.map((procedure) => (
<PaginationNavigation currentPage={currentPage} totalPages={totalPages} />

{procedures.map((procedure) => (
<Entry
key={procedure.id}
title={procedure.title}
Expand All @@ -21,21 +52,8 @@ export default async function Page() {
}))}
/>
))}

<PaginationNavigation currentPage={currentPage} totalPages={totalPages} />
</>
);
}

async function getData(): Promise<IProcedure[]> {
const res = await fetch(`${process.env.PROCEDURES_SERVER_URL}/procedures/list/past`, {
headers: {
'Cache-Control': 'no-cache',
cache: 'no-store',
},
});

if (!res.ok) {
throw new Error('Fehler beim Abrufen der Daten');
}

return res.json();
}
55 changes: 37 additions & 18 deletions bundestag.io/admin/src/app/list/upcoming/page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
import Entry from '../_components/entry';
import { IProcedure } from '@democracy-deutschland/bundestagio-common';
import { unstable_noStore as noStore } from 'next/cache';
import Link from 'next/link';
import { PaginationNavigation } from '../_components/pagination-navigation';

const ITEMS_PER_PAGE = 10;

export const dynamic = 'force-dynamic';

export default async function Page() {
async function getData(page: number = 1): Promise<{ procedures: IProcedure[]; count: number }> {
const limit = ITEMS_PER_PAGE;

console.log(
'getData',
page,
`${process.env.PROCEDURES_SERVER_URL}/procedures/list/upcoming?limit=${limit}&offset=${page}`,
);
const res = await fetch(`${process.env.PROCEDURES_SERVER_URL}/procedures/list/upcoming?limit=${limit}&page=${page}`, {
headers: {
'Cache-Control': 'no-cache',
cache: 'no-store',
},
});

if (!res.ok) {
throw new Error('Fehler beim Abrufen der Daten');
}

return res.json();
}

export default async function Page({ searchParams }: { searchParams: { page?: string } }) {
noStore();
const data = await getData();
const currentPage = searchParams.page ? parseInt(searchParams.page, 10) : 1; // Standardwert ist Seite 1
const { procedures, count } = await getData(currentPage);
const totalPages = Math.ceil(count / ITEMS_PER_PAGE); // Berechne die Gesamtseitenzahl basierend auf der Anzahl der Elemente

return (
<>
<h1>Upcoming procedures</h1>
{data.map((procedure) => (

<PaginationNavigation currentPage={currentPage} totalPages={totalPages} />

{procedures.map((procedure) => (
<Entry
key={procedure.id}
title={procedure.title}
Expand All @@ -21,21 +53,8 @@ export default async function Page() {
}))}
/>
))}

<PaginationNavigation currentPage={currentPage} totalPages={totalPages} />
</>
);
}

async function getData(): Promise<IProcedure[]> {
const res = await fetch(`${process.env.PROCEDURES_SERVER_URL}/procedures/list/upcoming`, {
headers: {
'Cache-Control': 'no-cache',
cache: 'no-store',
},
});

if (!res.ok) {
throw new Error('Fehler beim Abrufen der Daten');
}

return res.json();
}
2 changes: 1 addition & 1 deletion services/procedures/bruno/environments/localhost.bru
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
vars {
url: http://localhost:3000
url: http://localhost:3006
}
2 changes: 1 addition & 1 deletion services/procedures/bruno/findAll.bru
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ get {
}

assert {
res.body.procedureId: isString
res.body.procedures[0].procedureId: isString
res.status: eq 200
}
8 changes: 6 additions & 2 deletions services/procedures/bruno/pastProcedures.bru
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ meta {
}

get {
url: {{url}}/procedures/list/past
url: {{url}}/procedures/list/past?limit=2
body: none
auth: none
}

params:query {
limit: 2
}

assert {
res.body[0].procedureId: isString
res.body.count: gt 0
res.status: eq 200
}
9 changes: 7 additions & 2 deletions services/procedures/bruno/upcomingProcedures.bru
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ meta {
}

get {
url: {{url}}/procedures/list/upcoming
url: {{url}}/procedures/list/upcoming?limit=1&page=2
body: none
auth: none
}

params:query {
limit: 1
page: 2
}

assert {
res.body[0].procedureId: isString
res.body.procedures[0].procedureId: isString
res.status: eq 200
}
2 changes: 1 addition & 1 deletion services/procedures/garden.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ spec:
sourcePath: src
mode: one-way
overrides:
- command: [pnpm, --filter, procedures, run, garden:dev]
- command: [pnpm, --filter, procedures, run, dev]
patchResources:
- name: procedures
kind: Deployment
Expand Down
14 changes: 14 additions & 0 deletions services/procedures/src/decorators/pagination.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';

export const Pagination = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
const { page = 1, limit = 10 } = request.query;

// Ensure valid pagination values
const validPage = Math.max(1, parseInt(page));
const validLimit = Math.min(Math.max(1, parseInt(limit)), 100); // Max limit of 100

return { page: validPage, limit: validLimit };
},
);
Loading

0 comments on commit 27aa003

Please sign in to comment.