-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added ability to list swiss holidays for easier camp start sele…
…ction #12
- Loading branch information
1 parent
1ea0e8c
commit 222621a
Showing
10 changed files
with
493 additions
and
1 deletion.
There are no files selected for viewing
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
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,86 @@ | ||
import axios from "axios"; | ||
import { format } from "date-fns"; | ||
|
||
const client = axios.create({ | ||
baseURL: 'https://openholidaysapi.org/', | ||
headers: { | ||
"Content-type": "application/json", | ||
}, | ||
}); | ||
|
||
export const loadSubdivisions = async (languageCode?: OHApiLanguageCode): Promise<OHApiSubdivision[]> => { | ||
const params = getBaseParams(languageCode); | ||
return (await client.get<OHApiSubdivision[]>('/subdivisions', { params: params })).data; | ||
} | ||
|
||
export const loadPublicHolidays = async (validFromDate: Date, validToDate: Date, languageCode?: OHApiLanguageCode): Promise<OHApiHoliday[]> => { | ||
const params = getBaseParams(languageCode); | ||
params.append('validFrom', format(validFromDate, 'yyyy-MM-dd')); | ||
params.append('validTo', format(validToDate, 'yyyy-MM-dd')); | ||
|
||
return (await client.get<OHApiHoliday[]>('/publicholidays', { params: params })).data; | ||
} | ||
|
||
export const loadSchoolHolidays = async (validFromDate: Date, validToDate: Date, languageCode?: OHApiLanguageCode): Promise<OHApiHoliday[]> => { | ||
const params = getBaseParams(languageCode); | ||
params.append('validFrom', format(validFromDate, 'yyyy-MM-dd')); | ||
params.append('validTo', format(validToDate, 'yyyy-MM-dd')); | ||
|
||
return (await client.get<OHApiHoliday[]>('/schoolholidays', { params: params })).data; | ||
} | ||
|
||
const getBaseParams = (languageCode?: OHApiLanguageCode): URLSearchParams => { | ||
const params = new URLSearchParams(); | ||
params.append('countryIsoCode', 'CH'); | ||
|
||
if (languageCode) { | ||
params.append('languageIsoCode', languageCode); | ||
} | ||
|
||
return params; | ||
} | ||
|
||
export const parseLanguageOrDefault = (lang: string): OHApiLanguageCode => { | ||
const openApiLanguageCode = lang.toUpperCase(); | ||
return isValidLanguageCode(openApiLanguageCode) | ||
? openApiLanguageCode as OHApiLanguageCode | ||
: 'EN'; | ||
} | ||
|
||
const isValidLanguageCode = (code: string): boolean => code === 'DE' || code === 'FR' || code === 'IT' || code === 'EN'; | ||
|
||
export type OHApiLanguageCode = 'DE' | 'FR' | 'IT' | 'EN'; | ||
|
||
export interface OHApiSubdivision { | ||
name: OHApiLanguageText[]; | ||
shortName: string; | ||
category: OHApiLanguageText[]; | ||
code: string; | ||
isoCode: string; | ||
children: string[]; | ||
officialLanguages: string[]; | ||
comment: string | null; | ||
} | ||
|
||
export interface OHApiHoliday { | ||
id: string; | ||
name: OHApiLanguageText[]; | ||
type: string; | ||
startDate: string; | ||
endDate: string; | ||
nationwide: boolean; | ||
regionalScope: string; | ||
temporalScope: string; | ||
subdivisions?: OHApiSubdivisionInfo[]; | ||
comment?: OHApiLanguageText[]; | ||
} | ||
|
||
export interface OHApiLanguageText { | ||
language: OHApiLanguageCode; | ||
text: string; | ||
} | ||
|
||
export interface OHApiSubdivisionInfo { | ||
code: string; | ||
shortName: string; | ||
} |
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
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
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
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
Oops, something went wrong.