-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
303 lines (282 loc) · 7.98 KB
/
App.js
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import { Router, Switch, Route } from "react-router-dom";
import React, { useState } from "react";
import ReactGA from 'react-ga';
import { createBrowserHistory } from 'history';
import { gql, useQuery } from "@apollo/client";
import get from "lodash.get";
import Post from "./Post";
import { getUrlFromMappingByPathName } from "./utils";
import LandingPage from "./LandingPage";
import ListingPage from "./ListingPage";
import SimplePage from "./SimplePage";
import { UnknownComponent } from "./components";
import {
actionFields,
assetFields,
seoFields,
subpageNavigationItemFields,
} from "./graphQLFragments";
import GraphQLLoader from "./components/GraphQLLoader";
import getSeo from "./utils/getSeo";
import { getListingPaginationAndFilter } from "./utils/queryString";
export default function App(props) {
const homePageQuery = gql`
query HomePageQuery($codename: String!) {
post_All {
items {
slug
_system_ {
codename
type {
_system_ {
codename
}
}
}
}
}
homepage(codename: $codename) {
content {
... on LandingPage {
_system_ {
codename
type {
_system_ {
codename
}
}
}
}
}
_seo {
...SeoFields
}
headerLogo {
...AssetFields
}
title
favicon {
url
}
font {
items {
_system_ {
codename
}
}
}
palette {
items {
_system_ {
codename
}
}
}
mainMenu {
... on Menu {
_system_ {
codename
}
actions {
items {
... on Action {
...ActionFields
}
}
}
}
}
subpages {
items {
... on NavigationItem {
...SubpageNavigationItemFields
subpages {
items {
...SubpageNavigationItemFields
}
}
}
}
}
}
}
${seoFields}
${assetFields}
${actionFields}
${subpageNavigationItemFields}
`;
const getNavigationData = (parrentSlug, item) => {
if (item._system_?.type?._system_.codename === "post") {
return {
slug: parrentSlug.concat([item.slug]),
navigationType: "post",
navigationCodename: item._system_?.codename,
contentCodename: item._system_?.codename,
contentType: item._system_?.type._system_.codename,
};
}
return {
slug: parrentSlug.concat([item.slug]),
navigationType: "navigationItem",
navigationCodename: item._system_?.codename,
contentCodename: item.content._system_.codename,
contentType: item.content._system_.type._system_.codename,
};
};
const homepageCodename = "homepage";
const getMappings = (data) => {
const mappings = [
{
slug: [],
navigationCodename: homepageCodename,
navigationType: "homepage",
contentCodename: data.homepage.content._system_.codename,
contentType: data.homepage.content._system_.type._system_.codename,
},
];
data.homepage.subpages.items.forEach((item) => {
const navigationData = getNavigationData([], item);
mappings.push(navigationData);
mappings.push(
...item.subpages.items.map((subItem) =>
getNavigationData(navigationData.slug, subItem)
)
);
const content = item.content;
if (content._system_.type._system_.codename === "listing_page") {
const listingData = data[`${content.contentType}_All`];
if (!listingData) {
console.error(
`Unknown listing page content type: ${content.contentType}`
);
} else {
mappings.push(
...listingData.items.map((subItem) =>
getNavigationData(navigationData.slug, subItem)
)
);
}
}
});
return mappings.reduce((result, item) => {
result[[].concat(item.slug).join("/")] = {
navigationCodename: item.navigationCodename,
navigationType: item.navigationType,
contentCodename: item.contentCodename,
contentType: item.contentType,
};
return result;
}, {});
};
const getSiteConfiguration = (data) => {
return {
asset: get(data, "homepage.headerLogo", null),
title: get(data, "homepage.title", ""),
mainMenuActions: get(
data,
"homepage.mainMenu.actions.items",
[]
),
favicon: get(data, "homepage.favicon.url", null),
font: get(data, "homepage.font.items[0]._system_.codename", null),
palette: get(data, "homepage.palette.items[0]._system_.codename", null),
};
};
const { loading, error } = useQuery(homePageQuery, {
variables: { codename: homepageCodename },
onCompleted: (data) => {
const mappings = getMappings(data);
const siteConfiguration = getSiteConfiguration(data);
setMappings(mappings);
setSiteConfiguration(siteConfiguration);
setHomepageSeo(getSeo(data.homepage._seo));
},
});
const [mappings, setMappings] = useState(null);
const [siteConfiguration, setSiteConfiguration] = useState(null);
const [homepageSeo, setHomepageSeo] = useState(null);
if (error || loading || !mappings || !siteConfiguration || !homepageSeo) {
return <GraphQLLoader error={error} loading={loading} />;
}
const history = createBrowserHistory();
if (props.initializeAnalytics) {
history.listen(location => {
ReactGA.set({ page: location.pathname });
ReactGA.pageview(location.pathname);
});
}
return (
<Router history={history} basename={process.env.PUBLIC_URL}>
<Switch>
<Route path="/" render={renderPage} />
</Switch>
</Router>
);
function renderPage({ location }) {
const navigationItem = getUrlFromMappingByPathName(mappings, location.pathname);
if (!navigationItem) {
if (process.env.NODE_ENV === "development") {
console.error(`Unknown navigation item pathname: ${location.pathname}`);
return (
<div>
<h2>Not found</h2>
<pre>{JSON.stringify(mappings, undefined, 2)}</pre>
</div>
);
}
return <h2>Not found</h2>;
}
const pageProps = {
siteConfiguration,
mappings,
};
if (navigationItem.navigationType === "homepage") {
pageProps["seo"] = homepageSeo;
pageProps["codename"] = navigationItem.contentCodename;
}
switch (navigationItem.contentType) {
case "landing_page":
return (
<LandingPage
{...pageProps}
codename={
pageProps["codename"] || navigationItem.navigationCodename
}
/>
);
case "listing_page":
return (
<ListingPage
{...pageProps}
codename={navigationItem.navigationCodename}
{...getListingPaginationAndFilter(location)}
/>
);
case "simple_page":
return (
<SimplePage
{...pageProps}
codename={
pageProps["codename"] || navigationItem.navigationCodename
}
/>
);
case "post":
return (
<Post {...pageProps} codename={navigationItem.contentCodename} />
);
default:
if (process.env.NODE_ENV === "development") {
console.error(
`Unknown navigation item content type: ${navigationItem.contentType}`
);
return (
<UnknownComponent>
<pre>{JSON.stringify(mappings, undefined, 2)}</pre>
</UnknownComponent>
);
}
return null;
}
}
}