|
1 | | -import React, { useEffect, useState } from "react"; |
2 | | -import logo from "./commit-logo.png"; |
3 | | -import "./App.css"; |
| 1 | +import React from 'react' |
| 2 | +import { |
| 3 | + BrowserRouter as Router, |
| 4 | + Switch, |
| 5 | + Route, |
| 6 | + Redirect, |
| 7 | +} from 'react-router-dom' |
| 8 | +import Home from './pages/Home' |
| 9 | +import Login from './pages/Login' |
| 10 | +import PageNotFound from './pages/PageNotFound' |
4 | 11 |
|
5 | | -import "./components/Info"; |
6 | | -import InfoPanel from "./components/Info"; |
| 12 | +import './App.css' |
| 13 | +import Header from './components/Header' |
7 | 14 |
|
8 | | -// Set config based on the environment variable the build was run under. |
9 | | -let config = {}; |
10 | | -if (process.env.REACT_APP_CONFIG === "production") { |
11 | | - config = require("./config/production.json"); |
12 | | -} else if (process.env.REACT_APP_CONFIG === "staging") { |
13 | | - config = require("./config/staging.json"); |
14 | | -} else { |
15 | | - config = require("./config/development.json"); |
| 15 | +// A wrapper for <Route> that redirects to the login |
| 16 | +// screen if you're not yet authenticated. |
| 17 | +function PrivateRoute({ children, ...rest }) { |
| 18 | + // TODO: perform actual check |
| 19 | + const isAuthenticated = false |
| 20 | + return ( |
| 21 | + <Route |
| 22 | + {...rest} |
| 23 | + render={({ location }) => |
| 24 | + isAuthenticated ? ( |
| 25 | + children |
| 26 | + ) : ( |
| 27 | + <Redirect |
| 28 | + to={{ |
| 29 | + pathname: '/login', |
| 30 | + state: { from: location }, |
| 31 | + }} |
| 32 | + /> |
| 33 | + ) |
| 34 | + } |
| 35 | + /> |
| 36 | + ) |
16 | 37 | } |
17 | 38 |
|
18 | 39 | function App() { |
19 | | - const [data, setData] = useState({ |
20 | | - info: {}, |
21 | | - error: null, |
22 | | - }); |
23 | | - |
24 | | - const [status, setStatus] = useState({ |
25 | | - code: "Checking...", |
26 | | - }) |
27 | | - |
28 | | - useEffect(() => { |
29 | | - fetch(`${config.backendURL}/status/about`) |
30 | | - .then(result => { |
31 | | - setStatus({ |
32 | | - code: result.status, |
33 | | - }) |
34 | | - return result.json() |
35 | | - }) |
36 | | - .then(data => { |
37 | | - setData({ |
38 | | - info: data, |
39 | | - error: null |
40 | | - }) |
41 | | - }) |
42 | | - .catch(error => { |
43 | | - setData({ |
44 | | - info: {}, |
45 | | - error: error |
46 | | - }) |
47 | | - }); |
48 | | - }, []); |
49 | | - |
50 | 40 | return ( |
51 | | - <div className="App"> |
52 | | - <header className="App-header"> |
53 | | - <img src={logo} className="App-logo" alt="logo" /> |
54 | | - <h1>zero</h1> |
55 | | - <InfoPanel data={data} status={status} config={config} /> |
56 | | - </header> |
57 | | - </div> |
58 | | - ); |
| 41 | + <Router> |
| 42 | + <div className="App"> |
| 43 | + <Header /> |
| 44 | + |
| 45 | + <Switch> |
| 46 | + <Route exact path="/"> |
| 47 | + <Home /> |
| 48 | + </Route> |
| 49 | + <Route path="/login"> |
| 50 | + <Login /> |
| 51 | + </Route> |
| 52 | + <Route path="*"> |
| 53 | + <PageNotFound /> |
| 54 | + </Route> |
| 55 | + </Switch> |
| 56 | + </div> |
| 57 | + </Router> |
| 58 | + ) |
59 | 59 | } |
60 | 60 |
|
61 | | -export default App; |
| 61 | +export default App |
0 commit comments