-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
102 lines (92 loc) 路 2.17 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
/* eslint-disable quotes */
import React, { useEffect, useState } from 'react';
import styled from 'styled-components';
import fetch from 'isomorphic-unfetch';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Home from './views/Home';
import About from './views/About';
import Blog from './views/Blog';
import User from './views/User';
import NotFound from './views/404';
import Global from './style/global';
import Reset from './style/reset';
import { blogData } from './util/consts';
import { colors } from './style/consts';
import endpoints from './util/endpoints';
const Wrapper = styled.div`
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: #fafafa;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 1.6rem;
p {
margin-bottom: 1.2rem;
}
a button {
background: ${colors.grey};
line-height: 200%;
border: none;
border-radius: 0.4rem;
padding: 0.4rem 1.2rem;
}
a {
margin-right: 0.8rem;
margin-bottom: 0.8rem;
&:last-child {
margin: 0;
}
}
`;
export default function App() {
const [dynamicData, setDynamicData] = useState([]);
useEffect(() => {
fetchDynamicData();
}, []);
function getStaticPages() {
return blogData.map((page, index) => {
return (
<Route key={index} path={`/blog/${page.slug}`}>
<Blog {...page} />
</Route>
);
});
}
function fetchDynamicData() {
fetch(endpoints.users)
.then(res => res.json())
.then(data => setDynamicData(data));
}
function getDynamicPages() {
if (!dynamicData) return;
return dynamicData.map((r, i) => (
<Route key={i} path={`/user/${r.id}`}>
<User {...r} />
</Route>
));
}
return (
<Wrapper>
<Global />
<Reset />
<Router>
<Switch>
<Route exact path="/">
<Home />
</Route>
<Route path="/about">
<About />
</Route>
{getDynamicPages()}
{getStaticPages()}
<Route path="*" component={NotFound} />
</Switch>
</Router>
</Wrapper>
);
}