123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- // import React, { useEffect, useState } from 'react'
- // import { Router, Route, Redirect, withRouter, Switch } from 'react-router-dom';
- // import { Provider, useSelector } from 'react-redux';
- // import { store } from './components/redux/index';
- // import createHistory from "history/createBrowserHistory";
- // import './App.css';
- // // import logo from './logo.svg';
- // // импорт страниц
- // import { CUser } from './components/user';
- // import { CFeed } from './components/feed';
- // import { CComments } from './components/post';
- // import { CreatePost } from './components/create_post';
- // import AuthReg from './components/auth_reg';
- // // import Header from './components/structure/header';
- // import { CHeader } from './components/structure/header';
- // import Footer from './components/structure/footer';
- // import Search from './components/search';
- // import Page404 from './components/404/page404';
- // // url проекта
- // export const url = 'http://hipstagram.node.ed.asmer.org.ua/'
- // const history = createHistory()
- // // Приватный роутинг - удалить, если не буду использовать
- // {
- // // фейковый авторизатор
- // const fakeAuth = {
- // isAuthenticated: false,
- // authenticate(cb) {
- // this.isAuthenticated = true
- // setTimeout(cb, 100)
- // },
- // signout(cb) {
- // this.isAuthenticated = false
- // setTimeout(cb, 100)
- // }
- // }
- // // кнопка разлогина, которая рисуется, если залогинен
- // const AuthButton = withRouter(({ history }) => (
- // fakeAuth.isAuthenticated && (
- // <p>
- // Welcome! <button onClick={() => {
- // fakeAuth.signout(() => history.push('/'))
- // }}>Sign out</button>
- // </p>
- // )
- // ))
- // const PrivateRoute = ({ component: Component, ...rest }) => (
- // <Route {...rest} render={(props) => (
- // typeof localStorage.authToken === 'string'
- // ? <Component {...props} />
- // : <Redirect to={{
- // pathname: '/authorization',
- // state: { from: props.location }
- // }} />
- // )} />
- // )
- // }
- // // роутинг в зависимости от того.залогинен пользователь или нет
- // const MainRoutes = () => {
- // const currentState = useSelector(state => state?.auth?.token)
- // return (
- // <main style={{ flexGrow: '1' }}>
- // {currentState
- // ? <Switch>
- // <Route path="/" component={CFeed} exact />
- // <Route path="/post/:postId" component={CComments} />
- // <Route path="/user/:userId" component={CUser} />
- // <Route path="/createpost" component={CreatePost} />
- // <Route path='/search' component={Search} />
- // <Route path="*" component={Page404} />
- // </Switch>
- // : <Switch>
- // <Route path="/" component={AuthReg} exact />
- // <Route path="/registration" component={AuthReg} />
- // <Route path="*" component={Page404} />
- // </Switch>
- // }
- // </main>
- // )
- // }
- // export default function App() {
- // return (
- // <Provider store={store}>
- // <Router history={history}>
- // <div className="wrapper">
- // <CHeader />
- // <MainRoutes />
- // <Footer />
- // </div>
- // </Router>
- // </Provider>
- // )
- // }
- import React, { createContext, useContext, useEffect, useState } from 'react'
- import { Router, Route, Redirect, withRouter, Switch } from 'react-router-dom';
- import { Provider, useSelector } from 'react-redux';
- import { store } from './components/redux/index';
- import createHistory from "history/createBrowserHistory";
- import './App.css';
- // import logo from './logo.svg';
- // импорт страниц
- import { CUser } from './components/user';
- import { CFeed } from './components/feed';
- import { CComments } from './components/post';
- import { CreatePost } from './components/create_post';
- import AuthReg from './components/auth_reg';
- // import Header from './components/structure/header';
- import { CHeader } from './components/structure/header';
- import Footer from './components/structure/footer';
- import Search from './components/search';
- import Page404 from './components/404/page404';
- // url проекта
- export const url = 'http://hipstagram.node.ed.asmer.org.ua/'
- const history = createHistory()
- // общий контекст для модального окна
- export const ModalForCountsContext = createContext()
- export const UpdateProfile = createContext()
- // Приватный роутинг - удалить, если не буду использовать
- {
- // фейковый авторизатор
- const fakeAuth = {
- isAuthenticated: false,
- authenticate(cb) {
- this.isAuthenticated = true
- setTimeout(cb, 100)
- },
- signout(cb) {
- this.isAuthenticated = false
- setTimeout(cb, 100)
- }
- }
- // кнопка разлогина, которая рисуется, если залогинен
- const AuthButton = withRouter(({ history }) => (
- fakeAuth.isAuthenticated && (
- <p>
- Welcome! <button onClick={() => {
- fakeAuth.signout(() => history.push('/'))
- }}>Sign out</button>
- </p>
- )
- ))
- const PrivateRoute = ({ component: Component, ...rest }) => (
- <Route {...rest} render={(props) => (
- typeof localStorage.authToken === 'string'
- ? <Component {...props} />
- : <Redirect to={{
- pathname: '/authorization',
- state: { from: props.location }
- }} />
- )} />
- )
- }
- // роутинг в зависимости от того.залогинен пользователь или нет
- const MainRoutes = () => {
- const currentState = useSelector(state => state?.auth?.token)
- // функции управления открытием/закрытием модального окна
- const [modalName, setModalName] = useState('')
- const [modalArray, setModalArray] = useState([])
- const [openModal, setOpenModal] = useState(false);
- const handleOpenModal = (param) => {
- setOpenModal(true)
- param.arr && setModalArray([...param.arr]) // идет проверка на null, из-за баги на беке?
- setModalName(param.name)
- }
- const handleCloseModal = () => {
- setOpenModal(false)
- setModalArray([])
- }
- const aboutMe = useSelector(state => state?.promise?.AboutMe?.payload)
- const myFollowings = aboutMe?.following.map(item => {
- return { _id: item._id }
- })
- // функция управления моими данными для обновления профиля
- const [updateProfile, setUpdateProfile] = useState({ _id: aboutMe?._id, login: aboutMe?.login, nick: aboutMe?.nick, avatar: { _id: aboutMe?.avatar?._id }, following: myFollowings })
- // console.log('updateProfile: ', updateProfile)
- return (
- <main style={{ flexGrow: '1' }}>
- {currentState
- ? <Switch>
- <UpdateProfile.Provider value={[updateProfile, setUpdateProfile]}>
- <ModalForCountsContext.Provider value={[modalName, setModalName, modalArray, setModalArray, openModal, setOpenModal, handleOpenModal, handleCloseModal]}>
- <Route path="/" component={CFeed} exact />
- <Route path="/post/:postId" component={CComments} />
- <Route path="/user/:userId" component={CUser} />
- </ModalForCountsContext.Provider>
- <Route path='/search' component={Search} />
- </UpdateProfile.Provider>
- <Route path="/createpost" component={CreatePost} />
- <Route path="*" component={Page404} />
- </Switch>
- : <Switch>
- <Route path="/" component={AuthReg} exact />
- <Route path="/registration" component={AuthReg} />
- <Route path="*" component={Page404} />
- </Switch>
- }
- </main>
- )
- }
- export default function App() {
- return (
- <Provider store={store}>
- <Router history={history}>
- <div className="wrapper">
- <CHeader />
- <MainRoutes />
- <Footer />
- </div>
- </Router>
- </Provider>
- )
- }
|