@@ -3,6 +3,8 @@ import { Link } from 'react-router-dom';
3
3
import PropTypes from 'prop-types' ;
4
4
5
5
import truncate from 'src/helpers/truncate' ;
6
+ import { fetchAllPosts } from 'src/helpers/loadData' ;
7
+
6
8
import FlexBox from 'src/Components/FlexBox' ;
7
9
import CardBox from 'src/Components/CardBox' ;
8
10
import { H3 , Span } from 'src/Components/Typo' ;
@@ -12,21 +14,30 @@ import Badge from 'src/Components/Badge';
12
14
// Import all files inside `data` via Webpack require.context
13
15
// Read more:
14
16
// https://goo.gl/315fi3 - Importing Multiple Markdown files into a React Component with Webpack
15
- const postContext = require . context ( '../../../content' , false , / \. m d $ / ) ;
16
- const postFiles = postContext
17
- . keys ( )
18
- . map ( filename => postContext ( filename ) ) ;
19
-
20
17
class Posts extends React . Component {
21
18
state = {
22
- posts : [ ] ,
23
- lang : 'en' ,
19
+ posts : ( this . props . staticContext && this . props . staticContext . data )
20
+ ? this . props . staticContext . data : [ ] ,
24
21
}
25
22
26
- componentDidMount ( ) {
27
- const posts = postFiles ;
23
+ async componentDidMount ( ) {
28
24
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
+ }
30
41
}
31
42
32
43
render ( ) {
@@ -46,8 +57,8 @@ class Posts extends React.Component {
46
57
>
47
58
{
48
59
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 } ` } >
51
62
< H3 dangerouslySetInnerHTML = { { __html : post . title } } />
52
63
</ Link >
53
64
< Span dangerouslySetInnerHTML = { { __html : truncate ( post . __content ) } } />
@@ -71,6 +82,14 @@ Posts.propTypes = {
71
82
lang : PropTypes . string . isRequired ,
72
83
} ) ,
73
84
} ) . isRequired ,
85
+
86
+ staticContext : PropTypes . shape ( {
87
+ data : PropTypes . array ,
88
+ } ) ,
89
+ } ;
90
+
91
+ Posts . defaultProps = {
92
+ staticContext : { } ,
74
93
} ;
75
94
76
95
export default Posts ;
0 commit comments