speed-flowchart-web/src/pages/CalendarPage.tsx

133 lines
4 KiB
TypeScript
Raw Normal View History

2022-07-27 02:55:05 +02:00
import {
Button,
Card,
CardActions,
CardContent,
Grid,
2022-07-28 18:59:28 +02:00
Skeleton,
2022-07-27 02:55:05 +02:00
TextField,
Typography,
} from '@mui/material';
import React from 'react';
import { useNavigate } from 'react-router-dom';
import { Context } from '../data/Context';
import { Competiton } from '../models/Competition';
/**
*
* @return {JSX.Element} calendar page
*/
export default function CalendarPage() {
2022-07-28 18:59:28 +02:00
const competitions = React.useRef<Competiton[]>([]);
const [filteredCompetitions, setFilteredCompetitions] =
React.useState<Competiton[]>();
const [filter, setFilter] = React.useState<string>();
2022-07-29 00:36:15 +02:00
const { api, setTitle, setContainerMaxWidth } = React.useContext(Context);
2022-07-27 02:55:05 +02:00
const navigate = useNavigate();
2022-07-28 19:39:06 +02:00
React.useEffect(() => {
setTitle('DAV calendar');
2022-07-29 00:36:15 +02:00
setContainerMaxWidth('lg');
2022-07-28 19:39:06 +02:00
}, []);
2022-07-27 02:55:05 +02:00
React.useEffect(() => {
api.getCompetitions('GER').then(result => {
console.log(result.competitions.filter);
2022-07-28 18:59:28 +02:00
competitions.current = result.competitions.filter(competition =>
competition.discipline?.includes('speed'),
2022-07-27 02:55:05 +02:00
);
2022-07-28 18:59:28 +02:00
setFilteredCompetitions(filterCompetitions(competitions.current));
2022-07-27 02:55:05 +02:00
});
}, []);
2022-07-28 18:59:28 +02:00
React.useEffect(() => {
if (competitions.current.length > 0)
setFilteredCompetitions(filterCompetitions(competitions.current));
}, [filter]);
const filterCompetitions = (competitions: Competiton[]) => {
2022-07-28 19:39:06 +02:00
return competitions.filter(c => {
const queryMatches = c.name
.toLowerCase()
.includes(filter?.toLocaleLowerCase() ?? '');
const date = new Date(c.date).getTime();
const currentDate = new Date().getTime();
return queryMatches && currentDate - date > 24 * 60 * 60 * 1000;
});
2022-07-28 18:59:28 +02:00
};
2022-07-27 02:55:05 +02:00
const openCompetition = (competitionId: string, categoryId: string) => {
navigate(`/speed-flowchart/${competitionId}/${categoryId}`);
};
return (
<>
2022-07-29 00:36:15 +02:00
<Grid container spacing={2} direction={'column'}>
<Grid item>
2022-07-27 02:55:05 +02:00
<TextField
label='search for competition'
variant='outlined'
2022-07-28 18:59:28 +02:00
onChange={e => setFilter(e.target.value)}
2022-07-27 02:55:05 +02:00
fullWidth
/>
</Grid>
2022-07-28 18:59:28 +02:00
{filteredCompetitions?.map(competition => (
2022-07-29 00:36:15 +02:00
<Grid item key={`competition-card-${competition.WetId}`}>
2022-07-27 02:55:05 +02:00
<Card>
<CardContent>
<Typography gutterBottom variant='h5' component='div'>
{competition.name}
</Typography>
<Typography variant='body2' color='text.secondary'>
{competition.date_span}
</Typography>
</CardContent>
<CardActions>
<Grid container spacing={2}>
{competition.cats.map(category => (
<Grid
item
key={`competition-card-${competition.WetId}-category-button-${category.GrpId}`}
xs
>
<Button
size='small'
fullWidth
sx={{ width: '100%' }}
onClick={() =>
openCompetition(competition.WetId, category.GrpId)
}
>
{category.name}
</Button>
</Grid>
))}
</Grid>
</CardActions>
</Card>
</Grid>
))}
2022-07-28 18:59:28 +02:00
{filteredCompetitions === undefined && (
<Grid item xs={12}>
<Card>
<CardContent>
<Skeleton height={50}></Skeleton>
<Skeleton></Skeleton>
</CardContent>
<CardActions>
<Skeleton width={200}></Skeleton>
</CardActions>
</Card>
</Grid>
)}
{filteredCompetitions?.length === 0 && (
<Grid item xs={12} className={'center-children'}>
Nothing found!
</Grid>
)}
2022-07-27 02:55:05 +02:00
</Grid>
</>
);
}