11import MainPage from "./pages/MainPage/MainPage" ;
22import { DashBoard } from "./templates/DashBoardTemplate/DashBoard" ;
33import { Redirect , Route , Router , Switch } from "react-router-dom" ;
4- import { createBrowserHistory } from "history"
4+ import { createBrowserHistory } from "history" ;
55import ModalHOC from "./HOC/Modal/ModalHOC" ;
66import ModalSelectPlugin from "./components/Modal/ModalSelectPlugin" ;
77import { PrivateRoutes } from "./Routes/PrivateRoutes" ;
88import checkAuth from "./utils/CheckAuth" ;
99import { PublicRoutes } from "./Routes/PublicRoutes" ;
1010import PublicLayout from "./templates/PublicLayout/PublicLayout" ;
11- import Profile from ' ./pages/Profile/Profile'
11+ import Profile from " ./pages/Profile/Profile" ;
1212import WebTour from "./pages/WebTour/WebTour" ;
1313import Admin from "./pages/Admin/Admin" ;
14- import ' ./styles/main.scss'
15- import ' antd/dist/antd.css' ;
14+ import " ./styles/main.scss" ;
15+ import " antd/dist/antd.css" ;
1616import { useSelector } from "react-redux" ;
1717import FirstTimeLogin from "./pages/FirstTimeLogin/FirstTimeLogin" ;
1818import ProtectedLayout from "./templates/ProtectedLayout/ProtectedLayout" ;
1919import LoginWithSocial from "./pages/Login/LoginWithSocial" ;
2020import Page404 from "./pages/Page404" ;
2121
22-
2322export const history = createBrowserHistory ( ) ;
2423
2524function App ( ) {
26-
2725 // TODO: custom hook => lưu cache
2826 // Attatactment: Luu cac ảnh ở trong này (Obj trên redux)
2927
3028 // cache dữ liệu bằng private router
3129
32-
33-
3430 // const isAuthenticated = checkAuth()
3531
3632 return (
@@ -39,47 +35,67 @@ function App() {
3935 < ModalSelectPlugin />
4036 < WebTour />
4137 < Switch >
42-
4338 { Object . keys ( PrivateRoutes ) . map ( ( item , index ) => {
44- const { Component, path } = PrivateRoutes [ item ]
39+ const { Component, path } = PrivateRoutes [ item ] ;
4540 // TODO: Thêm key={index} bị giật => why??
46- return < Route exact path = { path } render = { ( route ) => {
47- if ( checkAuth . getToken ( ) !== null && PrivateRoutes [ item ] . isDashboard ) {
48- return < DashBoard route = { route } ComponentRender = { Component } />
49- } else if ( checkAuth . getToken ( ) !== null && PrivateRoutes [ item ] . firstLogin ) {
50- return < PublicLayout route = { route } Component = { Component } />
51- } else if ( checkAuth . getToken ( ) !== null ) {
52- return < PublicLayout route = { route } Component = { Component } />
53- } else {
54- return < Redirect to = { '/login' } />
55- }
56- } } />
41+ return (
42+ < Route
43+ exact
44+ path = { path }
45+ render = { ( route ) => {
46+ if ( PrivateRoutes [ item ] . isDashboard ) {
47+ return (
48+ < DashBoard route = { route } ComponentRender = { Component } />
49+ ) ;
50+ } else if (
51+ checkAuth . getToken ( ) !== null &&
52+ PrivateRoutes [ item ] . firstLogin
53+ ) {
54+ return < PublicLayout route = { route } Component = { Component } /> ;
55+ } else if ( checkAuth . getToken ( ) !== null ) {
56+ return < PublicLayout route = { route } Component = { Component } /> ;
57+ } else {
58+ return < Redirect to = { "/login" } /> ;
59+ }
60+ } }
61+ />
62+ ) ;
5763 // return isAuthenticated !== null ? <Route key={path} exact path={path} render={(route) => {return <DashBoard route={route} ComponentRender={Component} /> }} /> : <Redirect key={index} to={'/login'} />
5864 } ) }
5965
6066 { Object . keys ( PublicRoutes ) . map ( ( item , index ) => {
61- const { Component, path } = PublicRoutes [ item ]
62- return < Route exact key = { path } path = { path } render = { ( route ) => {
63- if ( checkAuth . getToken ( ) === null && PublicRoutes [ item ] . tokenNeeded ) {
64- return < PublicLayout route = { route } Component = { Component } />
65- } else if ( ! PublicRoutes [ item ] . tokenNeeded ) {
66- return < PublicLayout route = { route } Component = { Component } />
67- } else {
68- return < Redirect to = { '/' } />
69- }
70- } } />
67+ const { Component, path } = PublicRoutes [ item ] ;
68+ return (
69+ < Route
70+ exact
71+ key = { path }
72+ path = { path }
73+ render = { ( route ) => {
74+ if (
75+ checkAuth . getToken ( ) === null &&
76+ PublicRoutes [ item ] . tokenNeeded
77+ ) {
78+ return < PublicLayout route = { route } Component = { Component } /> ;
79+ } else if ( ! PublicRoutes [ item ] . tokenNeeded ) {
80+ return < PublicLayout route = { route } Component = { Component } /> ;
81+ } else {
82+ return < Redirect to = { "/" } /> ;
83+ }
84+ } }
85+ />
86+ ) ;
7187 } ) }
7288
7389 { /*TODO: Gửi api xác minh người dùng có phải admin hay k */ }
7490 < Route path = "/" exact component = { MainPage } />
7591
7692 < Route path = "/success" exact component = { LoginWithSocial } />
7793 { /* <Route path="/firstTimeLogin" exact component={FirstTimeLogin} /> */ }
78- < Route path = "/profile/:username " exact component = { Profile } />
79- < Route path = "/*" exact component = { Page404 } />
94+ < Route path = "/profile/" exact component = { Profile } />
95+ < Route path = "/*" exact component = { Page404 } />
8096 </ Switch >
8197 </ Router >
82- )
98+ ) ;
8399}
84100
85101export default App ;
0 commit comments