added option to customize year

This commit is contained in:
2024-08-12 00:37:38 +02:00
parent 08249974b8
commit c5812cf597

View File

@@ -2,39 +2,44 @@ import { useEffect, useState } from "react";
type Holiday = { "date": string, "localName": string, "countryCode": string, "fixed": string, "global": string, "types": string[] }
async function getText(): Promise<Holiday[]> {
const resp = await fetch("https://date.nager.at/api/v3/publicholidays/2024/DE")
const text_resp = await resp.text();
return JSON.parse(text_resp);
async function getText(satYear: string): Promise<Holiday[]> {
return JSON.parse(await (await fetch("https://date.nager.at/api/v3/publicholidays/" + satYear + "/DE")).text());
}
function ListHolidays() {
const [keys, setKeys] = useState<Holiday[]>([]);
const [year, setYear] = useState<string>("2024");
const [satYear, setSatYear] = useState<string>("2024");
useEffect(() => {
getText().then((value) => {
getText(satYear).then((value) => {
setKeys(value);
});
}, [])
return <table>
<thead>
<tr>
<th>Date</th>
<th>Name</th>
</tr>
</thead>
<tbody>
{ keys.map((holiday) =>
<tr key={holiday.date + "-" + holiday.localName}>
<td>
{holiday.date}
</td>
<td>
{holiday.localName}
</td>
}, [satYear])
return <>
<table>
<thead>
<tr>
<th>Date</th>
<th>Name</th>
</tr>
)}
</tbody>
</table>
</thead>
<tbody>
{keys.map((holiday) =>
<tr key={holiday.date + "-" + holiday.localName}>
<td>
{holiday.date}
</td>
<td>
{holiday.localName}
</td>
</tr>
)}
</tbody>
</table>
<input type="text" value={year} onChange={(e) => { setYear(e.target.value) }} />
<input type="submit" onClick={() => { setSatYear(year) }} />
</>
}
export default ListHolidays;