App.js 2.9 KB

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