Fix routing
This commit is contained in:
parent
a135458fb9
commit
f7409a4ffe
6 changed files with 58 additions and 60 deletions
|
|
@ -1,14 +1,27 @@
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
import { Switch, Route, useHistory } from 'react-router-dom'
|
||||||
import './index.css'
|
import './index.css'
|
||||||
|
|
||||||
import Header from '~components/Header'
|
import Header from '~components/Header'
|
||||||
import Main from '~components/Main'
|
import Main from '~components/Main'
|
||||||
|
|
||||||
function App() {
|
import New from '~routes/New'
|
||||||
|
import Party from '~routes/Party'
|
||||||
|
import Parties from '~routes/Parties'
|
||||||
|
import Profile from '~routes/Profile'
|
||||||
|
|
||||||
|
|
||||||
|
const App = () => {
|
||||||
|
const history = useHistory()
|
||||||
|
const route = (pathname: string) => history.push(pathname)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Header />
|
<Header navigate={route} />
|
||||||
<Main />
|
<Route exact path='/' component={New} />
|
||||||
|
<Route exact path='/parties/' component={Parties} />
|
||||||
|
<Route path='/p/:hash' component={Party} />
|
||||||
|
<Route exact path='/:username' component={Profile} />
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import { RouteComponentProps, useLocation, withRouter } from 'react-router-dom'
|
import { useLocation } from 'react-router-dom'
|
||||||
import { useCookies } from 'react-cookie'
|
import { useCookies } from 'react-cookie'
|
||||||
|
|
||||||
import Button from '~components/Button'
|
import Button from '~components/Button'
|
||||||
|
|
@ -7,14 +7,17 @@ import HeaderMenu from '~components/HeaderMenu'
|
||||||
|
|
||||||
import './index.css'
|
import './index.css'
|
||||||
|
|
||||||
interface Props {}
|
interface Props {
|
||||||
interface HeaderProps extends RouteComponentProps<Props> {}
|
navigate: (pathname: string) => void
|
||||||
|
}
|
||||||
|
|
||||||
const Header: React.FC<HeaderProps> = (props: HeaderProps) => {
|
const Header = (props: Props) => {
|
||||||
const [username, setUsername] = useState(undefined)
|
const [username, setUsername] = useState(undefined)
|
||||||
const [cookies, setCookie, removeCookie] = useCookies(['user'])
|
const [cookies, setCookie, removeCookie] = useCookies(['user'])
|
||||||
let location = useLocation()
|
let location = useLocation()
|
||||||
|
|
||||||
|
const route = (pathname: string) => props.navigate(pathname)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (cookies.user) {
|
if (cookies.user) {
|
||||||
setUsername(cookies.user.username)
|
setUsername(cookies.user.username)
|
||||||
|
|
@ -33,16 +36,7 @@ const Header: React.FC<HeaderProps> = (props: HeaderProps) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
function newParty() {
|
function newParty() {
|
||||||
if (props.history.location.pathname === '/') {
|
props.navigate('/')
|
||||||
window.history.replaceState(null, `Grid Tool`, `/`)
|
|
||||||
props.history.go(0)
|
|
||||||
} else {
|
|
||||||
props.history.push('/')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function route(pathname: string) {
|
|
||||||
props.history.push(pathname)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function logout() {
|
function logout() {
|
||||||
|
|
@ -60,7 +54,7 @@ const Header: React.FC<HeaderProps> = (props: HeaderProps) => {
|
||||||
<div className="left">
|
<div className="left">
|
||||||
<div className="dropdown">
|
<div className="dropdown">
|
||||||
<Button type="menu">Menu</Button>
|
<Button type="menu">Menu</Button>
|
||||||
<HeaderMenu username={username} navigate={route} logout={logout} />
|
<HeaderMenu username={username} logout={logout} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="push" />
|
<div className="push" />
|
||||||
|
|
@ -74,4 +68,4 @@ const Header: React.FC<HeaderProps> = (props: HeaderProps) => {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default withRouter(Header)
|
export default Header
|
||||||
|
|
@ -23,6 +23,14 @@
|
||||||
background: #e9e9e9;
|
background: #e9e9e9;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.MenuItem a {
|
||||||
|
color: #777
|
||||||
|
}
|
||||||
|
|
||||||
|
.MenuItem a:hover {
|
||||||
|
color: #555
|
||||||
|
}
|
||||||
|
|
||||||
.MenuGroup {
|
.MenuGroup {
|
||||||
border-bottom: 2px solid #f5f5f5;
|
border-bottom: 2px solid #f5f5f5;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,12 @@ import SignupModal from '~components/SignupModal'
|
||||||
|
|
||||||
import { useModal as useSignupModal } from '~utils/useModal'
|
import { useModal as useSignupModal } from '~utils/useModal'
|
||||||
import { useModal as useLoginModal } from '~utils/useModal'
|
import { useModal as useLoginModal } from '~utils/useModal'
|
||||||
import { Route } from 'react-router'
|
import { Link, Route } from 'react-router-dom'
|
||||||
|
import Profile from '~routes/Profile'
|
||||||
|
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
username?: string
|
username?: string
|
||||||
navigate: (pathname: string) => void
|
|
||||||
logout: () => void
|
logout: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -21,18 +21,27 @@ const HeaderMenu = (props: Props) => {
|
||||||
|
|
||||||
function authItems() {
|
function authItems() {
|
||||||
return (
|
return (
|
||||||
<ul className="Menu auth">
|
<nav>
|
||||||
<div className="MenuGroup">
|
<ul className="Menu auth">
|
||||||
<li className="MenuItem" onClick={ () => props.username ? props.navigate(`/${props.username}`) : '' }>My Parties</li>
|
<div className="MenuGroup">
|
||||||
</div>
|
<li className="MenuItem">
|
||||||
<div className="MenuGroup">
|
<Link to={props.username || ''}>My Parties</Link>
|
||||||
<li className="MenuItem" onClick={ () => props.navigate('about') }>About</li>
|
</li>
|
||||||
<li className="MenuItem" onClick={ () => props.navigate('guides') }>Guides</li>
|
</div>
|
||||||
</div>
|
<div className="MenuGroup">
|
||||||
<div className="MenuGroup">
|
<li className="MenuItem">
|
||||||
<li className="MenuItem" onClick={props.logout}>Logout</li>
|
<Link to='/about'>About</Link>
|
||||||
</div>
|
</li>
|
||||||
</ul>
|
|
||||||
|
<li className="MenuItem">
|
||||||
|
<Link to='/guides'>Guides</Link>
|
||||||
|
</li>
|
||||||
|
</div>
|
||||||
|
<div className="MenuGroup">
|
||||||
|
<li className="MenuItem" onClick={props.logout}>Logout</li>
|
||||||
|
</div>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,27 +0,0 @@
|
||||||
import React from 'react'
|
|
||||||
import { Router, Route } from 'react-router-dom'
|
|
||||||
|
|
||||||
import history from '~utils/history'
|
|
||||||
|
|
||||||
import New from '~routes/New'
|
|
||||||
import Party from '~routes/Party'
|
|
||||||
import Parties from '~routes/Parties'
|
|
||||||
import Profile from '~routes/Profile'
|
|
||||||
|
|
||||||
// The Main component renders one of the three provided
|
|
||||||
// Routes (provided that one matches). Both the /roster
|
|
||||||
// and /schedule routes will match any pathname that starts
|
|
||||||
// with /roster or /schedule. The / route will only match
|
|
||||||
// when the pathname is exactly the string "/"
|
|
||||||
const Main = () => (
|
|
||||||
<main>
|
|
||||||
<Router history={history}>
|
|
||||||
<Route exact path='/' component={New} />
|
|
||||||
<Route exact path='/parties/' component={Parties} />
|
|
||||||
<Route path='/p/:hash' component={Party} />
|
|
||||||
<Route exact path='/:username' component={Profile} />
|
|
||||||
</Router>
|
|
||||||
</main>
|
|
||||||
)
|
|
||||||
|
|
||||||
export default Main
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import ReactDOM from 'react-dom'
|
import ReactDOM from 'react-dom'
|
||||||
|
import { createBrowserHistory } from 'history'
|
||||||
import { CookiesProvider } from 'react-cookie'
|
import { CookiesProvider } from 'react-cookie'
|
||||||
import { BrowserRouter } from 'react-router-dom'
|
import { BrowserRouter } from 'react-router-dom'
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue