@@ -2,14 +2,15 @@ import React from 'react';
22import PropTypes from 'prop-types' ;
33import { Switch , Route , Redirect } from 'react-router-dom' ;
44import DUMMY_DATA from './DUMMY_DATA' ;
5+ import { tokenServices , ProtectedRoute } from './utils' ;
56import {
67 NavBar ,
78 HomePage ,
89 PathPage ,
910 LessonPage ,
1011 CatalogPage ,
11- // RegistrationPage,
12- // DashboardPage,
12+ RegistrationPage ,
13+ DashboardPage ,
1314 SupportPage ,
1415 AboutPage ,
1516 ErrorPage ,
@@ -27,54 +28,115 @@ const links = {
2728 dashboard : '/dashboard' ,
2829} ;
2930
30- function App ( ) {
31- const { courses } = DUMMY_DATA ;
31+ class App extends React . Component {
32+ constructor ( props ) {
33+ super ( props ) ;
34+ this . state = {
35+ user : null ,
36+ isAuthenticated : false ,
37+ } ;
38+ }
3239
33- return (
34- < div id = "start-page" >
35- < NavBar links = { links } />
36- < Switch >
37- < Route exact path = "/" component = { HomePage } />
38- < Route
39- exact
40- path = { links . path }
41- render = { props => < PathPage { ...props } path_data = { DUMMY_DATA } /> }
42- />
43- < Route exact path = { links . catalog } component = { CatalogPage } />
44- { /* <Route exact path={links.login} component={RegistrationPage} /> */ }
45- { /* <Route exact path={links.dashboard} component={DashboardPage} /> */ }
46- < Route exact path = { links . support } component = { SupportPage } />
47- < Route exact path = { links . about } component = { AboutPage } />
40+ // componentWillMount = () => {
41+ // const { history, location } = this.props;
42+ // console.log(history);
43+ // console.log(location);
44+ // }
45+
46+ /**
47+ * get user data from database
48+ */
49+ componentDidMount = ( ) => {
50+ const user = tokenServices . getToken ( ) ;
51+ if ( user ) {
52+ this . setState ( { isAuthenticated : true , user } ) ;
53+ }
54+ } ;
55+
56+ handleLogin = ( ) => {
57+ const user = tokenServices . getToken ( ) ;
58+ if ( user ) {
59+ this . setState ( { isAuthenticated : true , user } ) ;
60+ } else {
61+ this . setState ( { isAuthenticated : null , user : null } ) ;
62+ }
63+ } ;
4864
49- < Redirect exact from = "/courses/:course" to = "/courses/:course/0" />
50- < Route
51- path = "/courses/:course/:order"
52- render = { props => {
53- const courseObj = courses . find (
54- course => course . slug === props . match . params . course ,
55- ) ;
56- const prevCourse = courses . find ( course => course . order === courseObj . order - 1 ) ;
57- const nextCourse = courses . find ( course => course . order === courseObj . order + 1 ) ;
58- const order = parseInt ( props . match . params . order , 10 ) ;
59- if ( ! courseObj || order >= courseObj . lessons . length ) return < ErrorPage /> ;
60- return (
61- < LessonPage
62- { ...props }
63- course_title = { courseObj . title }
64- lessons = { courseObj . lessons }
65- curr_lesson_num = { order }
66- base_path = { courseObj . slug }
67- prev_slug = { prevCourse ? prevCourse . slug : undefined }
68- next_slug = { nextCourse ? nextCourse . slug : undefined }
65+ handleLogoff = ( ) => {
66+ tokenServices . removeToken ( ) ;
67+ this . setState ( { isAuthenticated : false , user : null } ) ;
68+ } ;
6969
70- />
71- ) ;
72- } }
70+ render ( ) {
71+ const { courses } = DUMMY_DATA ;
72+ const { user, isAuthenticated } = this . state ;
73+
74+ return (
75+ < div id = "start-page" >
76+ < NavBar
77+ links = { links }
78+ isAuthenticated = { isAuthenticated }
79+ handleLogoff = { this . handleLogoff }
7380 />
74- < Route component = { ErrorPage } />
75- </ Switch >
76- </ div >
77- ) ;
81+ < Switch >
82+ < Route exact path = "/" component = { HomePage } />
83+ < Route
84+ exact
85+ path = { links . path }
86+ render = { props => < PathPage { ...props } path_data = { DUMMY_DATA } /> }
87+ />
88+ < Route exact path = { links . catalog } component = { CatalogPage } />
89+ { /* Login Protected Route */ }
90+ < ProtectedRoute
91+ path = { links . login }
92+ isAuthenticated = { ! isAuthenticated }
93+ redirectLink = { links . dashboard }
94+ component = { RegistrationPage }
95+ handleLogin = { this . handleLogin }
96+ />
97+ { /* Dashboard Protected Route */ }
98+ < ProtectedRoute
99+ path = { links . dashboard }
100+ isAuthenticated = { isAuthenticated }
101+ redirectLink = { links . login }
102+ component = { DashboardPage }
103+ />
104+ < Route exact path = { links . support } component = { SupportPage } />
105+ < Route exact path = { links . about } component = { AboutPage } />
106+
107+ < Redirect exact from = "/courses/:course" to = "/courses/:course/0" />
108+ < Route
109+ path = "/courses/:course/:order"
110+ render = { props => {
111+ const courseObj = courses . find (
112+ course => course . slug === props . match . params . course ,
113+ ) ;
114+ const prevCourse = courses . find (
115+ course => course . order === courseObj . order - 1 ,
116+ ) ;
117+ const nextCourse = courses . find (
118+ course => course . order === courseObj . order + 1 ,
119+ ) ;
120+ const order = parseInt ( props . match . params . order , 10 ) ;
121+ if ( ! courseObj || order >= courseObj . lessons . length ) return < ErrorPage /> ;
122+ return (
123+ < LessonPage
124+ { ...props }
125+ course_title = { courseObj . title }
126+ lessons = { courseObj . lessons }
127+ curr_lesson_num = { order }
128+ base_path = { courseObj . slug }
129+ prev_slug = { prevCourse ? prevCourse . slug : undefined }
130+ next_slug = { nextCourse ? nextCourse . slug : undefined }
131+ />
132+ ) ;
133+ } }
134+ />
135+ < Route component = { ErrorPage } />
136+ </ Switch >
137+ </ div >
138+ ) ;
139+ }
78140}
79141
80142App . propTypes = {
0 commit comments