App.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import React, { useState } from "react";
  2. import "./App.css";
  3. import "bootstrap/dist/css/bootstrap.min.css";
  4. import { store, socket } from "./reducers";
  5. import createHistory from "history/createBrowserHistory";
  6. import { Provider, connect } from "react-redux";
  7. import { Router, Route, Switch } from "react-router-dom";
  8. import { CLogin } from "./pages/LoginPage";
  9. import { CRegistration } from "./pages/RegPage";
  10. import Header from "./pages/Header";
  11. import { CProfilePage } from "./pages/ProfilePage";
  12. import { CNewChatPage } from "./pages/NewChatPage";
  13. import { CChangePass } from "./pages/ChangePassPage";
  14. import { ChangesDone } from "./pages/ChangesDonePage";
  15. import { ChangesDoneForChats } from "./pages/ChangeDoneForChat";
  16. import { AboutUs } from "./pages/AboutUs";
  17. import { Redirect } from "react-router-dom";
  18. import { CChatMsgs } from "./pages/ChatMsgsPage";
  19. import { CChatsAside } from "./pages/ChatsAside";
  20. import { Preloader } from "./helpers/preloaders";
  21. import { CChatEditing } from "./pages/ChatEditing";
  22. export const history = createHistory();
  23. const AuthSwitch = ({ token }) => {
  24. if (token) {
  25. console.log("подключение сокета");
  26. socket.emit("jwt", token);
  27. }
  28. return (
  29. <>
  30. <Switch>
  31. <Route path="/login" component={() => <></>} />
  32. <Route path="/registration" component={() => <></>} />
  33. <Header />
  34. </Switch>
  35. <Route path="/login" component={CLogin} />
  36. <Route path="/registration" component={CRegistration} />
  37. <Route path="/profile" component={CProfilePage} />
  38. <Route path="/newchat" component={CNewChatPage} />
  39. <Route path="/changepas" component={CChangePass} />
  40. <Route path="/changesdone" component={ChangesDone} />
  41. <Route path="/changesdonechats" component={ChangesDoneForChats} />
  42. <Route path="/chatediting/:_id" component={CChatEditing} />
  43. <Route path="/aboutus" component={AboutUs} />
  44. <div className="mainContainer">
  45. <Route path="/main/" component={CChatsAside} />
  46. <Route path="/main/:_id" component={CChatMsgs} />
  47. </div>
  48. <Redirect
  49. to={
  50. store.getState().auth.token === undefined
  51. ? "/login"
  52. : "/main"
  53. }
  54. />
  55. </>
  56. );
  57. };
  58. const CAuthSwitch = connect((state) => ({ token: state.auth.token || null }))(
  59. AuthSwitch
  60. );
  61. function App() {
  62. return (
  63. <>
  64. <Router history={history}>
  65. <Provider store={store}>
  66. <div className="container">
  67. <CAuthSwitch />
  68. </div>
  69. </Provider>
  70. </Router>
  71. </>
  72. );
  73. }
  74. export default App;