-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(admin): reuse duplicate code of new lists
Signed-off-by: Manuel Ruck <[email protected]>
- Loading branch information
Manuel Ruck
committed
Nov 30, 2024
1 parent
6005a48
commit 91b4be1
Showing
4 changed files
with
77 additions
and
104 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
bundestag.io/admin/src/app/list/_components/PageTemplate.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} /> | ||
</> | ||
)} | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} />; | ||
} |