Added paging to dashboard.

This commit is contained in:
Mike Cao 2023-08-30 15:23:08 -07:00
parent 50b4ac9889
commit 33ffa0b3d1
6 changed files with 56 additions and 39 deletions

View File

@ -1,8 +1,8 @@
import { useState } from 'react'; import { Button, Icon, Icons, Text } from 'react-basics';
import { Button, Icon, Icons, Text, Flexbox } from 'react-basics';
import Link from 'next/link'; import Link from 'next/link';
import Page from 'components/layout/Page'; import Page from 'components/layout/Page';
import PageHeader from 'components/layout/PageHeader'; import PageHeader from 'components/layout/PageHeader';
import Pager from 'components/common/Pager';
import WebsiteChartList from 'components/pages/websites/WebsiteChartList'; import WebsiteChartList from 'components/pages/websites/WebsiteChartList';
import DashboardSettingsButton from 'components/pages/dashboard/DashboardSettingsButton'; import DashboardSettingsButton from 'components/pages/dashboard/DashboardSettingsButton';
import DashboardEdit from 'components/pages/dashboard/DashboardEdit'; import DashboardEdit from 'components/pages/dashboard/DashboardEdit';
@ -11,23 +11,24 @@ import useApi from 'components/hooks/useApi';
import useDashboard from 'store/dashboard'; import useDashboard from 'store/dashboard';
import useMessages from 'components/hooks/useMessages'; import useMessages from 'components/hooks/useMessages';
import useLocale from 'components/hooks/useLocale'; import useLocale from 'components/hooks/useLocale';
import useApiFilter from 'components/hooks/useApiFilter';
export function Dashboard() { export function Dashboard() {
const { formatMessage, labels, messages } = useMessages(); const { formatMessage, labels, messages } = useMessages();
const dashboard = useDashboard(); const { showCharts, editing } = useDashboard();
const { showCharts, limit, editing } = dashboard;
const [max, setMax] = useState(limit);
const { get, useQuery } = useApi();
const { data, isLoading, error } = useQuery(['websites'], () =>
get('/websites', { includeTeams: 1 }),
);
const hasData = data && data?.data.length !== 0;
const { dir } = useLocale(); const { dir } = useLocale();
const { get, useQuery } = useApi();
function handleMore() { const { page, handlePageChange } = useApiFilter();
setMax(max + limit); const pageSize = 10;
} const {
data: result,
isLoading,
error,
} = useQuery(['websites', page, pageSize], () =>
get('/websites', { includeTeams: 1, page, pageSize }),
);
const { data, count } = result || {};
const hasData = data && data?.length !== 0;
return ( return (
<Page loading={isLoading} error={error}> <Page loading={isLoading} error={error}>
@ -48,19 +49,17 @@ export function Dashboard() {
)} )}
{hasData && ( {hasData && (
<> <>
{editing && <DashboardEdit websites={data?.data} />} {editing && <DashboardEdit />}
{!editing && ( {!editing && (
<WebsiteChartList websites={data?.data} showCharts={showCharts} limit={max} /> <>
)} <WebsiteChartList websites={data} showCharts={showCharts} limit={pageSize} />
{max < data.length && ( <Pager
<Flexbox justifyContent="center"> page={page}
<Button onClick={handleMore}> pageSize={pageSize}
<Icon rotate={dir === 'rtl' ? 180 : 0}> count={count}
<Icons.More /> onPageChange={handlePageChange}
</Icon> />
<Text>{formatMessage(labels.more)}</Text> </>
</Button>
</Flexbox>
)} )}
</> </>
)} )}

View File

@ -5,23 +5,33 @@ import { Button } from 'react-basics';
import { firstBy } from 'thenby'; import { firstBy } from 'thenby';
import useDashboard, { saveDashboard } from 'store/dashboard'; import useDashboard, { saveDashboard } from 'store/dashboard';
import useMessages from 'components/hooks/useMessages'; import useMessages from 'components/hooks/useMessages';
import useApi from 'components/hooks/useApi';
import styles from './DashboardEdit.module.css'; import styles from './DashboardEdit.module.css';
import Page from 'components/layout/Page';
const dragId = 'dashboard-website-ordering'; const dragId = 'dashboard-website-ordering';
export function DashboardEdit({ websites }) { export function DashboardEdit() {
const settings = useDashboard(); const settings = useDashboard();
const { websiteOrder } = settings; const { websiteOrder } = settings;
const { formatMessage, labels } = useMessages(); const { formatMessage, labels } = useMessages();
const [order, setOrder] = useState(websiteOrder || []); const [order, setOrder] = useState(websiteOrder || []);
const { get, useQuery } = useApi();
const {
data: result,
isLoading,
error,
} = useQuery(['websites'], () => get('/websites', { includeTeams: 1 }));
const { data: websites } = result || {};
const ordered = useMemo( const ordered = useMemo(() => {
() => if (websites) {
websites return websites
.map(website => ({ ...website, order: order.indexOf(website.id) })) .map(website => ({ ...website, order: order.indexOf(website.id) }))
.sort(firstBy('order')), .sort(firstBy('order'));
[websites, order], }
); return [];
}, [websites, order]);
function handleWebsiteDrag({ destination, source }) { function handleWebsiteDrag({ destination, source }) {
if (!destination || destination.index === source.index) return; if (!destination || destination.index === source.index) return;
@ -49,7 +59,7 @@ export function DashboardEdit({ websites }) {
} }
return ( return (
<> <Page loading={isLoading} error={error}>
<div className={styles.buttons}> <div className={styles.buttons}>
<Button onClick={handleSave} variant="action" size="small"> <Button onClick={handleSave} variant="action" size="small">
{formatMessage(labels.save)} {formatMessage(labels.save)}
@ -95,7 +105,7 @@ export function DashboardEdit({ websites }) {
</Droppable> </Droppable>
</DragDropContext> </DragDropContext>
</div> </div>
</> </Page>
); );
} }

View File

@ -42,8 +42,13 @@ export default async (
} = req.auth; } = req.auth;
if (req.method === 'GET') { if (req.method === 'GET') {
req.query.id = userId; if (!req.query.id) {
req.query.pageSize = 100; req.query.id = userId;
}
if (!req.query.pageSize) {
req.query.pageSize = 100;
}
return userWebsites(req as any, res); return userWebsites(req as any, res);
} }

View File

@ -142,6 +142,7 @@ export async function getReports(
...pageFilters, ...pageFilters,
...(options?.include && { include: options.include }), ...(options?.include && { include: options.include }),
}); });
const count = await prisma.client.report.count({ const count = await prisma.client.report.count({
where, where,
}); });

View File

@ -135,6 +135,7 @@ export async function getTeams(
...pageFilters, ...pageFilters,
...(options?.include && { include: options?.include }), ...(options?.include && { include: options?.include }),
}); });
const count = await prisma.client.team.count({ where }); const count = await prisma.client.team.count({ where });
return { data: teams, count, ...getParameters }; return { data: teams, count, ...getParameters };

View File

@ -107,7 +107,8 @@ export async function getWebsites(
...pageFilters, ...pageFilters,
...(options?.include && { include: options.include }), ...(options?.include && { include: options.include }),
}); });
const count = await prisma.client.website.count({ where });
const count = await prisma.client.website.count({ where: { ...where, deletedAt: null } });
return { data: websites, count, ...getParameters }; return { data: websites, count, ...getParameters };
} }