From a11ebdae4a15e749c2ba8c1a5050afc1f1e8e02b Mon Sep 17 00:00:00 2001
From: stdavis
Date: Mon, 4 Nov 2024 15:29:20 -0700
Subject: [PATCH] feat: wire up selection to download panel
---
src/components/Download.tsx | 2 +-
src/components/ResultsGrid.tsx | 6 +++---
src/components/utils.test.ts | 33 ++++++++++++++++++++++++++++++++-
src/components/utils.ts | 10 +++++++++-
4 files changed, 45 insertions(+), 6 deletions(-)
diff --git a/src/components/Download.tsx b/src/components/Download.tsx
index 9f24c9a..20488b6 100644
--- a/src/components/Download.tsx
+++ b/src/components/Download.tsx
@@ -89,7 +89,7 @@ export default function Download({ eventIds }: { eventIds: string[] }): JSX.Elem
variant="secondary"
onPress={onDownloadClick}
>
- {state.isBusy ? : 'Download'}
+ {state.isBusy ? : `Download ${eventIds.length} record${eventIds.length > 1 ? 's' : ''}`}
{state.error && {state.error}
}
diff --git a/src/components/ResultsGrid.tsx b/src/components/ResultsGrid.tsx
index 6d8c77d..54475bb 100644
--- a/src/components/ResultsGrid.tsx
+++ b/src/components/ResultsGrid.tsx
@@ -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;
@@ -137,7 +137,7 @@ export default function ResultsGrid() {
return {error.message};
}
- 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') {
@@ -238,7 +238,7 @@ export default function ResultsGrid() {
-
+
>
diff --git a/src/components/utils.test.ts b/src/components/utils.test.ts
index 6fd3552..fbc3e07 100644
--- a/src/components/utils.test.ts
+++ b/src/components/utils.test.ts
@@ -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[] = [
@@ -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 = 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([]);
+ });
+});
diff --git a/src/components/utils.ts b/src/components/utils.ts
index 5d036dc..b39043a 100644
--- a/src/components/utils.ts
+++ b/src/components/utils.ts
@@ -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';
@@ -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[]);
+}