Skip to content

Commit 660a2c9

Browse files
committed
add selection Support to Discovery Table
add ExportToDataLibrary configurations
1 parent 7e3978d commit 660a2c9

File tree

5 files changed

+159
-8
lines changed

5 files changed

+159
-8
lines changed

packages/frontend/src/features/Discovery/ActionBar/ActionBar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from 'react';
2-
import { ExportToDataLibrary } from '../types';
2+
import { ExportFromDiscoveryActions } from '../types';
33
import { FiDownload as DownloadIcon } from 'react-icons/fi';
44
import DataLibraryActionButton from './DataLibraryActionButton';
55

66
interface ActionBarProps {
7-
config: ExportToDataLibrary;
7+
config: ExportFromDiscoveryActions;
88
}
99

1010
const ActionBar = ({ config }: ActionBarProps) => {
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import React, { useState } from 'react';
2+
import { Button, ComboboxItem, Group, Select } from '@mantine/core';
3+
import { useDataLibrary } from '@gen3/core';
4+
import { useDeepCompareEffect } from 'use-deep-compare';
5+
import { ExportToDataLibraryConfiguration } from '../types';
6+
7+
const extractListNameAndId = (data: any): ComboboxItem[] =>
8+
Object.keys(data).map((id) => ({ value: id, label: data[id].name }));
9+
10+
interface DiscoveryAddToDataLibraryProps {
11+
selectedResources: Record<string, any>;
12+
exportToDataLibrary: ExportToDataLibraryConfiguration;
13+
}
14+
15+
const DiscoveryDataLibrary = ({
16+
selectedResources,
17+
exportToDataLibrary,
18+
}: DiscoveryAddToDataLibraryProps) => {
19+
const [data, setData] = useState<ComboboxItem[]>([]);
20+
const [currentList, setCurrentList] = useState<ComboboxItem | null>(null);
21+
const [loading, setLoading] = useState(false);
22+
const [error, setError] = useState<Record<string, any> | null>(null);
23+
24+
const {
25+
dataLibrary,
26+
updateListInDataLibrary,
27+
addListToDataLibrary,
28+
isLoading,
29+
isError,
30+
error: dataLibraryError,
31+
} = useDataLibrary(false);
32+
33+
const saveToList = (
34+
listname: string,
35+
listId: string | undefined = undefined,
36+
) => {
37+
if (selectedResources.length === 0) return;
38+
const items = selectedResources.reduce(
39+
(acc: Record<string, any>, resource: Record<string, any>) => {
40+
const dataObjects = resource[exportToDataLibrary.dataObjectFieldName];
41+
const datasetId = resource[exportToDataLibrary.datesetIdFieldName];
42+
43+
const datafiles = dataObjects.reduce(
44+
(dataAcc: Record<string, any>, dataObject: any) => {
45+
const guid = dataObject[exportToDataLibrary.dataObjectIdField];
46+
return {
47+
...dataAcc,
48+
[guid]: {
49+
dataset_guid: datasetId,
50+
...dataObject,
51+
},
52+
};
53+
},
54+
{},
55+
);
56+
return {
57+
...acc,
58+
...datafiles,
59+
};
60+
},
61+
{},
62+
);
63+
64+
if (listId) {
65+
updateListInDataLibrary(listId, items);
66+
} else {
67+
addListToDataLibrary(items);
68+
}
69+
};
70+
71+
useDeepCompareEffect(() => {
72+
if (dataLibrary && !isError) {
73+
const listItems = extractListNameAndId(dataLibrary.lists);
74+
setData(listItems);
75+
}
76+
if (isError) {
77+
setError(dataLibraryError);
78+
}
79+
}, [dataLibrary, isError]);
80+
81+
// fetch the list
82+
83+
const notLoggedIn = false;
84+
85+
return (
86+
<React.Fragment>
87+
<Group>
88+
<Select
89+
data={data}
90+
onChange={(value, option) => setCurrentList(option)}
91+
placeholder={
92+
notLoggedIn
93+
? 'Login to save to a list'
94+
: 'Select/create a list to save to'
95+
}
96+
disabled={notLoggedIn}
97+
value={currentList?.value}
98+
/>
99+
<Button
100+
loading={loading}
101+
disabled={
102+
error !== null ||
103+
loading ||
104+
data?.length === 0 ||
105+
currentList === undefined ||
106+
selectedResources.length === 0 ||
107+
notLoggedIn
108+
}
109+
onClick={() => {
110+
if (currentList) {
111+
saveToList(currentList.label, currentList.value);
112+
} else {
113+
saveToList('New List');
114+
}
115+
}}
116+
>
117+
{' '}
118+
Save to List
119+
</Button>
120+
</Group>
121+
</React.Fragment>
122+
);
123+
};
124+
125+
export default DiscoveryDataLibrary;

packages/frontend/src/features/Discovery/DiscoveryIndexPanel.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ const DiscoveryIndexPanel = ({
9393
);
9494
}
9595

96+
const updateSelection = (ids: Array<string>) => {
97+
console.log('ids updated', ids);
98+
};
99+
96100
return (
97101
<div className="flex flex-col items-center p-4 w-full bg-base-lightest">
98102
<DiscoveryProvider discoveryIndexConfig={discoveryConfig}>
@@ -168,6 +172,7 @@ const DiscoveryIndexPanel = ({
168172
dataRequestStatus={dataRequestStatus}
169173
setPagination={setPagination}
170174
setSorting={setSorting}
175+
setSelection={updateSelection}
171176
pagination={pagination}
172177
sorting={sorting}
173178
/>

packages/frontend/src/features/Discovery/DiscoveryTable.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22
import {
33
MantineReactTable,
44
MRT_Cell,
@@ -39,6 +39,7 @@ interface DiscoveryTableProps {
3939
sorting: MRT_SortingState;
4040
setPagination: OnChangeFn<PaginationState>;
4141
setSorting: OnChangeFn<SortingState>;
42+
setSelection: (selection: Array<string>) => void;
4243
}
4344

4445
const DiscoveryTable = ({
@@ -47,6 +48,7 @@ const DiscoveryTable = ({
4748
dataRequestStatus,
4849
setSorting,
4950
setPagination,
51+
setSelection,
5052
pagination,
5153
sorting,
5254
}: DiscoveryTableProps) => {
@@ -108,7 +110,8 @@ const DiscoveryTable = ({
108110
enableColumnActions: false,
109111
enableStickyHeader: true,
110112
enableStickyFooter: true,
111-
113+
getRowId: (originalRow) =>
114+
config.studyField ? originalRow[config.studyField] : originalRow.id,
112115
// TODO: keep this to explore later
113116
// mantineTableContainerProps: ({ table }) => {
114117
// return {
@@ -168,6 +171,13 @@ const DiscoveryTable = ({
168171
},
169172
});
170173

174+
const { rowSelection } = table.getState().rowSelection;
175+
176+
useEffect(() => {
177+
//fetch data based on row selection state or something
178+
setSelection(Object.keys(rowSelection));
179+
}, [rowSelection, setSelection]);
180+
171181
return (
172182
<React.Fragment>
173183
<StudyDetails />

packages/frontend/src/features/Discovery/types.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ interface DiscoveryPageTitle {
202202
text: string;
203203
}
204204

205-
export interface DataLibraryActionButton {
205+
export interface ExportFromDiscoveryActionButton {
206206
type:
207207
| 'manifest'
208208
| 'zip'
@@ -239,12 +239,15 @@ export interface AuthorizationValues {
239239
menuText: string;
240240
}
241241

242-
export interface ExportToDataLibrary {
243-
buttons: DataLibraryActionButton[];
242+
export interface ExportFromDiscoveryActions {
243+
buttons: ExportFromDiscoveryActionButton[];
244244
enabled?: boolean;
245245
verifyExternalLogins?: boolean;
246246
loginRequireForAllButtons?: boolean;
247247
manifestFieldName?: string;
248+
dataObjectFieldName: string;
249+
datesetIdFieldName: string;
250+
dataObjectIdField: string;
248251
}
249252

250253
export interface DataAuthorization {
@@ -286,6 +289,13 @@ export interface ChartsSection {
286289
charts?: Record<string, SummaryChartWithField>;
287290
}
288291

292+
export interface ExportToDataLibraryConfiguration {
293+
enabled: boolean;
294+
dataObjectFieldName: string;
295+
datesetIdFieldName: string;
296+
dataObjectIdField: string;
297+
}
298+
289299
// TODO: Type the rest of the config
290300
export interface DiscoveryIndexConfig {
291301
guidType?: string;
@@ -295,7 +305,8 @@ export interface DiscoveryIndexConfig {
295305
advSearchFilters?: AdvancedSearchFilters;
296306
aiSearch?: boolean;
297307
pageTitle: DiscoveryPageTitle;
298-
exportToDataLibrary?: ExportToDataLibrary;
308+
exportFromDiscovery?: ExportFromDiscoveryActions;
309+
exportToDataLibrary: ExportToDataLibraryConfiguration;
299310
search?: SearchConfig;
300311
authorization: DataAuthorization;
301312
dataLoader?: DataLoader;

0 commit comments

Comments
 (0)