create match full page

This commit is contained in:
Ruidy Nemausat 2020-07-22 22:32:01 +02:00
parent 10df79e847
commit 5ab2967c88
6 changed files with 60 additions and 27 deletions

View file

@ -2,20 +2,24 @@ import React, { FC } from "react";
import { Switch, Router, Route } from "react-router-dom"; import { Switch, Router, Route } from "react-router-dom";
import { createBrowserHistory } from "history"; import { createBrowserHistory } from "history";
import MainLayout from "./layouts/MainLayout";
import Index from "./pages/Index"; import Index from "./pages/Index";
import Detail from "./pages/Detail"; import Detail from "./pages/Detail";
const App: FC = () => ( const App: FC = () => (
<Router history={createBrowserHistory()}> <Router history={createBrowserHistory()}>
<Switch> <MainLayout>
<Route exact path="/"> <Switch>
<Index /> <Route exact path="/">
</Route> <Index />
</Route>
<Route exact path="/:slug"> <Route exact path="/:slug">
<Detail /> <Detail />
</Route> </Route>
</Switch> </Switch>
</MainLayout>
</Router> </Router>
); );

View file

@ -1,5 +1,6 @@
import React, { FC } from "react"; import React, { FC } from "react";
import AppBar from "@material-ui/core/AppBar"; import AppBar from "@material-ui/core/AppBar";
import Link from "@material-ui/core/Link";
import Toolbar from "@material-ui/core/Toolbar"; import Toolbar from "@material-ui/core/Toolbar";
import IconButton from "@material-ui/core/IconButton"; import IconButton from "@material-ui/core/IconButton";
import Typography from "@material-ui/core/Typography"; import Typography from "@material-ui/core/Typography";
@ -12,6 +13,7 @@ import {
} from "@material-ui/core/styles"; } from "@material-ui/core/styles";
import MenuIcon from "@material-ui/icons/Menu"; import MenuIcon from "@material-ui/icons/Menu";
import SearchIcon from "@material-ui/icons/Search"; import SearchIcon from "@material-ui/icons/Search";
import { Link as RouterLink } from "react-router-dom";
const useStyles = makeStyles((theme: Theme) => const useStyles = makeStyles((theme: Theme) =>
createStyles({ createStyles({
@ -86,7 +88,9 @@ const Header: FC<{ title: string }> = ({ title }) => {
<MenuIcon /> <MenuIcon />
</IconButton> </IconButton>
<Typography className={classes.title} variant="h6" noWrap> <Typography className={classes.title} variant="h6" noWrap>
{title} <Link component={RouterLink} to="/" color="secondary">
{title}
</Link>
</Typography> </Typography>
<div className={classes.search}> <div className={classes.search}>
<div className={classes.searchIcon}> <div className={classes.searchIcon}>

View file

@ -0,0 +1,22 @@
import React, { FC } from "react";
import Moment from "react-moment";
import { Container, Typography } from "@material-ui/core";
import IGame from "../types/Game";
const MatchFull: FC<{ game: IGame }> = ({ game }) => (
<Container>
<Typography gutterBottom variant="h5" component="h3">
{game.title}
</Typography>
<Typography variant="body2" color="textSecondary" component="p">
{game.competition.name}
<br />
<Moment format="DD MMMM YYYY - HH:mm">{game.date}</Moment>
</Typography>
<div dangerouslySetInnerHTML={{ __html: game.videos[0].embed }}></div>
</Container>
);
export default MatchFull;

View file

@ -1,18 +1,16 @@
import React, { FC, useState, useEffect } from "react"; import React, { FC } from "react";
import { useRouteMatch } from "react-router-dom"; import { useRouteMatch } from "react-router-dom";
import { withGame, IGameState } from "../store/GameProvider";
import Loader from "../components/Loader"; import Loader from "../components/Loader";
// import MatchItem from "../components/MatchItem"; import MatchFull from "../components/MatchFull";
const Detail: FC = () => { const Detail: FC<IGameState> = ({ loading, selectGameByID }) => {
// const [game, setGame] = useState([]);
const [loading, _] = useState(true);
const { params } = useRouteMatch<{ slug: string }>(); const { params } = useRouteMatch<{ slug: string }>();
const game = selectGameByID(params.slug);
useEffect(() => {}, []); return loading ? <Loader /> : <MatchFull game={game} />;
console.log(params.slug);
return loading ? <Loader /> : <div>{/* <MatchItem {...game} /> */}</div>;
}; };
export default Detail; export default withGame((props: IGameState) => <Detail {...props} />);

View file

@ -1,12 +1,7 @@
import React, { FC } from "react"; import React, { FC } from "react";
import MatchList from "../components/MatchList"; import MatchList from "../components/MatchList";
import MainLayout from "../layouts/MainLayout";
const Index: FC = () => ( const Index: FC = () => <MatchList />;
<MainLayout>
<MatchList />
</MainLayout>
);
export default Index; export default Index;

View file

@ -8,24 +8,34 @@ export interface IGameState {
games: IGame[]; games: IGame[];
loading: boolean; loading: boolean;
setGames: React.Dispatch<React.SetStateAction<IGame[]>>; setGames: React.Dispatch<React.SetStateAction<IGame[]>>;
selectGameByID: (slug: string) => IGame;
} }
export const GameContext = createContext<IGameState>({ export const GameContext = createContext<IGameState>({
games: [], games: [],
loading: true, loading: true,
setGames: () => {}, setGames: () => {},
selectGameByID: () => {
return {} as IGame;
},
}); });
const GameProvider: FC = ({ children }) => { const GameProvider: FC = ({ children }) => {
const [games, setGames] = useState<IGame[]>([]); const [games, setGames] = useState<IGame[]>([]);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const selectGameByID = (slug: string): IGame => {
const id = parseInt(slug);
const game = games[id];
return game;
};
useEffect(() => { useEffect(() => {
apiClient apiClient
.get("/") .get("/")
.then(({ data }) => { .then(({ data }) => {
setLoading(false);
setGames(data); setGames(data);
setLoading(false);
}) })
.catch((err) => { .catch((err) => {
setLoading(false); setLoading(false);
@ -35,7 +45,7 @@ const GameProvider: FC = ({ children }) => {
}, []); }, []);
return ( return (
<GameContext.Provider value={{ games, loading, setGames }}> <GameContext.Provider value={{ games, loading, setGames, selectGameByID }}>
{children} {children}
</GameContext.Provider> </GameContext.Provider>
); );