feat(pages): add recent images to dashboard

This commit is contained in:
diced 2021-09-06 15:58:01 -07:00
parent ee48456291
commit 636de18642
No known key found for this signature in database
GPG key ID: 85AB64C74535D76E
3 changed files with 34 additions and 4 deletions

View file

@ -9,7 +9,7 @@ export default function Card(props) {
const { name, children, ...other } = props; const { name, children, ...other } = props;
return ( return (
<MuiCard sx={{ minWidth: 100 }} {...other}> <MuiCard sx={{ minWidth: '100%' }} {...other}>
<CardContent> <CardContent>
<Typography variant='h3'>{name}</Typography> <Typography variant='h3'>{name}</Typography>
{children} {children}

View file

@ -11,7 +11,10 @@ import {
ButtonGroup, ButtonGroup,
Typography, Typography,
Grid, Grid,
Skeleton Skeleton,
CardActionArea,
CardMedia,
Card as MuiCard
} from '@material-ui/core'; } from '@material-ui/core';
import Link from 'components/Link'; import Link from 'components/Link';
@ -85,15 +88,18 @@ export default function Dashboard() {
const user = useStoreSelector(state => state.user); const user = useStoreSelector(state => state.user);
const [images, setImages] = useState([]); const [images, setImages] = useState([]);
const [recent, setRecent] = useState([]);
const [page, setPage] = useState(0); const [page, setPage] = useState(0);
const [stats, setStats] = useState(null); const [stats, setStats] = useState(null);
const [rowsPerPage, setRowsPerPage] = useState(10); const [rowsPerPage, setRowsPerPage] = useState(10);
const updateImages = async () => { const updateImages = async () => {
const imgs = await useFetch('/api/user/images'); const imgs = await useFetch('/api/user/images');
const recent = await useFetch('/api/user/recent');
const stts = await useFetch('/api/stats'); const stts = await useFetch('/api/stats');
setImages(imgs); setImages(imgs);
setStats(stts); setStats(stts);
setRecent(recent);
}; };
const handleChangePage = (event, newPage) => { const handleChangePage = (event, newPage) => {
@ -119,6 +125,26 @@ export default function Dashboard() {
<Typography variant='h4'>Welcome back {user?.username}</Typography> <Typography variant='h4'>Welcome back {user?.username}</Typography>
<Typography color='GrayText' pb={2}>You have <b>{images.length ? images.length : '...'}</b> images</Typography> <Typography color='GrayText' pb={2}>You have <b>{images.length ? images.length : '...'}</b> images</Typography>
<Typography variant='h4'>Recent Images</Typography>
<Grid container spacing={4} py={2}>
{recent.length ? recent.map(image => (
<Grid item xs={12} sm={3} key={image.id}>
<MuiCard sx={{ minWidth: '100%' }}>
<CardActionArea>
<CardMedia
sx={{ height: 220 }}
image={image.url}
title={image.file}
/>
</CardActionArea>
</MuiCard>
</Grid>
)) : [1,2,3,4].map(x => (
<Grid item xs={12} sm={3} key={x}>
<Skeleton variant='rectangular' width='100%' height={220} sx={{ borderRadius: 1 }}/>
</Grid>
))}
</Grid>
<Typography variant='h4'>Stats</Typography> <Typography variant='h4'>Stats</Typography>
<Grid container spacing={4} py={2}> <Grid container spacing={4} py={2}>
<Grid item xs={12} sm={4}> <Grid item xs={12} sm={4}>
@ -140,7 +166,8 @@ export default function Dashboard() {
<StatText>{stats ? stats.count_users : <Skeleton variant='text' />}</StatText> <StatText>{stats ? stats.count_users : <Skeleton variant='text' />}</StatText>
</Card> </Card>
</Grid> </Grid>
</Grid><Card name='Images' sx={{ my: 2 }} elevation={0} variant='outlined'> </Grid>
<Card name='Images' sx={{ my: 2 }} elevation={0} variant='outlined'>
<Link href='/dashboard/images' pb={2}>View Gallery</Link> <Link href='/dashboard/images' pb={2}>View Gallery</Link>
<TableContainer sx={{ maxHeight: 440 }}> <TableContainer sx={{ maxHeight: 440 }}>
<Table size='small'> <Table size='small'>

View file

@ -5,7 +5,7 @@ async function handler(req: NextApiReq, res: NextApiRes) {
const user = await req.user(); const user = await req.user();
if (!user) return res.forbid('not logged in'); if (!user) return res.forbid('not logged in');
const take = Number(req.query.take ?? 3); const take = Number(req.query.take ?? 4);
if (take > 50) return res.error('take can\'t be more than 50'); if (take > 50) return res.error('take can\'t be more than 50');
@ -21,6 +21,9 @@ async function handler(req: NextApiReq, res: NextApiRes) {
} }
}); });
// @ts-ignore
images.map(image => image.url = `/r/${image.file}`);
return res.json(images); return res.json(images);
} }