Skip to content

Commit

Permalink
Merge pull request #324 from Vizzuality/SKY30-487-fe-implement-a-way-…
Browse files Browse the repository at this point in the history
…to-indicate-when-the-data-of-a-table-is-loading

Indicate to the user when the tables are loading
  • Loading branch information
clementprdhomme authored Oct 15, 2024
2 parents 1956698 + 9d468eb commit ad4950f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 12 deletions.
5 changes: 1 addition & 4 deletions frontend/src/components/ui/skeleton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { cn } from '@/lib/classnames';

function Skeleton({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) {
return (
<div
className={cn('animate-pulse rounded-md bg-slate-100 dark:bg-slate-800', className)}
{...props}
/>
<div className={cn('animate-pulse bg-slate-100 dark:bg-slate-800', className)} {...props} />
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,12 @@ export const useData = (
) => {
const locale = useLocale();

const { data: locationType, isSuccess: isLocationSuccess } = useGetLocations<string>(
const {
data: locationType,
isSuccess: isLocationSuccess,
isLoading: isLocationLoading,
isFetching: isLocationFetching,
} = useGetLocations<string>(
{
locale,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down Expand Up @@ -343,7 +348,7 @@ export const useData = (
}
}

const { data } = useGetProtectionCoverageStats<
const { data, isLoading, isFetching } = useGetProtectionCoverageStats<
[GlobalRegionalTableColumns[], ProtectionCoverageStatListResponseMetaPagination]
>(
{
Expand Down Expand Up @@ -467,5 +472,9 @@ export const useData = (
}
);

return data;
return {
data,
isLoading: isLoading || isLocationLoading,
isFetching: isFetching || isLocationFetching,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { usePreviousImmediate } from 'rooks';

import FiltersButton from '@/components/filters-button';
import TooltipButton from '@/components/tooltip-button';
import { Skeleton } from '@/components/ui/skeleton';
import Table from '@/containers/map/content/details/table';
import { useSyncMapContentSettings } from '@/containers/map/sync-settings';
import { FCWithMessages } from '@/types';
Expand Down Expand Up @@ -47,7 +48,11 @@ const GlobalRegionalTable: FCWithMessages = () => {
pageSize: 100,
});

const [data, { total }] = useData(
const {
data: [data, { total }],
isLoading,
isFetching,
} = useData(
locationCode as string,
tab === 'marine' || tab === 'terrestrial' ? tab : null,
sorting,
Expand All @@ -68,6 +73,34 @@ const GlobalRegionalTable: FCWithMessages = () => {
setPagination((prevPagination) => ({ ...prevPagination, pageIndex: 0 }));
}, [filters, sorting]);

// While the data is loading, we're showing a table with skeletons
if (isLoading || isFetching) {
const newColumns = columns.map((column) => ({
...column,
cell: () => (
<div className="text-4xl font-bold">
<Skeleton className="my-3 h-4 w-full" />
</div>
),
}));

const newData = data.length > 0 ? data : [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}];

return (
<Table
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
columns={newColumns}
data={newData}
sorting={sorting}
onSortingChange={setSorting}
pagination={pagination}
onPaginationChange={setPagination}
rowCount={total ?? 0}
/>
);
}

return (
<Table
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,9 @@ export const useData = (
);

// If the user isn't filtering, only one request is sufficient to get all of the table's data
const { data } = useGetPas<[NationalHighseasTableColumns[], PaListResponseMetaPagination]>(
const { data, isLoading, isFetching } = useGetPas<
[NationalHighseasTableColumns[], PaListResponseMetaPagination]
>(
{
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand Down Expand Up @@ -748,5 +750,5 @@ export const useData = (
}
);

return data;
return { data, isLoading, isFetching };
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { usePreviousImmediate } from 'rooks';

import FiltersButton from '@/components/filters-button';
import TooltipButton from '@/components/tooltip-button';
import { Skeleton } from '@/components/ui/skeleton';
import Table from '@/containers/map/content/details/table';
import { useSyncMapContentSettings } from '@/containers/map/sync-settings';
import { FCWithMessages } from '@/types';
Expand Down Expand Up @@ -47,7 +48,11 @@ const NationalHighseasTable: FCWithMessages = () => {
pageSize: 100,
});

const [data, { total }] = useData(
const {
data: [data, { total }],
isLoading,
isFetching,
} = useData(
locationCode as string,
tab === 'marine' || tab === 'terrestrial' ? tab : null,
sorting,
Expand All @@ -68,6 +73,34 @@ const NationalHighseasTable: FCWithMessages = () => {
setPagination((prevPagination) => ({ ...prevPagination, pageIndex: 0 }));
}, [filters, sorting]);

// While the data is loading, we're showing a table with skeletons
if (isLoading || isFetching) {
const newColumns = columns.map((column) => ({
...column,
cell: () => (
<div className="text-4xl font-bold">
<Skeleton className="my-3 h-4 w-full" />
</div>
),
}));

const newData = data.length > 0 ? data : [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}];

return (
<Table
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
columns={newColumns}
data={newData}
sorting={sorting}
onSortingChange={setSorting}
pagination={pagination}
onPaginationChange={setPagination}
rowCount={total ?? 0}
/>
);
}

return (
<Table
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ type Props = AppProps & {
const App: React.FC<AppProps> = ({ Component, pageProps }: Props) => {
// Never ever instantiate the client outside a component, hook or callback as it can leak data
// between users
const [queryClient] = useState(() => new QueryClient());
const [queryClient] = useState(
() => new QueryClient({ defaultOptions: { queries: { refetchOnWindowFocus: false } } })
);

const router = useRouter();

Expand Down

0 comments on commit ad4950f

Please sign in to comment.