From 522be0af4f1fc66d8ae2c6989d6363ae89513f9c Mon Sep 17 00:00:00 2001 From: Fran McDade Date: Wed, 19 Feb 2025 10:34:23 +1000 Subject: [PATCH] feat: make dataset detail export to terra to select all in the initial view (#4383) --- .../anvil-cmg/common/aggregatedEntities.ts | 6 +-- .../anvil-cmg/common/viewModelBuilders.ts | 45 ++++++++++++++++++- .../dev/detail/dataset/export/export.ts | 2 +- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/app/apis/azul/anvil-cmg/common/aggregatedEntities.ts b/app/apis/azul/anvil-cmg/common/aggregatedEntities.ts index a563b8d61..b1c825f0a 100644 --- a/app/apis/azul/anvil-cmg/common/aggregatedEntities.ts +++ b/app/apis/azul/anvil-cmg/common/aggregatedEntities.ts @@ -54,7 +54,7 @@ export interface AggregatedDatasetResponse { * /index/libraries). */ export interface AggregatedDonor { - organism_type: string[]; + organism_type: (string | null)[]; phenotypic_sex: string[]; reported_ethnicity: string[]; } @@ -89,8 +89,8 @@ export interface AggregatedDiagnosisResponse { * /index/libraries). */ export interface AggregatedFile { - data_modality: string[]; - file_format: string; + data_modality: (string | null)[]; + file_format: string[]; file_id: string; file_type: string; } diff --git a/app/viewModelBuilders/azul/anvil-cmg/common/viewModelBuilders.ts b/app/viewModelBuilders/azul/anvil-cmg/common/viewModelBuilders.ts index bb4fa18a4..fb3470903 100644 --- a/app/viewModelBuilders/azul/anvil-cmg/common/viewModelBuilders.ts +++ b/app/viewModelBuilders/azul/anvil-cmg/common/viewModelBuilders.ts @@ -524,13 +524,13 @@ export function buildDatasetPath(datasetsResponse: DatasetsResponse): string { * @param viewContext - View context. * @returns model to be used as props for the ExportToTerra component. */ -export const builDatasetTerraExport = ( +export const buildDatasetTerraExport = ( datasetsResponse: DatasetsResponse, viewContext: ViewContext ): React.ComponentProps => { const { fileManifestState } = viewContext; // Get the initial filters. - const filters = getExportEntityFilters(datasetsResponse); + const filters = getExportTerraEntityFilters(datasetsResponse); // Grab the form facet. const formFacet = getFormFacets(fileManifestState); return { @@ -1262,6 +1262,31 @@ export function getExportSelectedDataSummary( ]); } +/** + * Returns the export to terra entity filters for the given datasets response. + * Includes dataset ID, donor organism type, and file format. + * @param datasetsResponse - Response model return from datasets API. + * @returns export to terra entity filters. + */ +function getExportTerraEntityFilters( + datasetsResponse: DatasetsResponse +): Filters { + return [ + ...getExportEntityFilters(datasetsResponse), + { + categoryKey: ANVIL_CMG_CATEGORY_KEY.DONOR_ORGANISM_TYPE, + value: processRawEntityArrayValue( + datasetsResponse.donors, + "organism_type" + ), + }, + { + categoryKey: ANVIL_CMG_CATEGORY_KEY.FILE_FILE_FORMAT, + value: processRawEntityArrayValue(datasetsResponse.files, "file_format"), + }, + ]; +} + /** * Returns the file summary facet, where facet terms are generated from the file summary. * @param fileFacet - File facet. @@ -1432,6 +1457,22 @@ function mapCurrentQuery( ]; } +/** + * Processes entities and extracts unique string or null values + * from a specified key that holds an array of (string | null)[]. + * @param responseValues - Response model return from API. + * @param key - The key whose values (arrays of string or null) will be processed. + * @returns A unique list of string or null values. + */ +function processRawEntityArrayValue< + T extends Record, + K extends keyof T, +>(responseValues: T[], key: K): (string | null)[] { + const flatValues = responseValues.flatMap((value) => value[key]); + const uniqueValues = new Set(flatValues); + return [...uniqueValues]; +} + /** * Renders configuration component children when the given authentication state is not authenticated. * @param _ - Unused. diff --git a/site-config/anvil-cmg/dev/detail/dataset/export/export.ts b/site-config/anvil-cmg/dev/detail/dataset/export/export.ts index 0db7435a9..eb12eb170 100644 --- a/site-config/anvil-cmg/dev/detail/dataset/export/export.ts +++ b/site-config/anvil-cmg/dev/detail/dataset/export/export.ts @@ -54,7 +54,7 @@ export const exportConfig: ExportConfig = { children: [ { component: C.ExportToTerra, - viewBuilder: V.builDatasetTerraExport, + viewBuilder: V.buildDatasetTerraExport, } as ComponentConfig, ], component: C.BackPageContentMainColumn,