Skip to content

Commit 8380d0f

Browse files
author
Turbo
committed
Preload data for Posts and Books, fix language
1 parent 9d5879f commit 8380d0f

File tree

19 files changed

+153
-97
lines changed

19 files changed

+153
-97
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"@babel/preset-react": "^7.0.0",
2727
"@babel/register": "^7.0.0",
2828
"@babel/runtime": "^7.2.0",
29-
"accepts": "^1.3.5",
3029
"babel-loader": "^8.0.4",
3130
"babel-plugin-inline-dotenv": "^1.2.0",
3231
"babel-plugin-module-resolver": "^3.1.1",

src/App/AppWrapper.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@ const AppWrapper = ({ lang }) => (
1515
</ThemeProvider>
1616
);
1717

18-
AppWrapper.defaultProps = {
19-
lang: 'en',
20-
};
21-
2218
AppWrapper.propTypes = {
23-
lang: PropTypes.string,
19+
lang: PropTypes.string.isRequired,
2420
};
2521

2622
export default AppWrapper;

src/App/AppRoot.js renamed to src/App/Client.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React from 'react';
33
import { BrowserRouter as Router } from 'react-router-dom';
44
import AppWrapper from 'src/App/AppWrapper';
55
import ScrollToTop from 'src/Components/ScrollToTop';
6+
import { getLocaleOnClient } from 'src/i18n/helpers';
67

78
/* eslint-disable no-restricted-globals */
89
export default class extends React.Component {
@@ -12,13 +13,13 @@ export default class extends React.Component {
1213
}
1314

1415
render() {
15-
const isEn = location.pathname.substr(1, 2) === 'en' && 'en';
16-
const isVi = location.pathname.substr(1, 2) === 'vi' && 'vi';
17-
const currentLang = isEn || isVi || 'en';
16+
// Determine current language by name
17+
const currentLang = getLocaleOnClient(location);
18+
1819
return (
1920
<Router>
2021
<ScrollToTop>
21-
<AppWrapper lang={navigator && currentLang} />
22+
<AppWrapper lang={currentLang} />
2223
</ScrollToTop>
2324
</Router>
2425
);

src/App/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default ({ lang }) => (
4747
/>
4848

4949
{/* Define Redirect logic if any */}
50-
<RedirectWithStatus httpStatus={301} exact from="/:lang" to={`/${lang}`} />
50+
<RedirectWithStatus httpStatus={301} from="/:lang" to={`/${lang}`} />
5151
<RedirectWithStatus httpStatus={301} from="/:lang/users" to="/" />
5252
<RedirectWithStatus httpStatus={302} from="/:lang/courses" to="/:lang/404" />
5353

@@ -56,6 +56,5 @@ export default ({ lang }) => (
5656
</Switch>
5757

5858
<Footer />
59-
{/* Footer here */}
6059
</React.Fragment>
6160
);

src/Pages/Books/index.js

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import loadData from 'src/helpers/loadData';
3+
import { fetchAllBooks } from 'src/helpers/loadData';
44
import Head from 'src/Components/Head';
5-
import Footer from 'src/Components/Footer';
5+
import FlexBox from 'src/Components/FlexBox';
6+
import { H3 } from 'src/Components/Typo';
67

7-
class Books extends React.Component {
8-
constructor(props) {
9-
super(props);
10-
11-
// Check if staticContext exists
12-
// because it will be undefined if rendered through a BrowserRouter
13-
this.state = (props.staticContext && props.staticContext.data) ? {
14-
data: props.staticContext.data,
15-
} : { data: [] };
16-
}
178

18-
componentDidMount() {
19-
setTimeout(() => {
20-
if (window.ROUTE_LOADED_DATA) {
9+
class Books extends React.Component {
10+
// Check if staticContext exists
11+
// because it will be undefined if rendered through a BrowserRouter
12+
state = {
13+
books: (this.props.staticContext && this.props.staticContext.data)
14+
? this.props.staticContext.data : [],
15+
};
16+
17+
async componentDidMount() {
18+
if (window.ROUTE_LOADED_DATA) {
19+
// console.log('Data preloaded');
20+
this.setState({
21+
books: window.ROUTE_LOADED_DATA,
22+
});
23+
delete window.ROUTE_LOADED_DATA;
24+
} else {
25+
// console.log('Data not preloaded. Fetching...');
26+
await fetchAllBooks().then((data) => {
2127
this.setState({
22-
data: window.ROUTE_LOADED_DATA,
23-
});
24-
delete window.ROUTE_LOADED_DATA;
25-
} else {
26-
loadData('posts').then((data) => {
27-
this.setState({
28-
data,
29-
});
28+
books: data,
3029
});
31-
}
32-
}, 0);
30+
});
31+
}
3332
}
3433

3534
render() {
36-
const { data } = this.state;
35+
const { books } = this.state;
3736
return (
3837
<React.Fragment>
3938
<Head title="Books" />
40-
<ul>{data.map(post => <li key={post.id}>{post.title}</li>)}</ul>
41-
<Footer />
39+
<FlexBox width={1/2} mx="auto" flexDirection="column" alignItems="flex-start">
40+
{
41+
books.map(book => <H3 key={book.id}>{book.title}</H3>)
42+
}
43+
</FlexBox>
4244
</React.Fragment>
4345
);
4446
}

src/Pages/Posts/index.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { Link } from 'react-router-dom';
33
import PropTypes from 'prop-types';
44

55
import truncate from 'src/helpers/truncate';
6+
import { fetchAllPosts } from 'src/helpers/loadData';
7+
68
import FlexBox from 'src/Components/FlexBox';
79
import CardBox from 'src/Components/CardBox';
810
import { H3, Span } from 'src/Components/Typo';
@@ -12,21 +14,30 @@ import Badge from 'src/Components/Badge';
1214
// Import all files inside `data` via Webpack require.context
1315
// Read more:
1416
// https://goo.gl/315fi3 - Importing Multiple Markdown files into a React Component with Webpack
15-
const postContext = require.context('../../../content', false, /\.md$/);
16-
const postFiles = postContext
17-
.keys()
18-
.map(filename => postContext(filename));
19-
2017
class Posts extends React.Component {
2118
state = {
22-
posts: [],
23-
lang: 'en',
19+
posts: (this.props.staticContext && this.props.staticContext.data)
20+
? this.props.staticContext.data : [],
2421
}
2522

26-
componentDidMount() {
27-
const posts = postFiles;
23+
async componentDidMount() {
2824
const { lang } = this.props.match.params;
29-
this.setState(state => ({ ...state, posts, lang }));
25+
26+
if (window.ROUTE_LOADED_DATA) {
27+
// console.log('Data preloaded');
28+
this.setState({
29+
posts: window.ROUTE_LOADED_DATA,
30+
});
31+
delete window.ROUTE_LOADED_DATA;
32+
} else {
33+
// console.log('Data not preloaded. Fetching...');
34+
await fetchAllPosts().then((data) => {
35+
this.setState({
36+
lang,
37+
posts: data,
38+
});
39+
});
40+
}
3041
}
3142

3243
render() {
@@ -46,8 +57,8 @@ class Posts extends React.Component {
4657
>
4758
{
4859
posts.map((post, i) => (
49-
<CardBox width={[1, 1/3]} px={[1, 2, 3]} py={[0, 1, 2]} mx={[1, 2, 3]}>
50-
<Link key={i} to={`/${lang}/posts/${post.slug}`}>
60+
<CardBox key={i} width={[1, 1/3]} px={[1, 2, 3]} py={[0, 1, 2]} mx={[1, 2, 3]}>
61+
<Link to={`/${lang}/posts/${post.slug}`}>
5162
<H3 dangerouslySetInnerHTML={{ __html: post.title }} />
5263
</Link>
5364
<Span dangerouslySetInnerHTML={{ __html: truncate(post.__content) }} />
@@ -71,6 +82,14 @@ Posts.propTypes = {
7182
lang: PropTypes.string.isRequired,
7283
}),
7384
}).isRequired,
85+
86+
staticContext: PropTypes.shape({
87+
data: PropTypes.array,
88+
}),
89+
};
90+
91+
Posts.defaultProps = {
92+
staticContext: {},
7493
};
7594

7695
export default Posts;

src/client-locale/constants.js

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

src/client-locale/extractLocalesFromReq.js

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

src/client-locale/guessLocale.js

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

src/helpers/getCookie.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const getCookie = (name) => {
2+
const match = document.cookie.match(new RegExp(`(^| )${name}=([^;]+)`));
3+
return (match) ? match[2] : null;
4+
};
5+
6+
export default getCookie;

0 commit comments

Comments
 (0)