App.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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="*" component={Page404} />
  75. // </Switch>
  76. // }
  77. // </main>
  78. // )
  79. // }
  80. // export default function App() {
  81. // return (
  82. // <Provider store={store}>
  83. // <Router history={history}>
  84. // <div className="wrapper">
  85. // <CHeader />
  86. // <MainRoutes />
  87. // <Footer />
  88. // </div>
  89. // </Router>
  90. // </Provider>
  91. // )
  92. // }
  93. import React, { createContext, useContext, useEffect, useState } from 'react'
  94. import { Router, Route, Redirect, withRouter, Switch } from 'react-router-dom';
  95. import { Provider, useSelector } from 'react-redux';
  96. import { store } from './components/redux/index';
  97. import createHistory from "history/createBrowserHistory";
  98. import './App.css';
  99. // import logo from './logo.svg';
  100. // импорт страниц
  101. import { CUser } from './components/user';
  102. import { CFeed } from './components/feed';
  103. import { CComments } from './components/post';
  104. import { CreatePost } from './components/create_post';
  105. import AuthReg from './components/auth_reg';
  106. // import Header from './components/structure/header';
  107. import { CHeader } from './components/structure/header';
  108. import Footer from './components/structure/footer';
  109. import Search from './components/search';
  110. import Page404 from './components/404/page404';
  111. // url проекта
  112. export const url = 'http://hipstagram.node.ed.asmer.org.ua/'
  113. const history = createHistory()
  114. // общий контекст для модального окна
  115. export const ModalForCountsContext = createContext()
  116. export const UpdateProfile = createContext()
  117. // Приватный роутинг - удалить, если не буду использовать
  118. {
  119. // фейковый авторизатор
  120. const fakeAuth = {
  121. isAuthenticated: false,
  122. authenticate(cb) {
  123. this.isAuthenticated = true
  124. setTimeout(cb, 100)
  125. },
  126. signout(cb) {
  127. this.isAuthenticated = false
  128. setTimeout(cb, 100)
  129. }
  130. }
  131. // кнопка разлогина, которая рисуется, если залогинен
  132. const AuthButton = withRouter(({ history }) => (
  133. fakeAuth.isAuthenticated && (
  134. <p>
  135. Welcome! <button onClick={() => {
  136. fakeAuth.signout(() => history.push('/'))
  137. }}>Sign out</button>
  138. </p>
  139. )
  140. ))
  141. const PrivateRoute = ({ component: Component, ...rest }) => (
  142. <Route {...rest} render={(props) => (
  143. typeof localStorage.authToken === 'string'
  144. ? <Component {...props} />
  145. : <Redirect to={{
  146. pathname: '/authorization',
  147. state: { from: props.location }
  148. }} />
  149. )} />
  150. )
  151. }
  152. // роутинг в зависимости от того.залогинен пользователь или нет
  153. const MainRoutes = () => {
  154. const currentState = useSelector(state => state?.auth?.token)
  155. // функции управления открытием/закрытием модального окна
  156. const [modalName, setModalName] = useState('')
  157. const [modalArray, setModalArray] = useState([])
  158. const [openModal, setOpenModal] = useState(false);
  159. const handleOpenModal = (param) => {
  160. setOpenModal(true)
  161. param.arr && setModalArray([...param.arr]) // идет проверка на null, из-за баги на беке?
  162. setModalName(param.name)
  163. }
  164. const handleCloseModal = () => {
  165. setOpenModal(false)
  166. setModalArray([])
  167. }
  168. const aboutMe = useSelector(state => state?.promise?.AboutMe?.payload)
  169. const myFollowings = aboutMe?.following.map(item => {
  170. return { _id: item._id }
  171. })
  172. // функция управления моими данными для обновления профиля
  173. const [updateProfile, setUpdateProfile] = useState({ _id: aboutMe?._id, login: aboutMe?.login, nick: aboutMe?.nick, avatar: { _id: aboutMe?.avatar?._id }, following: myFollowings })
  174. // console.log('updateProfile: ', updateProfile)
  175. return (
  176. <main style={{ flexGrow: '1' }}>
  177. {currentState
  178. ? <Switch>
  179. <UpdateProfile.Provider value={[updateProfile, setUpdateProfile]}>
  180. <ModalForCountsContext.Provider value={[modalName, setModalName, modalArray, setModalArray, openModal, setOpenModal, handleOpenModal, handleCloseModal]}>
  181. <Route path="/" component={CFeed} exact />
  182. <Route path="/post/:postId" component={CComments} />
  183. <Route path="/user/:userId" component={CUser} />
  184. </ModalForCountsContext.Provider>
  185. <Route path='/search' component={Search} />
  186. </UpdateProfile.Provider>
  187. <Route path="/createpost" component={CreatePost} />
  188. <Route path="*" component={Page404} />
  189. </Switch>
  190. : <Switch>
  191. <Route path="/" component={AuthReg} exact />
  192. <Route path="/registration" component={AuthReg} />
  193. <Route path="*" component={Page404} />
  194. </Switch>
  195. }
  196. </main>
  197. )
  198. }
  199. export default function App() {
  200. return (
  201. <Provider store={store}>
  202. <Router history={history}>
  203. <div className="wrapper">
  204. <CHeader />
  205. <MainRoutes />
  206. <Footer />
  207. </div>
  208. </Router>
  209. </Provider>
  210. )
  211. }