-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
164 lines (151 loc) · 4.31 KB
/
index.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
import React from 'react';
import { BrowserRouter, Route, Switch, Redirect } from 'react-router-dom';
import * as firebase from 'firebase/app';
import 'firebase/auth';
import { hot } from 'react-hot-loader/root';
import {
SignInPage,
SignUpPage,
PointsPage,
InducteePointsPage,
ResumePage,
EventsPage,
CalendarPage,
EventEditPage,
EventDetailsPage,
EventSignInPage,
EventRsvpPage,
} from '@Pages';
import { Loading } from '@SharedComponents';
import { UserContext } from '@Contexts';
import * as ROUTES from '@Constants/routes';
import { getRolesFromClaims } from '@Services/claims';
import {
InducteeRoutingPermission,
OfficerRoutingPermission,
} from '@HOCs/RoutingPermissions';
import ApiConfigStore from '@Services/ApiConfigStore';
const INITIAL_STATES = {
userClaims: null,
userToken: null,
isLoading: true,
};
class App extends React.Component {
constructor(props) {
super(props);
this.state = { ...INITIAL_STATES };
}
componentDidMount() {
firebase.auth().onAuthStateChanged(async user => {
if (user) {
const tokenResult = await user.getIdTokenResult();
const { claims, token } = tokenResult;
this.setState({
userClaims: {
userId: claims.user_id,
userRoles: getRolesFromClaims(claims),
},
userToken: token,
isLoading: false,
});
} else {
this.setState({
userClaims: null,
userToken: null,
isLoading: false,
});
}
});
}
setClaims = claims => {
const { userToken } = this.state;
ApiConfigStore.setToken(userToken);
this.setState({
userClaims: {
userId: claims.user_id,
userRoles: getRolesFromClaims(claims),
},
});
};
render() {
const { userClaims, isLoading } = this.state;
if (isLoading) {
return <Loading />;
}
return (
<UserContext.Provider value={userClaims}>
<BrowserRouter>
<Switch>
<Route
exact
path={ROUTES.SIGN_IN}
render={() => <SignInPage setClaims={this.setClaims} />}
/>
<Route exact path={ROUTES.SIGN_UP} render={() => <SignUpPage />} />
<Route
exact
path={ROUTES.EVENT_SIGN_IN}
render={props => <EventSignInPage {...props} />}
/>
<Route
exact
path={ROUTES.EVENT_RSVP}
render={props => <EventRsvpPage {...props} />}
/>
<Route
exact
path={ROUTES.HOME}
render={props => InducteeRoutingPermission(EventsPage)(props)}
/>
<Route
exact
path={ROUTES.POINTS}
render={props => InducteeRoutingPermission(PointsPage)(props)}
/>
<Route
exact
path={ROUTES.RESUME}
render={props => InducteeRoutingPermission(ResumePage)(props)}
/>
<Route
exact
path={ROUTES.INDUCTEES}
render={props =>
OfficerRoutingPermission(InducteePointsPage)(props)
}
/>
<Route
exact
path={ROUTES.CALENDAR}
render={props => InducteeRoutingPermission(CalendarPage)(props)}
/>
<Route
exact
path={ROUTES.EVENT_DETAILS}
render={props =>
InducteeRoutingPermission(EventDetailsPage)(props)
}
/>
<Route
exact
path={ROUTES.EVENT_EDIT}
render={props => OfficerRoutingPermission(EventEditPage)(props)}
/>
{/* <Route
exact
path={ROUTES.PROFILE}
render={props => InducteeRoutingPermission(ProfilePage)(props)}
/>
<Route
exact
path={ROUTES.PROFILE_EDIT}
render={props => InducteeRoutingPermission(ProfileEditPage)(props)}
/> */}
<Route render={() => <Redirect to={ROUTES.HOME} />} />
</Switch>
</BrowserRouter>
</UserContext.Provider>
);
}
}
export default process.env.NODE_ENV === 'development' ? hot(App) : App;