mirror of
https://github.com/ManInDark/ReactHolidays.git
synced 2026-01-21 06:34:29 +01:00
added option to customize year
This commit is contained in:
@@ -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;
|
||||
Reference in New Issue
Block a user