From 6005a4871cc061a820d554054f6db6010bbb021b Mon Sep 17 00:00:00 2001 From: Manuel Ruck Date: Sat, 30 Nov 2024 09:01:41 +0100 Subject: [PATCH 1/2] feat(admin): show description if procedure list is empty Signed-off-by: Manuel Ruck --- bundestag.io/admin/src/app/list/past/page.tsx | 36 +++++++++++-------- .../admin/src/app/list/upcoming/page.tsx | 35 ++++++++++-------- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/bundestag.io/admin/src/app/list/past/page.tsx b/bundestag.io/admin/src/app/list/past/page.tsx index ea580072..7788ac78 100644 --- a/bundestag.io/admin/src/app/list/past/page.tsx +++ b/bundestag.io/admin/src/app/list/past/page.tsx @@ -34,21 +34,27 @@ export default async function Page({ searchParams }: { searchParams: { page?: st return ( <>

Past procedures

- - - {procedures.map((procedure) => ( - ({ - party: vote.party, - decision: vote.main, - }))} - /> - ))} - - + + {totalPages === 0 &&

There are no past procedures.

} + {totalPages > 0 && ( + <> + + + {procedures.map((procedure) => ( + ({ + party: vote.party, + decision: vote.main, + }))} + /> + ))} + + + + )} ); } diff --git a/bundestag.io/admin/src/app/list/upcoming/page.tsx b/bundestag.io/admin/src/app/list/upcoming/page.tsx index 7018ab70..038282a2 100644 --- a/bundestag.io/admin/src/app/list/upcoming/page.tsx +++ b/bundestag.io/admin/src/app/list/upcoming/page.tsx @@ -35,21 +35,26 @@ export default async function Page({ searchParams }: { searchParams: { page?: st <>

Upcoming procedures

- - - {procedures.map((procedure) => ( - ({ - party: vote.party, - decision: vote.main, - }))} - /> - ))} - - + {totalPages === 0 &&

There are no upcoming procedures.

} + {totalPages > 0 && ( + <> + + + {procedures.map((procedure) => ( + ({ + party: vote.party, + decision: vote.main, + }))} + /> + ))} + + + + )} ); } From 91b4be173cb1cf6804d8bce7cf4675d15f1d7e7f Mon Sep 17 00:00:00 2001 From: Manuel Ruck Date: Sat, 30 Nov 2024 11:54:00 +0100 Subject: [PATCH 2/2] refactor(admin): reuse duplicate code of new lists Signed-off-by: Manuel Ruck --- .../src/app/list/_components/PageTemplate.tsx | 39 ++++++++++++ .../src/app/list/_utils/fetchProcedures.ts | 22 +++++++ bundestag.io/admin/src/app/list/past/page.tsx | 60 +++---------------- .../admin/src/app/list/upcoming/page.tsx | 60 +++---------------- 4 files changed, 77 insertions(+), 104 deletions(-) create mode 100644 bundestag.io/admin/src/app/list/_components/PageTemplate.tsx create mode 100644 bundestag.io/admin/src/app/list/_utils/fetchProcedures.ts diff --git a/bundestag.io/admin/src/app/list/_components/PageTemplate.tsx b/bundestag.io/admin/src/app/list/_components/PageTemplate.tsx new file mode 100644 index 00000000..254a304a --- /dev/null +++ b/bundestag.io/admin/src/app/list/_components/PageTemplate.tsx @@ -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 ( + <> +

{title}

+ {totalPages === 0 &&

There are no {title.toLowerCase()}.

} + {totalPages > 0 && ( + <> + + {procedures.map((procedure) => ( + ({ + party: vote.party, + decision: vote.main, + }))} + /> + ))} + + + )} + + ); +} diff --git a/bundestag.io/admin/src/app/list/_utils/fetchProcedures.ts b/bundestag.io/admin/src/app/list/_utils/fetchProcedures.ts new file mode 100644 index 00000000..bcc2814d --- /dev/null +++ b/bundestag.io/admin/src/app/list/_utils/fetchProcedures.ts @@ -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 }; diff --git a/bundestag.io/admin/src/app/list/past/page.tsx b/bundestag.io/admin/src/app/list/past/page.tsx index 7788ac78..992b48b4 100644 --- a/bundestag.io/admin/src/app/list/past/page.tsx +++ b/bundestag.io/admin/src/app/list/past/page.tsx @@ -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 ( - <> -

Past procedures

- - {totalPages === 0 &&

There are no past procedures.

} - {totalPages > 0 && ( - <> - - - {procedures.map((procedure) => ( - ({ - party: vote.party, - decision: vote.main, - }))} - /> - ))} - - - - )} - + const currentPage = searchParams.page ? parseInt(searchParams.page, 10) : 1; + const { procedures, count } = await fetchProcedures( + `${process.env.PROCEDURES_SERVER_URL}/procedures/list/past`, + currentPage, ); + + return ; } diff --git a/bundestag.io/admin/src/app/list/upcoming/page.tsx b/bundestag.io/admin/src/app/list/upcoming/page.tsx index 038282a2..8cdb1ae5 100644 --- a/bundestag.io/admin/src/app/list/upcoming/page.tsx +++ b/bundestag.io/admin/src/app/list/upcoming/page.tsx @@ -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 ( - <> -

Upcoming procedures

- - {totalPages === 0 &&

There are no upcoming procedures.

} - {totalPages > 0 && ( - <> - - - {procedures.map((procedure) => ( - ({ - party: vote.party, - decision: vote.main, - }))} - /> - ))} - - - - )} - + const currentPage = searchParams.page ? parseInt(searchParams.page, 10) : 1; + const { procedures, count } = await fetchProcedures( + `${process.env.PROCEDURES_SERVER_URL}/procedures/list/upcoming`, + currentPage, ); + + return ; }