Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Added paging to dashboard.

Mike Cao33ffa0b3d13a
diff --git a/src/components/pages/dashboard/Dashboard.js b/src/components/pages/dashboard/Dashboard.js
index 8248cc81a..2294b8be8 100644
--- a/src/components/pages/dashboard/Dashboard.js
+++ b/src/components/pages/dashboard/Dashboard.js
@@ -1,8 +1,8 @@
-import { useState } from 'react';
-import { Button, Icon, Icons, Text, Flexbox } from 'react-basics';
+import { Button, Icon, Icons, Text } from 'react-basics';
import Link from 'next/link';
import Page from 'components/layout/Page';
import PageHeader from 'components/layout/PageHeader';
+import Pager from 'components/common/Pager';
import WebsiteChartList from 'components/pages/websites/WebsiteChartList';
import DashboardSettingsButton from 'components/pages/dashboard/DashboardSettingsButton';
import DashboardEdit from 'components/pages/dashboard/DashboardEdit';
@@ -11,23 +11,24 @@ import useApi from 'components/hooks/useApi';
import useDashboard from 'store/dashboard';
import useMessages from 'components/hooks/useMessages';
import useLocale from 'components/hooks/useLocale';
+import useApiFilter from 'components/hooks/useApiFilter';
export function Dashboard() {
const { formatMessage, labels, messages } = useMessages();
- const dashboard = useDashboard();
- const { showCharts, limit, editing } = dashboard;
- const [max, setMax] = useState(limit);
+ const { showCharts, editing } = useDashboard();
+ const { dir } = useLocale();
const { get, useQuery } = useApi();
- const { data, isLoading, error } = useQuery(['websites'], () =>
- get('/websites', { includeTeams: 1 }),
+ const { page, handlePageChange } = useApiFilter();
+ const pageSize = 10;
+ const {
+ data: result,
+ isLoading,
+ error,
+ } = useQuery(['websites', page, pageSize], () =>
+ get('/websites', { includeTeams: 1, page, pageSize }),
);
- const hasData = data && data?.data.length !== 0;
-
- const { dir } = useLocale();
-
- function handleMore() {
- setMax(max + limit);
- }
+ const { data, count } = result || {};
+ const hasData = data && data?.length !== 0;
return (
<Page loading={isLoading} error={error}>
@@ -48,19 +49,17 @@ export function Dashboard() {
)}
{hasData && (
<>
- {editing && <DashboardEdit websites={data?.data} />}
+ {editing && <DashboardEdit />}
{!editing && (
- <WebsiteChartList websites={data?.data} showCharts={showCharts} limit={max} />
- )}
- {max < data.length && (
- <Flexbox justifyContent="center">
- <Button onClick={handleMore}>
- <Icon rotate={dir === 'rtl' ? 180 : 0}>
- <Icons.More />
- </Icon>
- <Text>{formatMessage(labels.more)}</Text>
- </Button>
- </Flexbox>
+ <>
+ <WebsiteChartList websites={data} showCharts={showCharts} limit={pageSize} />
+ <Pager
+ page={page}
+ pageSize={pageSize}
+ count={count}
+ onPageChange={handlePageChange}
+ />
+ </>
)}
</>
)}
diff --git a/src/components/pages/dashboard/DashboardEdit.js b/src/components/pages/dashboard/DashboardEdit.js
index 4eb259d61..f628599fe 100644
--- a/src/components/pages/dashboard/DashboardEdit.js
+++ b/src/components/pages/dashboard/DashboardEdit.js
@@ -5,23 +5,33 @@ import { Button } from 'react-basics';
import { firstBy } from 'thenby';
import useDashboard, { saveDashboard } from 'store/dashboard';
import useMessages from 'components/hooks/useMessages';
+import useApi from 'components/hooks/useApi';
import styles from './DashboardEdit.module.css';
+import Page from 'components/layout/Page';
const dragId = 'dashboard-website-ordering';
-export function DashboardEdit({ websites }) {
+export function DashboardEdit() {
const settings = useDashboard();
const { websiteOrder } = settings;
const { formatMessage, labels } = useMessages();
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(
- () =>
- websites
+ const ordered = useMemo(() => {
+ if (websites) {
+ return websites
.map(website => ({ ...website, order: order.indexOf(website.id) }))
- .sort(firstBy('order')),
- [websites, order],
- );
+ .sort(firstBy('order'));
+ }
+ return [];
+ }, [websites, order]);
function handleWebsiteDrag({ destination, source }) {
if (!destination || destination.index === source.index) return;
@@ -49,7 +59,7 @@ export function DashboardEdit({ websites }) {
}
return (
- <>
+ <Page loading={isLoading} error={error}>
<div className={styles.buttons}>
<Button onClick={handleSave} variant="action" size="small">
{formatMessage(labels.save)}
@@ -95,7 +105,7 @@ export function DashboardEdit({ websites }) {
</Droppable>
</DragDropContext>
</div>
- </>
+ </Page>
);
}
diff --git a/src/pages/api/websites/index.ts b/src/pages/api/websites/index.ts
index d724f12f4..d6009caf6 100644
--- a/src/pages/api/websites/index.ts
+++ b/src/pages/api/websites/index.ts
@@ -42,8 +42,13 @@ export default async (
} = req.auth;
if (req.method === 'GET') {
- req.query.id = userId;
- req.query.pageSize = 100;
+ if (!req.query.id) {
+ req.query.id = userId;
+ }
+
+ if (!req.query.pageSize) {
+ req.query.pageSize = 100;
+ }
return userWebsites(req as any, res);
}
diff --git a/src/queries/admin/report.ts b/src/queries/admin/report.ts
index a053ba92d..59eb70356 100644
--- a/src/queries/admin/report.ts
+++ b/src/queries/admin/report.ts
@@ -142,6 +142,7 @@ export async function getReports(
...pageFilters,
...(options?.include && { include: options.include }),
});
+
const count = await prisma.client.report.count({
where,
});
diff --git a/src/queries/admin/team.ts b/src/queries/admin/team.ts
index 284b218e2..cf731ad42 100644
--- a/src/queries/admin/team.ts
+++ b/src/queries/admin/team.ts
@@ -135,6 +135,7 @@ export async function getTeams(
...pageFilters,
...(options?.include && { include: options?.include }),
});
+
const count = await prisma.client.team.count({ where });
return { data: teams, count, ...getParameters };
diff --git a/src/queries/admin/website.ts b/src/queries/admin/website.ts
index 3d0c773b6..cf4570cfc 100644
--- a/src/queries/admin/website.ts
+++ b/src/queries/admin/website.ts
@@ -107,7 +107,8 @@ export async function getWebsites(
...pageFilters,
...(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 };
}