Skip to content

Commit

Permalink
feat: wire up selection to download panel
Browse files Browse the repository at this point in the history
  • Loading branch information
stdavis committed Nov 29, 2024
1 parent 18e69fb commit a11ebda
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/components/Download.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default function Download({ eventIds }: { eventIds: string[] }): JSX.Elem
variant="secondary"
onPress={onDownloadClick}
>
{state.isBusy ? <Spinner /> : 'Download'}
{state.isBusy ? <Spinner /> : `Download ${eventIds.length} record${eventIds.length > 1 ? 's' : ''}`}
</Button>
</p>
{state.error && <div className="text-sm text-rose-600 forced-colors:text-[Mark]">{state.error}</div>}
Expand Down
6 changes: 3 additions & 3 deletions src/components/ResultsGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useSelection } from './contexts/SelectionProvider';
import Download from './Download';
import { getGridQuery, removeIrrelevantWhiteSpace } from './queryHelpers';
import { Cell, Column, Row, Table, TableHeader } from './Table';
import { getResultOidsFromStationIds, getStationIdsFromResultRows } from './utils';
import { getEventIdsForDownload, getResultOidsFromStationIds, getStationIdsFromResultRows } from './utils';

const STATION_NAME = 'STATION_NAME';
export type Result = Record<string, string | number | null>;
Expand Down Expand Up @@ -137,7 +137,7 @@ export default function ResultsGrid() {
return <span>{error.message}</span>;
}

const eventIds = data?.length ? (data.map((row) => row[config.fieldNames.ESRI_OID]) as string[]) : ([] as string[]);
const eventIdsForDownload = getEventIdsForDownload(data, selectedKeys);

const onSelectionChange = (selectedOids: Selection) => {
if (selectedOids === 'all') {
Expand Down Expand Up @@ -238,7 +238,7 @@ export default function ResultsGrid() {
</Table>
</TabPanel>
<TabPanel id="download">
<Download eventIds={eventIds} />
<Download eventIds={eventIdsForDownload} />
</TabPanel>
</Tabs>
</>
Expand Down
33 changes: 32 additions & 1 deletion src/components/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest';
import config from '../config';
import { Result } from './ResultsGrid';
import { getResultOidsFromStationIds, getStationIdsFromResultRows } from './utils';
import { getEventIdsForDownload, getResultOidsFromStationIds, getStationIdsFromResultRows } from './utils';

describe('getStationIdsFromResultRows', () => {
const data: Result[] = [
Expand Down Expand Up @@ -40,3 +40,34 @@ describe('getResultOidsFromStationIds', () => {
expect(result).toEqual(new Set());
});
});

describe('getEventIdsForDownload', () => {
const data: Result[] = [
{ [config.fieldNames.ESRI_OID]: '1', [config.fieldNames.EVENT_ID]: 'E1' },
{ [config.fieldNames.ESRI_OID]: '2', [config.fieldNames.EVENT_ID]: 'E2' },
];

it('should return all event IDs if selectedKeys is "all"', () => {
const selectedKeys = 'all';
const result = getEventIdsForDownload(data, selectedKeys);
expect(result).toEqual(['E1', 'E2']);
});

it('should return all event IDs if selectedKeys is an empty set', () => {
const selectedKeys: Set<string> = new Set();
const result = getEventIdsForDownload(data, selectedKeys);
expect(result).toEqual(['E1', 'E2']);
});

it('should return correct event IDs for selected OIDs', () => {
const selectedKeys = new Set(['1']);
const result = getEventIdsForDownload(data, selectedKeys);
expect(result).toEqual(['E1']);
});

it('should return an empty array if no matching OIDs', () => {
const selectedKeys = new Set(['3']);
const result = getEventIdsForDownload(data, selectedKeys);
expect(result).toEqual([]);
});
});
10 changes: 9 additions & 1 deletion src/components/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { composeRenderProps } from 'react-aria-components';
import { composeRenderProps, Selection } from 'react-aria-components';
import { twMerge } from 'tailwind-merge';
import { tv } from 'tailwind-variants';
import config from '../config';
Expand Down Expand Up @@ -36,3 +36,11 @@ export function getResultOidsFromStationIds(data: Result[] | undefined, selected
.map((row) => row[config.fieldNames.ESRI_OID] as string),
);
}

export function getEventIdsForDownload(data: Result[] | undefined, selectedKeys: Selection): string[] {
return selectedKeys === 'all' || selectedKeys.size === 0
? (data?.map((row) => row[config.fieldNames.EVENT_ID]) as string[])
: (data
?.filter((row) => selectedKeys.has(row[config.fieldNames.ESRI_OID] as string))
.map((row) => row[config.fieldNames.EVENT_ID]) as string[]);
}

0 comments on commit a11ebda

Please sign in to comment.