From fa35a343550c86058e104d63ca87f978b3443cd2 Mon Sep 17 00:00:00 2001 From: Songyi Huang Date: Tue, 13 Apr 2021 22:23:58 -0700 Subject: [PATCH] refactor navbar to functional component --- src/App.js | 28 +++++++-------- src/components/Navbar.js | 46 ++++++++++++------------- src/index.js | 1 + src/view/PVEView/PVEDoudizhuDemoView.js | 11 ++++-- src/view/PVEView/index.js | 4 +-- 5 files changed, 46 insertions(+), 44 deletions(-) diff --git a/src/App.js b/src/App.js index 200f935..ca79191 100644 --- a/src/App.js +++ b/src/App.js @@ -1,35 +1,33 @@ import React from 'react'; import { BrowserRouter as Router, Route, Redirect } from "react-router-dom"; import LeaderBoard from './view/LeaderBoard'; -import { DoudizhuGameView, LeducHoldemGameView } from './view/GameView'; -import { PVEDoudizhuDemoView } from './view/PVEView'; +import { DoudizhuReplayView, LeducHoldemReplayView } from './view/ReplayView'; +import { PvEDoudizhuDemoView } from './view/PvEView'; import Navbar from "./components/Navbar"; -import {makeStyles} from "@material-ui/core/styles"; -const useStyles = makeStyles(() => ({ - navBar: { - zIndex: 1002 - } -})); +const navbarSubtitleMap = { + "/leaderboard": "", + "/replay/doudizhu": "Doudizhu", + "/replay/leduc-holdem": "Leduc Hold'em", + "/pve/doudizhu-demo": "Doudizhu PvE Demo" +}; function App() { - const classes = useStyles(); - // todo: add 404 page return ( - +
{/* for test use */} {/* */} - + - - - + + +
); diff --git a/src/components/Navbar.js b/src/components/Navbar.js index e4f21aa..dc791c2 100644 --- a/src/components/Navbar.js +++ b/src/components/Navbar.js @@ -1,39 +1,37 @@ -import React from "react"; -import { Link } from 'react-router-dom'; +import React, { useEffect, useState } from "react"; +import { Link, useLocation } from 'react-router-dom'; import logo_white from "../assets/images/logo_white.png"; import GitHubIcon from "@material-ui/icons/GitHub"; import AppBar from "@material-ui/core/AppBar"; import axios from 'axios'; -class Navbar extends React.Component { - constructor(props) { - super(props); +function Navbar({subtitleMap}) { + const [stars, setStars] = useState('...'); + let location = useLocation(); + console.log(location.pathname, subtitleMap); + const subtitle = subtitleMap[location.pathname]; - this.state = {stars: '...'}; - } - - componentDidMount() { + useEffect(() => { axios.get("https://api.github.com/repos/datamllab/rlcard") .then(res=>{ - this.setState({stars: res.data.stargazers_count}); + setStars(res.data.stargazers_count); }); - } + }, []) - render() { - return ( - -
- {"Logo"} -
Showdown{this.props.gameName === '' ? '' : '/ ' + this.props.gameName}
-
-
{window.location.href = 'https://github.com/datamllab/rlcard'}}> -
-
Github
{this.state.stars} stars
-
+ return ( + +
+ {"Logo"} +
Showdown{subtitle ? '/ ' + subtitle : ''}
+
+
{window.location.href = 'https://github.com/datamllab/rlcard'}}> +
+
Github
{stars} stars
- +
+ ) - } + } export default Navbar; diff --git a/src/index.js b/src/index.js index 8f9c359..1d01335 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ import React from 'react'; import ReactDOM from 'react-dom'; + import './assets/index.scss'; import App from './App'; diff --git a/src/view/PVEView/PVEDoudizhuDemoView.js b/src/view/PVEView/PVEDoudizhuDemoView.js index 1968f81..9e772a2 100644 --- a/src/view/PVEView/PVEDoudizhuDemoView.js +++ b/src/view/PVEView/PVEDoudizhuDemoView.js @@ -1,15 +1,20 @@ import React from 'react'; +import Navbar from "../../components/Navbar"; +import { DoudizhuGameBoard } from '../../components/GameBoard'; -class PVEDoudizhuDemoView extends React.Component { + +class PvEDoudizhuDemoView extends React.Component { constructor(props) { super(props); } render() { return ( -
Under Development...
+
+
Under Development...
+
); } } -export default PVEDoudizhuDemoView; \ No newline at end of file +export default PvEDoudizhuDemoView; \ No newline at end of file diff --git a/src/view/PVEView/index.js b/src/view/PVEView/index.js index c2b1b61..5d89759 100644 --- a/src/view/PVEView/index.js +++ b/src/view/PVEView/index.js @@ -1,3 +1,3 @@ -import PVEDoudizhuDemoView from './PVEDoudizhuDemoView'; +import PvEDoudizhuDemoView from './PvEDoudizhuDemoView'; -export {PVEDoudizhuDemoView}; \ No newline at end of file +export {PvEDoudizhuDemoView}; \ No newline at end of file