-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
184 lines (167 loc) · 3.64 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
import React from 'react';
import PropTypes from 'prop-types';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import { Link, NavLink } from '../lib';
import I18n from './I18n';
const base = '/:locale(en|fr|pig)?';
const stripLocale = (pathname, locale) => {
if (!locale) {
return pathname;
}
return pathname.replace(`/${locale}`, '');
};
const Header = ({
location: { pathname },
match: { params: { locale } },
history,
}) => (
<ul className="header" history={history}>
<li>
<NavLink to="/">
<I18n t="home" />
</NavLink>
</li>
<li>
<NavLink to={{ pathname: '/about' }}>
<I18n t="about" />
</NavLink>
</li>
<li>
<NavLink to={(location) => ({ ...location, pathname: '/topics' })}>
<I18n t="topics" />
</NavLink>
</li>
<br />
<li>
<NavLink ignoreLocale to={`/fr${stripLocale(pathname, locale)}`}>
<I18n t="french">
Français
</I18n>
</NavLink>
</li>
<li>
<NavLink ignoreLocale to={`/en${stripLocale(pathname, locale)}`}>
<I18n t="english">
English
</I18n>
</NavLink>
</li>
<li>
<NavLink ignoreLocale to={`/pig${stripLocale(pathname, locale)}`}>
<I18n t="piglatin">
Pig Latin
</I18n>
</NavLink>
</li>
</ul>
);
Header.propTypes = {
location: PropTypes.shape({
pathname: PropTypes.string,
}).isRequired,
match: PropTypes.shape({
params: PropTypes.shape({
locale: PropTypes.string,
}).isRequired,
}).isRequired,
// eslint-disable-next-line react/forbid-prop-types
history: PropTypes.object.isRequired,
};
const Home = () => (
<>
<h2>
<I18n t="homePage.title" />
</h2>
<p>
<I18n t="homePage.text">
This is the homepage
</I18n>
</p>
</>
);
const Topic = ({ match: { params: { topicId } } }) => (
<>
<h3>
<I18n t={topicId} />
</h3>
</>
);
Topic.propTypes = {
match: PropTypes.shape({
params: PropTypes.shape({
topicId: PropTypes.string,
}).isRequired,
}).isRequired,
};
const About = () => (
<>
<h2>
<I18n t="aboutPage.title" />
</h2>
<p>
<I18n t="aboutPage.text">
About the project
</I18n>
</p>
</>
);
const Topics = ({ match: { url, path } }) => (
<>
<h2>
<I18n t="topics" />
</h2>
<ul>
<li>
<Link ignoreLocale to={`${url}/rendering`}>
<I18n t="topicPage.rendering">
Rendering with React
</I18n>
</Link>
</li>
<li>
<Link ignoreLocale to={`${url}/components`}>
<I18n t="topicPage.components">
Components
</I18n>
</Link>
</li>
<li>
<Link ignoreLocale to={`${url}/props_v_state`}>
<I18n t="topicPage.props">
Props v. State
</I18n>
</Link>
</li>
</ul>
<Route path={`${path}/:topicId`} component={Topic} />
<Route
exact
path={path}
render={() => (
<h3>
{' '}
<I18n t="topicPage.select"> Please select a topic. </I18n>
{' '}
</h3>
)}
/>
</>
);
Topics.propTypes = {
match: PropTypes.shape({
url: PropTypes.string,
path: PropTypes.string,
}).isRequired,
};
const App = () => (
<Router basename={process.env.PUBLIC_URL}>
<>
<Route path={base} component={Header} />
<hr />
<Route exact path={base} component={Home} />
<Route path={`${base}/about`} component={About} />
<Route path={`${base}/topics`} component={Topics} />
</>
</Router>
);
export default App;