Skip to content

Commit afef08b

Browse files
committed
[B][E] Ensure homepage content is SSR; prevent layout flash
1 parent c3d7aed commit afef08b

File tree

8 files changed

+132
-181
lines changed

8 files changed

+132
-181
lines changed

client/src/frontend/containers/Home/Collections.js

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import React from "react";
2+
import Feature from "./Feature";
3+
import CollectionNavigation from "frontend/components/CollectionNavigation";
4+
import { useFromStore } from "hooks";
5+
import useFetchHomepageContent from "./useFetchHomepageContent";
6+
import EntityCollection from "frontend/components/entity/Collection";
7+
import EntityCollectionPlaceholder from "global/components/entity/CollectionPlaceholder";
8+
9+
export default function Content() {
10+
const settings = useFromStore("settings", "select");
11+
12+
const { hasVisibleHomeProjectCollections, hasVisibleProjects } =
13+
settings?.attributes?.calculated ?? {};
14+
const showProjects = !hasVisibleHomeProjectCollections;
15+
16+
const {
17+
loaded,
18+
projects,
19+
collections,
20+
journals,
21+
features
22+
} = useFetchHomepageContent(showProjects);
23+
24+
if (!loaded) return null;
25+
26+
if (!projects?.length && !collections?.length && !journals?.length)
27+
return <EntityCollectionPlaceholder.Projects />;
28+
29+
const renderCollections = collections?.length
30+
? collections.map((projectCollection, i) => (
31+
<EntityCollection.ProjectCollectionSummary
32+
key={projectCollection.id}
33+
projectCollection={projectCollection}
34+
limit={projectCollection.attributes.homepageCount}
35+
bgColor={i % 2 === 0 ? "neutral05" : "white"}
36+
/>
37+
))
38+
: null;
39+
40+
const renderProjects = projects?.length ? (
41+
<EntityCollection.ProjectsSummary projects={projects} bgColor="neutral05" />
42+
) : null;
43+
44+
const count = showProjects ? 1 : collections?.length;
45+
46+
return (
47+
<>
48+
<Feature features={features} />
49+
{showProjects ? renderProjects : renderCollections}
50+
{!!journals?.length &&
51+
journals.map((journal, i) => (
52+
<EntityCollection.JournalSummary
53+
key={journal.id}
54+
journal={journal}
55+
bgColor={(count + i) % 2 === 0 ? "neutral05" : "white"}
56+
limit={8}
57+
/>
58+
))}
59+
{hasVisibleProjects && <CollectionNavigation />}
60+
</>
61+
);
62+
}

client/src/frontend/containers/Home/Feature.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
import React, { useMemo } from "react";
1+
import React from "react";
22
import PropTypes from "prop-types";
33
import Layout from "frontend/components/layout";
4-
import { featuresAPI } from "api";
5-
import { useFetch } from "hooks";
4+
import { useFromStore } from "hooks";
65
import { commonActions } from "actions/helpers";
76
import { useDispatch } from "react-redux";
87

9-
export default function HomeFeatureContainer({ authentication }) {
10-
const filters = useMemo(() => ({ home: true }), []);
11-
const { data: features } = useFetch({
12-
request: [featuresAPI.index, filters],
13-
withAuthDependency: true
14-
});
15-
8+
export default function HomeFeatureContainer({ features }) {
169
const dispatch = useDispatch();
1710
const actions = commonActions(dispatch);
1811

12+
const authentication = useFromStore("authentication");
13+
1914
return features?.length ? (
2015
<Layout.Splash
2116
feature={features[0]}

client/src/frontend/containers/Home/Projects.js

Lines changed: 0 additions & 51 deletions
This file was deleted.

client/src/frontend/containers/Home/__tests__/Home-test.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

client/src/frontend/containers/Home/__tests__/__snapshots__/Home-test.js.snap

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,15 @@
11
import React from "react";
22
import PropTypes from "prop-types";
3-
import Collections from "./Collections";
4-
import Projects from "./Projects";
5-
import Feature from "./Feature";
6-
import CollectionNavigation from "frontend/components/CollectionNavigation";
3+
import Content from "./Content";
74
import EventTracker, { EVENTS } from "global/components/EventTracker";
85
import HeadContent from "global/components/HeadContent";
9-
import { useFromStore } from "hooks";
10-
import { useLocation } from "react-router-dom";
116

127
export default function HomeContainer() {
13-
const settings = useFromStore("settings", "select");
14-
15-
const { hasVisibleHomeProjectCollections, hasVisibleProjects } =
16-
settings?.attributes?.calculated ?? {};
17-
const showProjects = !hasVisibleHomeProjectCollections;
18-
const authentication = useFromStore("authentication");
19-
const location = useLocation();
20-
218
return (
229
<>
2310
<HeadContent />
24-
<div
25-
style={{
26-
overflowX: "hidden"
27-
}}
28-
>
29-
<EventTracker event={EVENTS.VIEW_LIBRARY} />
30-
<Feature authentication={authentication} />
31-
{showProjects ? (
32-
<Projects location={location} authentication={authentication} />
33-
) : (
34-
<Collections />
35-
)}
36-
{hasVisibleProjects && <CollectionNavigation />}
37-
</div>
11+
<EventTracker event={EVENTS.VIEW_LIBRARY} />
12+
<Content />
3813
</>
3914
);
4015
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { useMemo } from "react";
2+
import { useFetch } from "hooks";
3+
import {
4+
projectCollectionsAPI,
5+
journalsAPI,
6+
projectsAPI,
7+
featuresAPI
8+
} from "api";
9+
10+
export default function useFetchHomepageContent(fetchProjects) {
11+
const collectionFilters = useMemo(
12+
() => ({
13+
visibleOnHomepage: true,
14+
order: "position ASC"
15+
}),
16+
[]
17+
);
18+
19+
const projectFilters = useMemo(
20+
() => ({
21+
standaloneModeEnforced: false,
22+
order: "sort_title, title"
23+
}),
24+
[]
25+
);
26+
27+
const journalFilters = useMemo(
28+
() => ({
29+
showOnHomepage: true
30+
}),
31+
[]
32+
);
33+
34+
const featuresFilters = useMemo(() => ({ home: true }), []);
35+
36+
const { data: projects, loaded: projectsLoaded } = useFetch({
37+
request: [projectsAPI.index, projectFilters, { number: 1, size: 20 }],
38+
condition: fetchProjects
39+
});
40+
const { data: collections, loaded: collectionsLoaded } = useFetch({
41+
request: [projectCollectionsAPI.index, collectionFilters],
42+
withAuthDependency: true,
43+
condition: !fetchProjects
44+
});
45+
46+
const { data: journals, loaded: journalsLoaded } = useFetch({
47+
request: [journalsAPI.index, journalFilters],
48+
withAuthDependency: true
49+
});
50+
51+
const { data: features, loaded: featuresLoaded } = useFetch({
52+
request: [featuresAPI.index, featuresFilters],
53+
withAuthDependency: true
54+
});
55+
56+
const loaded =
57+
journalsLoaded && featuresLoaded && fetchProjects
58+
? projectsLoaded
59+
: collectionsLoaded;
60+
61+
return { loaded, journals, projects, collections, features };
62+
}

0 commit comments

Comments
 (0)