App.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import React, { useEffect, useState } from 'react'
  2. import { Router, Route, Redirect, withRouter, Switch } from 'react-router-dom';
  3. import { Provider, useSelector } from 'react-redux';
  4. import { store } from './components/redux/index';
  5. import createHistory from "history/createBrowserHistory";
  6. import './App.css';
  7. // import logo from './logo.svg';
  8. // импорт страниц
  9. import { CUser } from './components/user';
  10. import { CFeed } from './components/feed';
  11. import { CComments } from './components/post';
  12. import { CreatePost } from './components/create_post';
  13. import AuthReg from './components/auth_reg';
  14. // import Header from './components/structure/header';
  15. import { CHeader } from './components/structure/header';
  16. import Footer from './components/structure/footer';
  17. import Search from './components/search';
  18. import Page404 from './components/404/page404';
  19. // url проекта
  20. export const url = 'http://hipstagram.node.ed.asmer.org.ua/'
  21. const history = createHistory()
  22. // Приватный роутинг - удалить, если не буду использовать
  23. {
  24. // фейковый авторизатор
  25. const fakeAuth = {
  26. isAuthenticated: false,
  27. authenticate(cb) {
  28. this.isAuthenticated = true
  29. setTimeout(cb, 100)
  30. },
  31. signout(cb) {
  32. this.isAuthenticated = false
  33. setTimeout(cb, 100)
  34. }
  35. }
  36. // кнопка разлогина, которая рисуется, если залогинен
  37. const AuthButton = withRouter(({ history }) => (
  38. fakeAuth.isAuthenticated && (
  39. <p>
  40. Welcome! <button onClick={() => {
  41. fakeAuth.signout(() => history.push('/'))
  42. }}>Sign out</button>
  43. </p>
  44. )
  45. ))
  46. const PrivateRoute = ({ component: Component, ...rest }) => (
  47. <Route {...rest} render={(props) => (
  48. typeof localStorage.authToken === 'string'
  49. ? <Component {...props} />
  50. : <Redirect to={{
  51. pathname: '/authorization',
  52. state: { from: props.location }
  53. }} />
  54. )} />
  55. )
  56. }
  57. // роутинг в зависимости от того.залогинен пользователь или нет
  58. const MainRoutes = () => {
  59. const currentState = useSelector(state => state?.auth?.token)
  60. return (
  61. <main style={{ flexGrow: '1' }}>
  62. {currentState
  63. ? <Switch>
  64. <Route path="/" component={CFeed} exact />
  65. <Route path="/post/:postId" component={CComments} />
  66. <Route path="/user/:userId" component={CUser} />
  67. <Route path="/createpost" component={CreatePost} />
  68. <Route path='/search' component={Search} />
  69. <Route path="*" component={Page404} />
  70. </Switch>
  71. : <Switch>
  72. <Route path="/" component={AuthReg} exact />
  73. <Route path="/registration" component={AuthReg} />
  74. {/* <Route path="/post/:postId" component={CComments} /> */}
  75. {/* <Route path="/user/:userId" component={CUser} /> */}
  76. <Route path="*" component={Page404} />
  77. </Switch>
  78. }
  79. </main>
  80. )
  81. }
  82. export default function App() {
  83. return (
  84. <Provider store={store}>
  85. <Router history={history}>
  86. <div className="wrapper">
  87. <CHeader />
  88. <MainRoutes />
  89. <Footer />
  90. </div>
  91. </Router>
  92. </Provider>
  93. )
  94. }