-
Notifications
You must be signed in to change notification settings - Fork 56
/
App.tsx
203 lines (181 loc) · 5.37 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import {
HashRouter as Router,
Switch,
Route,
Redirect,
useLocation,
} from "react-router-dom";
import { Helmet } from "react-helmet";
import deepcopy from "ts-deepcopy";
import "./css/style.scss";
import "primereact/resources/primereact.min.css";
import "primeicons/primeicons.css";
import "./App.css";
import CmsList from "./CmsList";
import CmsCardList from "./CmsCardList";
import CmsDetailView from "./CmsDetailView";
import {
AppState,
Cms,
FilterFieldSet,
ReceivedCmsData,
ActivePreset,
SHOW_ALL,
} from "./Cms";
import CmsService from "./CmsService";
import FilterService from "./FilterService";
import Analytics from "./Analytics";
import About from "./About";
import { ErrorBoundary } from "./ErrorBoundary";
import Header from "./Header";
import SmallHeader from "./SmallHeader";
import Navigation from "./Navigation";
import Footer from "./Footer";
import FilterAside from "./FilterAside";
import FilterMenu from "./FilterMenu";
const ScrollToTop = (): null => {
const { pathname } = useLocation();
React.useEffect(() => {
window.scrollTo(0, 0);
}, [pathname]);
return null;
};
const App = (): JSX.Element => {
const [appState, setAppState] = React.useState<AppState>();
React.useEffect(() => {
CmsService.getCmsData().then((cmsData: ReceivedCmsData) => {
setAppState(constructAppState(cmsData));
});
}, []);
const updateFilterFields = (
updatedFilterFields: FilterFieldSet,
preset: ActivePreset
): void => {
if (appState) {
const updatedAppState = deepcopy<AppState>(appState);
updatedAppState.filterResults = FilterService.filterCms(
updatedFilterFields,
appState.cms
);
const filterFields = updatedAppState.filterFields;
filterFields.current = updatedFilterFields;
filterFields.activePreset = preset;
setAppState(updatedAppState);
}
};
const toggleAside = (): void => {
if (appState) {
setAppState({ ...appState, showAside: !appState.showAside });
}
if (!appState?.showAside) {
document.body.classList.add("sidebar-open");
} else {
document.body.classList.remove("sidebar-open");
}
};
const setCookiesAccepted = (cookiesAccepted: boolean): void => {
if (appState) {
setAppState({ ...appState, cookiesAccepted });
}
};
const githubUrl = "https://github.com/gentics/headless-cms-comparison";
const genticsUrl = "https://www.gentics.com/genticscms/index.en.html";
const content = appState ? (
<Router /* basename={process.env.PUBLIC_URL} */>
<ScrollToTop />
<Navigation />
<Switch>
<Route exact path="/">
<Redirect to="/card" />
</Route>
<Route exact path="/card">
<FilterAside
filterFields={appState.filterFields}
updateFilterFields={updateFilterFields}
showAside={appState.showAside}
toggleAside={toggleAside}
cookiesAccepted={appState.cookiesAccepted}
/>
<Header />
<FilterMenu
filterFields={appState.filterFields}
updateFilterFields={updateFilterFields}
toggleAside={toggleAside}
cookiesAccepted={appState.cookiesAccepted}
/>
<main>
<CmsCardList
filterResults={appState.filterResults}
cms={appState.cms}
/>
</main>
</Route>
<Route exact path="/list">
<SmallHeader title="List view" />
<main>
<CmsList
filterFields={appState.filterFields.current}
cmsData={appState.cms}
/>
</main>
</Route>
<Route path="/detail/:cmsKey">
<SmallHeader title={appState.cms} />
<main>
<CmsDetailView
filterFields={appState.filterFields.current}
filterResults={appState.filterResults}
cmsData={appState.cms}
/>
</main>
</Route>
<Route exact path="/about">
<SmallHeader title="About us" />
<main>
<About githubUrl={githubUrl} genticsUrl={genticsUrl} />
</main>
</Route>
</Switch>
<Footer genticsUrl={genticsUrl} />
<Analytics
accepted={appState.cookiesAccepted}
setAccepted={setCookiesAccepted}
/>
</Router>
) : null;
return (
<div className="App">
<Helmet
defaultTitle="Headless CMS Comparison"
titleTemplate="Headless CMS Comparison - %s"
/>
<ErrorBoundary>{content}</ErrorBoundary>
</div>
);
};
function constructAppState(cmsData: {
fields?: { [x: string]: any };
cms: { [x: string]: Cms };
}): AppState {
const filterFields: FilterFieldSet = { basic: {}, special: {} };
filterFields.basic = FilterService.initializeBasicFields(
cmsData.fields?.properties
);
filterFields.special = FilterService.initializeSpecialFields();
const untouchedFilterFields = deepcopy<FilterFieldSet>(filterFields);
const appState: AppState = {
cms: cmsData.cms,
filterFields: {
current: filterFields,
untouched: untouchedFilterFields,
activePreset: SHOW_ALL,
},
filterResults: FilterService.getUnfilteredCms(cmsData.cms),
showAside: false,
cookiesAccepted: false,
};
return appState;
}
export default App;