-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.jsx
101 lines (95 loc) · 3.63 KB
/
App.jsx
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
import React from 'react';
import PropTypes from 'prop-types';
import { Meteor } from 'meteor/meteor';
import 'semantic-ui-css/semantic.css';
import { Roles } from 'meteor/alanning:roles';
import { HashRouter as Router, Route, Switch, Redirect } from 'react-router-dom';
/* RE-USED IMPORTS */
import NavBar from '../components/NavBar';
import Footer from '../components/Footer';
import Landing from '../pages/Landing';
import Signin from '../pages/Signin';
import Signup from '../pages/Signup';
import Signout from '../pages/Signout';
import NotFound from '../pages/NotFound';
import AboutUs from '../pages/AboutUs';
/* NEW IMPORTS */
import AthleteProfile from '../pages/AthleteProfile';
import AdminAthleteProfile from '../pages/AdminAthleteProfile';
import EditProfile from '../pages/EditProfile';
import AddVisit from '../pages/AddVisit';
import ProfileList from '../pages/ProfileList';
/** Top-level layout component for this application. Called in imports/startup/client/startup.jsx. */
class App extends React.Component {
render() {
return (
<Router>
<div>
<NavBar/>
<Switch>
{/* RE-USED PATHS */}
<Route exact path="/" component={Landing}/>
<Route path="/signin" component={Signin}/>
<Route path="/signup" component={Signup}/>
<Route path="/signout" component={Signout}/>
{/* NEW PATHS */}
<ProtectedRoute path="/profile" component={AthleteProfile}/>
<AdminProtectedRoute path="/admin-profile/:_id" component={AdminAthleteProfile}/>
<AdminProtectedRoute path="/edit-profile/:_id" component={EditProfile}/>
<AdminProtectedRoute path="/add-visit/:_id" component={AddVisit}/>
<AdminProtectedRoute path="/profile-list" component={ProfileList}/>
<Route path="/about" component={AboutUs}/>
<Route component={NotFound}/>
</Switch>
<Footer/>
</div>
</Router>
);
}
}
/**
* ProtectedRoute (see React Router v4 sample)
* Checks for Meteor login before routing to the requested page, otherwise goes to signin page.
* @param {any} { component: Component, ...rest }
*/
const ProtectedRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={(props) => {
const isLogged = Meteor.userId() !== null;
return isLogged ?
(<Component {...props} />) :
(<Redirect to={{ pathname: '/signin', state: { from: props.location } }}/>
);
}}
/>
);
/**
* AdminProtectedRoute (see React Router v4 sample)
* Checks for Meteor login and admin role before routing to the requested page, otherwise goes to signin page.
* @param {any} { component: Component, ...rest }
*/
const AdminProtectedRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={(props) => {
const isLogged = Meteor.userId() !== null;
const isAdmin = Roles.userIsInRole(Meteor.userId(), 'admin');
return (isLogged && isAdmin) ?
(<Component {...props} />) :
(<Redirect to={{ pathname: '/signin', state: { from: props.location } }}/>
);
}}
/>
);
/** Require a component and location to be passed to each ProtectedRoute. */
ProtectedRoute.propTypes = {
component: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
location: PropTypes.object,
};
/** Require a component and location to be passed to each AdminProtectedRoute. */
AdminProtectedRoute.propTypes = {
component: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
location: PropTypes.object,
};
export default App;