Skip to content

Commit 4494bff

Browse files
committed
refactor: use explicit functions for Hering API calls
1 parent 00cec56 commit 4494bff

22 files changed

+225
-202
lines changed

src/App.tsx

+19-29
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ import React, { createContext, lazy, Suspense, useEffect, useState } from 'react
22
import { HashRouter as Router, Route, Routes } from "react-router-dom";
33
import { useTranslation } from 'react-i18next';
44
import i18n from './i18n';
5-
import client from './client';
65
import { checkLinks } from './helper/LinkChecker';
7-
import { StartPageT } from "./pages/home/HomePage";
8-
import { CalendarPageT } from './pages/calendar/CalendarPage';
9-
import { SectionsByKey, SectionT } from './pages/section/SectionPage';
10-
import { ImpressumPageT } from './pages/impressum/ImpressumPage';
6+
import { SectionsByKey } from './pages/section/SectionPage';
117
import Loading from "./components/loading/Loading";
128
import Navigation from "./components/navigation/Navigation";
9+
import { HApiCalendarPage, HApiImpressumPage, HApiLink, loadCalendarPage, loadImpressumPage, loadLinks, loadSections, loadStartPage, HApiSection, HApiStartPage } from "./apis/hering-api";
1310

1411
const Footer = lazy(() => import('./components/footer/Footer'))
1512
const SectionHashHelper = lazy(() => import('./helper/SectionHashHelper'))
@@ -19,40 +16,33 @@ const SectionPage = lazy(() => import('./pages/section/SectionPage'))
1916
const CalendarPage = lazy(() => import('./pages/calendar/CalendarPage'))
2017
const SearchPage = lazy(() => import('./pages/search/SearchPage'))
2118

22-
export type LinkT = {
23-
title: string
24-
link: string | undefined
25-
key: string
26-
slug: string | null
27-
}
28-
29-
export const LinksContext = createContext<LinkT[]>([])
19+
export const LinksContext = createContext<HApiLink[]>([])
3020

3121
export default function App() {
3222

3323
const lang = i18n.language
3424
const { t } = useTranslation()
3525

36-
const [sections, setSections] = useState<SectionT[] | undefined>();
37-
const [links, setLinks] = useState<LinkT[] | undefined>();
38-
const [startPage, setStartPage] = useState<StartPageT | undefined>();
39-
const [calendarPage, setCalendarPage] = useState<CalendarPageT | undefined>();
40-
const [impressumPage, setImpressumPage] = useState<ImpressumPageT | undefined>();
26+
const [sections, setSections] = useState<HApiSection[] | undefined>();
27+
const [links, setLinks] = useState<HApiLink[] | undefined>();
28+
const [startPage, setStartPage] = useState<HApiStartPage | undefined>();
29+
const [calendarPage, setCalendarPage] = useState<HApiCalendarPage | undefined>();
30+
const [impressumPage, setImpressumPage] = useState<HApiImpressumPage | undefined>();
4131

4232
useEffect(() => {
4333
const fetchData = async () => {
4434
const responses = await Promise.all([
45-
client.get('/sections?_sort=sorting:ASC&_locale=' + lang),
46-
client.get('/links?_locale=' + lang),
47-
client.get('/start-page?_locale=' + lang),
48-
client.get('/calendar-page?_locale=' + lang),
49-
client.get('/impressum-page?_locale=' + lang)])
35+
loadSections(lang),
36+
loadLinks(lang),
37+
loadStartPage(lang),
38+
loadCalendarPage(lang),
39+
loadImpressumPage(lang)])
5040

51-
setSections(responses[0].data)
52-
setLinks(responses[1].data)
53-
setStartPage(responses[2].data)
54-
setCalendarPage(responses[3].data)
55-
setImpressumPage(responses[4].data)
41+
setSections(responses[0])
42+
setLinks(responses[1])
43+
setStartPage(responses[2])
44+
setCalendarPage(responses[3])
45+
setImpressumPage(responses[4])
5646
}
5747

5848
fetchData()
@@ -64,7 +54,7 @@ export default function App() {
6454
</div>
6555
}
6656

67-
const sectionsByKey = sections.reduce((map: SectionsByKey, section: SectionT) => {
57+
const sectionsByKey = sections.reduce((map: SectionsByKey, section: HApiSection) => {
6858
map[section.slug] = section
6959
return map
7060
}, {})

src/apis/hering-api.ts

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import axios from "axios";
2+
3+
const client = axios.create({
4+
// baseURL: 'http://localhost:1337/',
5+
baseURL: 'https://hering-api.herokuapp.com/',
6+
headers: {
7+
"Content-type": "application/json",
8+
},
9+
});
10+
11+
const getData = async <T>(url: string): Promise<T> => {
12+
const response = await client.get(url);
13+
return response.data as T;
14+
};
15+
16+
export const loadStartPage = async (lang: string): Promise<HApiStartPage> => {
17+
return await getData<HApiStartPage>(`/start-page?_locale=${lang}`)
18+
}
19+
20+
export const loadCalendarPage = async (lang: string): Promise<HApiCalendarPage> => {
21+
return await getData<HApiCalendarPage>(`/calendar-page?_locale=${lang}`)
22+
}
23+
24+
export const loadImpressumPage = async (lang: string): Promise<HApiImpressumPage> => {
25+
return await getData<HApiImpressumPage>(`/impressum-page?_locale=${lang}`)
26+
}
27+
28+
export const loadSections = async (lang: string): Promise<HApiSection[]> => {
29+
return await getData<HApiSection[]>(`/sections?_sort=sorting:ASC&_locale=${lang}`)
30+
};
31+
32+
export const loadLinks = async (lang: string): Promise<HApiLink[]> => {
33+
return await getData<HApiLink[]>(`/links?_locale=${lang}`)
34+
}
35+
36+
export const loadTasks = async (lang: string): Promise<HApiTask[]> => {
37+
return await getData<HApiTask[]>(`/tasks?_locale=${lang}`)
38+
}
39+
40+
export interface HApiStartPage {
41+
title: string
42+
menu_name: string
43+
icon: HApiIcon
44+
content: string
45+
}
46+
47+
export interface HApiCalendarPage {
48+
title: string
49+
menu_name: string
50+
icon: HApiIcon
51+
content: string
52+
}
53+
54+
export interface HApiImpressumPage {
55+
title: string
56+
menu_name: string
57+
content: string
58+
}
59+
60+
export interface HApiSection {
61+
chapters: HApiChapter[]
62+
sorting: number
63+
title: string
64+
content: string
65+
slug: string
66+
menu_name: string
67+
localizations: any
68+
}
69+
70+
export interface HApiChapter {
71+
id: number
72+
sorting: number
73+
title: string
74+
menu_name: string
75+
content: string
76+
slug: string
77+
slug_with_section: string
78+
icon: HApiIcon
79+
section: HApiSection
80+
responsible: HApiRole[]
81+
}
82+
83+
export interface HApiRole {
84+
rolle: string
85+
}
86+
87+
export interface HApiLink {
88+
title: string
89+
link: string | undefined
90+
key: string
91+
slug: string | null
92+
}
93+
94+
export interface HApiTask {
95+
id: number
96+
title: string
97+
days: number
98+
responsible: HApiRoles[]
99+
targets: HApiRoles[]
100+
chapters: HApiChapter[]
101+
}
102+
103+
export interface HApiRoles {
104+
rolle: string
105+
}
106+
107+
export interface HApiIcon {
108+
url: string
109+
mime: string
110+
}

src/client.jsx

-9
This file was deleted.

src/components/footer/Footer.tsx

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import React from 'react'
22
import i18n from '../../i18n'
33
import { Link, useLocation, useNavigate } from 'react-router-dom'
4-
import client from '../../client'
54
import FooterSvg from './FooterSvg'
65
import PbsLogoSvg from './PbsLogoSvg'
76
import './footer.less'
8-
import { SectionT } from "../../pages/section/SectionPage";
7+
import { loadSections, HApiSection } from "../../apis/hering-api";
98

109
type Props = {
11-
sections: SectionT[]
10+
sections: HApiSection[]
1211
}
1312

1413
function Footer(props: Props) {
@@ -18,7 +17,7 @@ function Footer(props: Props) {
1817
const navigate = useNavigate();
1918
const currentYear = new Date().getFullYear()
2019

21-
const changeLanguage = (lang: string, oldSections: SectionT[]) => {
20+
const changeLanguage = (lang: string, oldSections: HApiSection[]) => {
2221
let redirect = false
2322

2423
const path = location.pathname.replace('/', '')
@@ -36,13 +35,13 @@ function Footer(props: Props) {
3635
return
3736
}
3837

39-
client.get('/sections?_sort=sorting:ASC&_locale=' + lang).then((response: { data: any[] }) => {
38+
loadSections(lang).then((sections: HApiSection[]) => {
4039
if (currentSection) {
4140
const otherSection = currentSection['localizations'].find((l: any) => {
4241
return l.locale === lang
4342
})
4443

45-
const newCurrentSection = response.data.find((s: any) => {
44+
const newCurrentSection = sections.find((s: any) => {
4645
return s['id'] === otherSection['id']
4746
})
4847

src/components/navigation/Navigation.tsx

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import React, { useEffect, useRef, useState } from 'react'
22
import { Link, useLocation } from 'react-router-dom'
3-
import { ChapterT } from '../../pages/section/components/Chapter'
43
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
5-
import { CalendarPageT } from '../../pages/calendar/CalendarPage'
64
import { useTranslation } from "react-i18next";
7-
import { StartPageT } from '../../pages/home/HomePage';
85
import { faBars, faCalendarDays, faFishFins, faSearch } from "@fortawesome/free-solid-svg-icons";
96
import './nav.less'
10-
import { SectionT } from "../../pages/section/SectionPage";
117
import { CHAPTER_NAV_UPDATED_EVENT } from "../../shared/constants";
8+
import { HApiCalendarPage, HApiChapter, HApiSection, HApiStartPage } from "../../apis/hering-api";
129

1310
type Props = {
14-
startPage: StartPageT
15-
calendarPage: CalendarPageT
16-
sections: SectionT[]
11+
startPage: HApiStartPage
12+
calendarPage: HApiCalendarPage
13+
sections: HApiSection[]
1714
}
1815

1916
function Navigation({ startPage, calendarPage, sections }: Props) {
@@ -63,9 +60,9 @@ function Navigation({ startPage, calendarPage, sections }: Props) {
6360
}
6461
}, [location]);
6562

66-
function chapterList(section: SectionT) {
63+
function chapterList(section: HApiSection) {
6764
const chapters = section.chapters
68-
const chapterItems = chapters.map((chapter: ChapterT) => {
65+
const chapterItems = chapters.map((chapter: HApiChapter) => {
6966
const isActive = currentChapterSlug === chapter.slug
7067
return <li key={chapter.slug_with_section} className="subMenu" onClick={handleToggle}>
7168
<Link to={chapter.slug_with_section} className={isActive ? 'active' : ''}>{chapter.menu_name}</Link>
@@ -77,7 +74,7 @@ function Navigation({ startPage, calendarPage, sections }: Props) {
7774
</ul>
7875
}
7976

80-
const sectionList = sections.map((section: SectionT) => {
77+
const sectionList = sections.map((section: HApiSection) => {
8178
const isActive = location.pathname.replace('/', '') === section.slug
8279
const className = isActive ? 'active' : ''
8380
return <details key={section.slug} className={className} open={isActive}>

src/pages/calendar/CalendarPage.tsx

+6-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
import React, {useEffect} from 'react'
2-
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'
1+
import React, { useEffect } from 'react'
2+
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
33
import Markdown from 'react-markdown'
44
import remarkGfm from 'remark-gfm'
5-
import {LinkComponent} from '../../helper/MarkdownComponents'
5+
import { LinkComponent } from '../../helper/MarkdownComponents'
66
import CalendarForm from './components/CalendarForm'
7-
import {faCalendarDays} from "@fortawesome/free-solid-svg-icons"
7+
import { faCalendarDays } from "@fortawesome/free-solid-svg-icons"
88
import './calendar.less'
9-
import {IconT} from "../../shared/types";
10-
11-
export type CalendarPageT = {
12-
title: string
13-
menu_name: string
14-
icon: IconT
15-
content: string
16-
}
9+
import { HApiCalendarPage } from "../../apis/hering-api";
1710

1811
type Props = {
19-
page: CalendarPageT
12+
page: HApiCalendarPage
2013
}
2114

2215
function CalendarPage(props: Props) {

0 commit comments

Comments
 (0)