Updated ReportsTable.

This commit is contained in:
Mike Cao 2023-10-14 21:59:54 -07:00
parent 94a27226d1
commit 012d31e010
7 changed files with 12 additions and 81 deletions

View File

@ -5,14 +5,16 @@ import useFilterQuery from 'components/hooks/useFilterQuery';
import DataTable from 'components/common/DataTable';
import useCache from 'store/cache';
export default function ReportsDataTable() {
export default function ReportsDataTable({ websiteId }) {
const { get } = useApi();
const modified = useCache(state => state?.reports);
const queryResult = useFilterQuery(['reports', { modified }], params => get(`/reports`, params));
const queryResult = useFilterQuery(['reports', { websiteId, modified }], params =>
get(websiteId ? `/websites/${websiteId}/reports` : `/reports`, params),
);
return (
<DataTable queryResult={queryResult}>
{({ data }) => <ReportsTable data={data} showDomain={true} />}
{({ data }) => <ReportsTable data={data} showDomain={!websiteId} />}
</DataTable>
);
}

View File

@ -1,16 +1,17 @@
import { GridColumn, GridTable, Icon, Icons, Text, useBreakpoint } from 'react-basics';
import LinkButton from 'components/common/LinkButton';
import { useMessages } from 'components/hooks';
import useUser from 'components/hooks/useUser';
import { GridColumn, GridTable, Icon, Icons, Text } from 'react-basics';
import { REPORT_TYPES } from 'lib/constants';
import ReportDeleteButton from './ReportDeleteButton';
export function ReportsTable({ data = [], showDomain }) {
const { formatMessage, labels } = useMessages();
const { user } = useUser();
const breakpoint = useBreakpoint();
return (
<GridTable data={data}>
<GridTable data={data} cardMode={['xs', 'sm', 'md'].includes(breakpoint)}>
<GridColumn name="name" label={formatMessage(labels.name)} />
<GridColumn name="description" label={formatMessage(labels.description)} />
<GridColumn name="type" label={formatMessage(labels.type)}>

View File

@ -1,34 +1,12 @@
'use client';
import Page from 'components/layout/Page';
import Empty from 'components/common/Empty';
import ReportsTable from '../../../../(main)/reports/ReportsTable';
import { useMessages, useWebsiteReports } from 'components/hooks';
import Link from 'next/link';
import { Button, Flexbox, Icon, Icons, Text } from 'react-basics';
import { useMessages } from 'components/hooks';
import WebsiteHeader from '../WebsiteHeader';
import ReportsDataTable from 'app/(main)/reports/ReportsDataTable';
export function WebsiteReports({ websiteId }) {
const { formatMessage, labels } = useMessages();
const {
reports,
error,
isLoading,
deleteReport,
filter,
handleFilterChange,
handlePageChange,
handlePageSizeChange,
} = useWebsiteReports(websiteId);
const hasData = (reports && reports.data.length !== 0) || filter;
const handleDelete = async id => {
await deleteReport(id);
};
if (isLoading || error) {
return <Page loading={isLoading} error={error} />;
}
return (
<>
@ -43,17 +21,7 @@ export function WebsiteReports({ websiteId }) {
</Button>
</Link>
</Flexbox>
{hasData && (
<ReportsTable
data={reports}
onDelete={handleDelete}
onFilterChange={handleFilterChange}
onPageChange={handlePageChange}
onPageSizeChange={handlePageSizeChange}
filterValue={filter}
/>
)}
{!hasData && <Empty />}
<ReportsDataTable websiteId={websiteId} />
</>
);
}

View File

@ -20,4 +20,3 @@ export * from './useTheme';
export * from './useTimezone';
export * from './useUser';
export * from './useWebsite';
export * from './useWebsiteReports';

View File

@ -1,38 +0,0 @@
import { useState } from 'react';
import useApi from './useApi';
import useApiFilter from 'components/hooks/useApiFilter';
export function useWebsiteReports(websiteId) {
const [modified, setModified] = useState(Date.now());
const { get, useQuery, del, useMutation } = useApi();
const { mutate } = useMutation(reportId => del(`/reports/${reportId}`));
const { filter, page, pageSize, handleFilterChange, handlePageChange, handlePageSizeChange } =
useApiFilter();
const { data, error, isLoading } = useQuery(
['reports:website', { websiteId, modified, filter, page, pageSize }],
() => get(`/websites/${websiteId}/reports`, { websiteId, filter, page, pageSize }),
);
const deleteReport = id => {
mutate(id, {
onSuccess: () => {
setModified(Date.now());
},
});
};
return {
reports: data,
error,
isLoading,
deleteReport,
filter,
page,
pageSize,
handleFilterChange,
handlePageChange,
handlePageSizeChange,
};
}
export default useWebsiteReports;

View File

@ -20,7 +20,6 @@ export * from 'components/hooks/useTheme';
export * from 'components/hooks/useTimezone';
export * from 'components/hooks/useUser';
export * from 'components/hooks/useWebsite';
export * from 'components/hooks/useWebsiteReports';
export * from './app/(main)/settings/teams/[id]/TeamWebsiteAddForm';
export * from 'app/(main)/settings/teams/[id]/TeamEditForm';

View File

@ -1,3 +1,4 @@
import * as yup from 'yup';
import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
@ -9,7 +10,6 @@ export interface ReportsRequestQuery extends SearchFilter {
id: string;
}
import * as yup from 'yup';
const schema = {
GET: yup.object().shape({
id: yup.string().uuid().required(),