Skip to content

Commit

Permalink
feat: implement download members as CSV + add caching to table
Browse files Browse the repository at this point in the history
  • Loading branch information
VinayakRugvedi committed Sep 22, 2024
1 parent d7003c6 commit 174fa89
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
36 changes: 23 additions & 13 deletions frontend/src/components/partnerMapswipeStats/groupMembers.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useEffect, useState } from 'react';
import { useState } from 'react';
import { FormattedMessage } from 'react-intl';
import { useReactTable, getCoreRowModel, flexRender } from '@tanstack/react-table';
import { useParams } from 'react-router-dom';
import { useQuery } from '@tanstack/react-query';
import ReactPlaceholder from 'react-placeholder';

import { fetchLocalJSONAPI } from '../../network/genericJSONRequest';
import { BanIcon, CircleExclamationIcon } from '../svgIcons';
import { BanIcon, CircleExclamationIcon, DownloadIcon } from '../svgIcons';
import { PaginatorLine } from '../paginator';
import { API_URL } from '../../config';
import messages from './messages';
import './groupMembers.css';

Expand Down Expand Up @@ -71,8 +72,8 @@ export const GroupMembers = () => {

const rows = 10;

const { isLoading, isError, data, isRefetching, refetch } = useQuery({
queryKey: ['partners-mapswipe-statistics-group-members', partnerPermalink],
const { isLoading, isError, data, isFetching, isPreviousData } = useQuery({
queryKey: ['partners-mapswipe-statistics-group-members', partnerPermalink, pageNumber],
queryFn: async () => {
const response = await fetchLocalJSONAPI(
`partners/${partnerPermalink}/general-statistics/?limit=${rows}&offset=${
Expand All @@ -81,12 +82,10 @@ export const GroupMembers = () => {
);
return response;
},
keepPreviousData: true,
staleTime: 5 * 60 * 1000,
});

useEffect(() => {
refetch();
}, [pageNumber]);

const table = useReactTable({
columns: COLUMNS,
data: data?.members ?? [],
Expand All @@ -95,13 +94,24 @@ export const GroupMembers = () => {
columnResizeDirection: 'ltr',
});

const isEmpty = !isLoading && !isRefetching && !isError && data?.members.length === 0;
const isEmpty = !isLoading && !isFetching && !isError && data?.members.length === 0;

return (
<div>
<h3 className="f2 fw6 ttu barlow-condensed blue-dark mt0 pt2 mb4">
<FormattedMessage {...messages.groupMembers} />
</h3>
<div className="flex items-center gap-1 mb4">
<h3 className="f2 fw6 ttu barlow-condensed blue-dark mt0 pt2 mb0">
<FormattedMessage {...messages.groupMembers} />
</h3>
<a
href={`${API_URL}partners/${partnerPermalink}/general-statistics/?downloadAsCSV=true`}
download
className="blue-dark pt2"
>
<DownloadIcon className="mr2 self-center" />
<FormattedMessage {...messages.downloadMembersAsCSV} />
</a>
</div>

<div className="bg-white br1 shadow-6">
<div className="overflow-auto ph4 pv2">
<table className="f6 w-100 center" cellSpacing="0">
Expand Down Expand Up @@ -139,7 +149,7 @@ export const GroupMembers = () => {
<tbody className="lh-copy">
<ReactPlaceholder
customPlaceholder={<GroupMembersPlaceholder />}
ready={!isLoading && !isRefetching}
ready={!isLoading && !isFetching}
>
{table.getRowModel().rows.map((row) => (
<tr key={row.id}>
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/components/partnerMapswipeStats/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,8 @@ export default defineMessages({
id: 'management.partners.stats.mapswipe.area.compare',
defaultMessage: 'Compare',
},
downloadMembersAsCSV: {
id: 'management.partners.stats.mapswipe.groupMembers.download',
defaultMessage: 'Download CSV',
},
});

0 comments on commit 174fa89

Please sign in to comment.