import React Component useEffect from react import Provider from react

 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
import React, { Component, useEffect } from 'react'
import { Provider } from 'react-redux'
import store from './store'
import { HashRouter as Router, Route, Switch, Redirect } from 'react-router-dom'
import LoginPage from './components/LoginPage'
import AddItemPage from './components/AddItemPage'
import AddPropPage from './components/AddPropPage'
import ItemListPage from './components/ItemListPage'
import ItemPage from './components/ItemPage'
import PropListPage from './components/PropListPage'
import HomePage from './components/HomePage'
import NotFoundPage from './components/NotFoundPage'
import createMiddleware from './middlewares/createMiddleware'
import './styles/app.scss'
import { actionFetchUserData } from './store/system/actions'
const isAuthenticated = (Component : any ) => {
return (0 !== store.getState().system.jwt.length ? <Component /> : <Redirect to='/login' />)
}
const isNotAuthenticated = (Component: any) => {
return (0 === store.getState().system.jwt.length ? <Component /> : <Redirect to='/' />)
}
const initUser = () => {
const localJwt : string | null = localStorage.getItem('jwt')
if (null !== localJwt) {
store.dispatch(actionFetchUserData({
jwt: localJwt
}))
}
}
const App = () => {
// useEffect(() => initUser())
initUser()
return (<Provider store={store}>
<Router>
<Switch>
<Route exact
path="/"
render={() => createMiddleware(isAuthenticated, HomePage)} />
<Route path="/login"
render={() => createMiddleware(isNotAuthenticated, LoginPage)} />
<Route path="/items"
render={() => createMiddleware(isAuthenticated, ItemListPage)} />
<Route path="/items/add"
render={() => createMiddleware(isAuthenticated, AddItemPage)} />
<Route path="/props"
render={() => createMiddleware(isAuthenticated, PropListPage)} />
<Route path="/props/add"
render={() => createMiddleware(isAuthenticated, AddPropPage)} />
<Route path="/item/:id"
render={() => createMiddleware(isAuthenticated, ItemPage)} />
<Route exact
path="*"
component={NotFoundPage} />
</Switch>
</Router>
</Provider>)
}
export default App