Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(admin): show description if procedure list is empty #604

Merged
merged 2 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
54 changes: 8 additions & 46 deletions bundestag.io/admin/src/app/list/past/page.tsx
Original file line number Diff line number Diff line change
@@ -1,54 +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>
<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} />;
}
55 changes: 8 additions & 47 deletions bundestag.io/admin/src/app/list/upcoming/page.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +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>

<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} />;
}