Skip to content

Commit

Permalink
refactor(admin): reuse duplicate code of new lists
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Ruck <[email protected]>
  • Loading branch information
Manuel Ruck committed Nov 30, 2024
1 parent 6005a48 commit 91b4be1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 104 deletions.
39 changes: 39 additions & 0 deletions bundestag.io/admin/src/app/list/_components/PageTemplate.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Entry from '../_components/entry';
import { PaginationNavigation } from '../_components/pagination-navigation';
import { IProcedure } from '@democracy-deutschland/bundestagio-common';
import { ITEMS_PER_PAGE } from '../_utils/fetchProcedures';

interface PageTemplateProps {
title: string;
procedures: IProcedure[];
count: number;
currentPage: number;
}

export default function PageTemplate({ title, procedures, count, currentPage }: PageTemplateProps) {
const totalPages = Math.ceil(count / ITEMS_PER_PAGE);

return (
<>
<h1>{title}</h1>
{totalPages === 0 && <p>There are no {title.toLowerCase()}.</p>}
{totalPages > 0 && (
<>
<PaginationNavigation currentPage={currentPage} totalPages={totalPages} />
{procedures.map((procedure) => (
<Entry
key={procedure.id}
title={procedure.title}
procedureId={procedure.procedureId}
votes={procedure.customData?.voteResults.partyVotes.map((vote) => ({
party: vote.party,
decision: vote.main,
}))}
/>
))}
<PaginationNavigation currentPage={currentPage} totalPages={totalPages} />
</>
)}
</>
);
}
22 changes: 22 additions & 0 deletions bundestag.io/admin/src/app/list/_utils/fetchProcedures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { IProcedure } from '@democracy-deutschland/bundestagio-common';

const ITEMS_PER_PAGE = 10;

export async function fetchProcedures(url: string, page: number): Promise<{ procedures: IProcedure[]; count: number }> {
const limit = ITEMS_PER_PAGE;

const res = await fetch(`${url}?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 { ITEMS_PER_PAGE };
60 changes: 8 additions & 52 deletions bundestag.io/admin/src/app/list/past/page.tsx
Original file line number Diff line number Diff line change
@@ -1,60 +1,16 @@
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;
import { fetchProcedures } from '../_utils/fetchProcedures';
import PageTemplate from '../_components/PageTemplate';

export const dynamic = 'force-dynamic';

async function getData(page: number = 1): Promise<{ procedures: IProcedure[]; count: number }> {
const limit = ITEMS_PER_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 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>

{totalPages === 0 && <p>There are no past procedures.</p>}
{totalPages > 0 && (
<>
<PaginationNavigation currentPage={currentPage} totalPages={totalPages} />

{procedures.map((procedure) => (
<Entry
key={procedure.id}
title={procedure.title}
procedureId={procedure.procedureId}
votes={procedure.customData?.voteResults.partyVotes.map((vote) => ({
party: vote.party,
decision: vote.main,
}))}
/>
))}

<PaginationNavigation currentPage={currentPage} totalPages={totalPages} />
</>
)}
</>
const currentPage = searchParams.page ? parseInt(searchParams.page, 10) : 1;
const { procedures, count } = await fetchProcedures(
`${process.env.PROCEDURES_SERVER_URL}/procedures/list/past`,
currentPage,
);

return <PageTemplate title="Past procedures" procedures={procedures} count={count} currentPage={currentPage} />;
}
60 changes: 8 additions & 52 deletions bundestag.io/admin/src/app/list/upcoming/page.tsx
Original file line number Diff line number Diff line change
@@ -1,60 +1,16 @@
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;
import { fetchProcedures } from '../_utils/fetchProcedures';
import PageTemplate from '../_components/PageTemplate';

export const dynamic = 'force-dynamic';

async function getData(page: number = 1): Promise<{ procedures: IProcedure[]; count: number }> {
const limit = ITEMS_PER_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 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>

{totalPages === 0 && <p>There are no upcoming procedures.</p>}
{totalPages > 0 && (
<>
<PaginationNavigation currentPage={currentPage} totalPages={totalPages} />

{procedures.map((procedure) => (
<Entry
key={procedure.id}
title={procedure.title}
procedureId={procedure.procedureId}
votes={procedure.customData?.voteResults.partyVotes.map((vote) => ({
party: vote.party,
decision: vote.main,
}))}
/>
))}

<PaginationNavigation currentPage={currentPage} totalPages={totalPages} />
</>
)}
</>
const currentPage = searchParams.page ? parseInt(searchParams.page, 10) : 1;
const { procedures, count } = await fetchProcedures(
`${process.env.PROCEDURES_SERVER_URL}/procedures/list/upcoming`,
currentPage,
);

return <PageTemplate title="Upcoming procedures" procedures={procedures} count={count} currentPage={currentPage} />;
}

0 comments on commit 91b4be1

Please sign in to comment.