Conditional rendering for components

This commit is contained in:
Kristoffer Juelsenn 2022-04-23 10:19:26 +02:00
parent 779393e1af
commit 10476cd415
4 changed files with 41 additions and 18 deletions

View File

@ -14,6 +14,8 @@ import KeyboardDoubleArrowUpIcon from '@mui/icons-material/KeyboardDoubleArrowUp
import EditIcon from '@mui/icons-material/Edit'; import EditIcon from '@mui/icons-material/Edit';
import EmojiEventsIcon from '@mui/icons-material/EmojiEvents'; import EmojiEventsIcon from '@mui/icons-material/EmojiEvents';
let isLoggedIn = true;
function CreateButton(props) { function CreateButton(props) {
return ( return (
<Link to="/create"> <Link to="/create">
@ -111,11 +113,13 @@ function TournamentListItem(props) {
<Box sx={{flexGrow: 1, marginTop: "20px"}}> <Box sx={{flexGrow: 1, marginTop: "20px"}}>
<Grid container spacing={4} justifyContent="center" wrap="wrap"> <Grid container spacing={4} justifyContent="center" wrap="wrap">
<Grid item> { isLoggedIn ?
<Grid item>
<Link to={`/tournament/${props.tournament.id}/manage`}> <Link to={`/tournament/${props.tournament.id}/manage`}>
<Button className="ManageButton" variant="contained" color="primary" endIcon={<EditIcon />}>Edit Tournament</Button> <Button className="ManageButton" variant="contained" color="primary" endIcon={<EditIcon />}>Edit Tournament</Button>
</Link> </Link>
</Grid> </Grid> : null
}
<Grid item > <Grid item >
<Link to={`/tournament/${props.tournament.id}`} > <Link to={`/tournament/${props.tournament.id}`} >
<Button variant="contained" color="success"> <Button variant="contained" color="success">
@ -177,7 +181,9 @@ function Home() {
<Box component={Stack} direction="row" align="center" justifyContent="space-between" alignItems="center" sx={{flexGrow: 1}}> <Box component={Stack} direction="row" align="center" justifyContent="space-between" alignItems="center" sx={{flexGrow: 1}}>
{/* <CreateButton /> */} {/* <CreateButton /> */}
<Typography variant="h3">Tournaments</Typography> <Typography variant="h3">Tournaments</Typography>
<CreateButton /> { isLoggedIn ?
<CreateButton /> : null
}
</Box> </Box>
<TournamentList /> <TournamentList />
<Typography variant="h5" color="#555555"> <Typography variant="h5" color="#555555">

View File

@ -14,10 +14,14 @@ export default function LoginPage() {
return ( return (
<> <>
<AppBar pageTitle="Sign in" /> <AppBar pageTitle="Sign in" />
<h2>Sign in with google</h2> <Paper x={{width: "70vw", margin: "1.5% auto"}} component={Stack} direction="column" justifyContent="center" alignItems="center">
<a href={process.env.REACT_APP_LOGIN_URL}> <Stack direction="column" paddingTop={'0.5%'} alignItems={'center'}>
<img src="/btn_google_signing_dark.png" alt="Sign in with google" /> <Typography>Sign in with google</Typography>
</a> <Link to={process.env.REACT_APP_LOGIN_URL}>
<img src="/btn_google_signing_dark.png" alt="Sign in with google" />
</Link>
</Stack>
</Paper>
</> </>
); );
} }

View File

@ -10,6 +10,8 @@ import DoDisturbIcon from '@mui/icons-material/DoDisturb';
import BackspaceIcon from '@mui/icons-material/Backspace'; import BackspaceIcon from '@mui/icons-material/Backspace';
import AddCircleIcon from '@mui/icons-material/AddCircle'; import AddCircleIcon from '@mui/icons-material/AddCircle';
let isLoggedIn = true;
function showError(error) { function showError(error) {
alert("Something went wrong. \n" + error); alert("Something went wrong. \n" + error);
console.error(error); console.error(error);
@ -88,6 +90,8 @@ function Match(props){
.catch(err => showError(err)); .catch(err => showError(err));
}) })
console.log(props)
let today = new Date() let today = new Date()
let yesterday = today.setDate(today.getDate() - 1) let yesterday = today.setDate(today.getDate() - 1)
let isComplete = new Date(endTime) < yesterday let isComplete = new Date(endTime) < yesterday
@ -100,10 +104,10 @@ function Match(props){
<Typography className={`teamName`} align={'center'} sx={{fontSize:'1.5rem', maxWidth:'15vw', overflow:'hidden', wordWrap:'none'}}> <Typography className={`teamName`} align={'center'} sx={{fontSize:'1.5rem', maxWidth:'15vw', overflow:'hidden', wordWrap:'none'}}>
{team1Name} {team1Name}
</Typography> </Typography>
{ props.match.winnerId && (props.match.team1Id === props.match.winnerId) && !isComplete ? { props.match.teamId !== null && !isComplete && props.match.tier !== Math.log2(4) - 1 && props.match.winnerId === null && team1Name !== "TBA" ?
<IconButton color="error" aria-label="remmove winner" component="span"><BackspaceIcon /></IconButton> : null <IconButton color="error" aria-label="remmove winner" component="span"><BackspaceIcon /></IconButton> : null
} }
{ props.match.team1Id !== null && !isComplete ? { props.match.team1Id !== null && props.match.winnerId === null && !isComplete && team1Name !== "TBA" ?
<IconButton onClick={setWinner(props.match.team1Id)} color="success" aria-label="select winner" component="span"><AddCircleIcon /></IconButton> : null <IconButton onClick={setWinner(props.match.team1Id)} color="success" aria-label="select winner" component="span"><AddCircleIcon /></IconButton> : null
} }
{/* { props.match.winnerId && (props.match.team1Id === props.match.winnerId) && {/* { props.match.winnerId && (props.match.team1Id === props.match.winnerId) &&
@ -118,10 +122,10 @@ function Match(props){
<Typography className={`teamName`} sx={{fontSize:'1.5rem', maxWidth:'15vw', overflow:'hidden', wordWrap:'none'}}> <Typography className={`teamName`} sx={{fontSize:'1.5rem', maxWidth:'15vw', overflow:'hidden', wordWrap:'none'}}>
{team2Name} {team2Name}
</Typography> </Typography>
{ props.match.winnerId && (props.match.team2Id === props.match.winnerId) && !isComplete ? { props.match.teamId !== null && !isComplete && props.match.tier !== Math.log2(props.maxTeams) - 1 && props.match.winnerId === null && team2Name !== "TBA" ?
<IconButton color="error" aria-label="remmove winner" component="span"><BackspaceIcon /></IconButton> : null <IconButton color="error" aria-label="remmove winner" component="span"><BackspaceIcon /></IconButton> : null
} }
{ props.match.team2Id !== null && !isComplete ? { props.match.team1Id !== null && props.match.winnerId === null && !isComplete && team2Name !== "TBA" ?
<IconButton onClick={setWinner(props.match.team2Id)} color="success" aria-label="select winner" component="span"><AddCircleIcon /></IconButton> : null <IconButton onClick={setWinner(props.match.team2Id)} color="success" aria-label="select winner" component="span"><AddCircleIcon /></IconButton> : null
} }
{/* { props.match.winnerId && (props.match.team2Id === props.match.winnerId) && {/* { props.match.winnerId && (props.match.team2Id === props.match.winnerId) &&
@ -238,7 +242,9 @@ export default function TournamentOverview(props) {
return ( return (
<> <>
<Appbar pageTitle="View Tournament" /> <Appbar pageTitle="View Tournament" />
<RemovableBar tournamentId={tournamentId} /> { isLoggedIn ?
<RemovableBar tournamentId={tournamentId} /> : null
}
<BracketViewer tournamentId={tournamentId} className="bracketViewer" /> <BracketViewer tournamentId={tournamentId} className="bracketViewer" />
</> </>
); );

View File

@ -5,8 +5,11 @@ import MenuIcon from '@mui/icons-material/Menu';
import AccountCircleIcon from '@mui/icons-material/AccountCircle'; import AccountCircleIcon from '@mui/icons-material/AccountCircle';
import HistoryIcon from '@mui/icons-material/History'; import HistoryIcon from '@mui/icons-material/History';
import LogoutIcon from '@mui/icons-material/Logout'; import LogoutIcon from '@mui/icons-material/Logout';
import LoginIcon from '@mui/icons-material/Login';
import logo from "./../Asura2222.png"; import logo from "./../Asura2222.png";
var isLoggedIn = false; // props.isLoggedIn;
function LoggedInMenu() { function LoggedInMenu() {
const [anchorEl, setAnchorEl] = React.useState(null); const [anchorEl, setAnchorEl] = React.useState(null);
const open = Boolean(anchorEl); const open = Boolean(anchorEl);
@ -19,6 +22,7 @@ function LoggedInMenu() {
const logout = () => { const logout = () => {
console.log("Logged out"); console.log("Logged out");
isLoggedIn = false;
setAnchorEl(null); setAnchorEl(null);
} }
@ -41,12 +45,13 @@ function NotLoggedInButton() {
const login = () => { const login = () => {
console.log("Logged in"); console.log("Logged in");
isLoggedIn = true;
} }
return ( return (
<> <>
<Link to="/" style={{color:"white"}}> <Link to="/login" style={{color:"white"}}>
<Button sx={{color:"white"}} onclick={login}> <Button sx={{color:"white"}} onClick={login} endIcon={<LoginIcon />}>
Login Login
</Button> </Button>
</Link> </Link>
@ -55,8 +60,6 @@ function NotLoggedInButton() {
} }
export default function Appbar(props) { export default function Appbar(props) {
const isLoggedIn = true; // props.isLoggedIn;
return ( return (
<> <>
<CssBaseline /> <CssBaseline />
@ -81,9 +84,13 @@ export default function Appbar(props) {
<Grid item xs={8}> <Grid item xs={8}>
<Typography component="div"><h2>{props.pageTitle || ""}</h2></Typography> <Typography component="div"><h2>{props.pageTitle || ""}</h2></Typography>
</Grid> </Grid>
<Grid item xs={2}> { props.pageTitle !== "Sign in" ?
<Grid item xs={2}>
{ isLoggedIn ? <LoggedInMenu /> : <NotLoggedInButton /> } { isLoggedIn ? <LoggedInMenu /> : <NotLoggedInButton /> }
</Grid> </Grid> :
<Grid item xs={2}>
</Grid>
}
</Grid> </Grid>
</Box> </Box>
</Toolbar> </Toolbar>