Skip to content

Commit

Permalink
Merge pull request #1510 from BlueBrain/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Dinika authored Feb 22, 2024
2 parents bd2ff7c + 82f15c6 commit e86138a
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 51 deletions.
58 changes: 36 additions & 22 deletions src/shared/components/EditTableForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,18 @@ const EditTableForm: React.FC<{
return Object.assign(result, current);
}, {});

return Object.keys(mergedItem).map(title => ({
'@type': '',
name: title,
format: '',
enableSearch: false,
enableSort: false,
enableFilter: false,
}));
return Object.keys(mergedItem).map(title => {
const currentItemConfig = findCurrentColumnConfig(title);
return {
'@type': '',
name: title,
format: '',
enableSearch: false,
enableSort: false,
enableFilter: false,
...currentItemConfig,
};
});
}

const result = await querySparql(
Expand All @@ -295,14 +299,19 @@ const EditTableForm: React.FC<{
.sort((a, b) => {
return a.title > b.title ? 1 : -1;
})
.map(x => ({
'@type': 'text',
name: x.dataIndex,
format: '',
enableSearch: false,
enableSort: false,
enableFilter: false,
}));
.map(header => {
const currentHeaderConfig =
findCurrentColumnConfig(header.dataIndex) ?? {};
return {
'@type': 'text',
name: header.dataIndex,
format: '',
enableSearch: false,
enableSort: false,
enableFilter: false,
...currentHeaderConfig,
};
});
})
.catch(error => {
// Sometimes delta's error message can be in `name` or `reason` field.
Expand All @@ -322,12 +331,7 @@ const EditTableForm: React.FC<{
{
onSuccess: data => {
updateTableDataError(null);
if (
isNil(configuration) ||
(configuration as TableColumn[]).length === 0
) {
setConfiguration(data);
}
setConfiguration(data);
},
onError: (error: Error) => {
updateTableDataError(error);
Expand All @@ -339,6 +343,16 @@ const EditTableForm: React.FC<{
}
);

const findCurrentColumnConfig = (name: string) => {
if (Array.isArray(configuration)) {
return configuration.find(column => column.name === name);
}
if (configuration?.name === name) {
return { ...configuration };
}
return undefined;
};

const onChangeName = (event: any) => {
setName(event.target.value);
setNameError(false);
Expand Down
3 changes: 2 additions & 1 deletion src/shared/containers/ResourceViewActionsContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ const ResourceViewActionsContainer: React.FC<{
} else {
const localStorageObjects = toLocalStorageResources(
resource,
'resource-view'
'resource-view',
recordKey
);

selectedRowKeys = uniq([...selectedRowKeys, recordKey]);
Expand Down
8 changes: 6 additions & 2 deletions src/shared/hooks/useAccessDataForTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -499,13 +499,15 @@ export const useAccessDataForTable = (
return;
})
.process(async row => {
const recordKey = getStudioLocalStorageKey(row);
const fetchedRow = await fetchResourceForDownload(
getStudioLocalStorageKey(row),
nexus
);
const localStorageResources = toLocalStorageResources(
fetchedRow,
'studios'
'studios',
recordKey
);
rowKeysForLS.push(getStudioLocalStorageKey(row));

Expand Down Expand Up @@ -544,7 +546,8 @@ export const useAccessDataForTable = (
const deltaResource = await fetchResourceForDownload(recordKey, nexus);
const localStorageResource = toLocalStorageResources(
deltaResource,
'studios'
'studios',
recordKey
);
localStorageRowKeys = [...localStorageRowKeys, recordKey];
localStorageRows = [...localStorageRows, ...localStorageResource];
Expand Down Expand Up @@ -631,6 +634,7 @@ export const useAccessDataForTable = (
);
return result;
}

return {};
},
{
Expand Down
20 changes: 15 additions & 5 deletions src/shared/molecules/MyDataTable/MyDataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,16 @@ const MyDataTable: React.FC<TProps> = ({
setFetchingResources(true);
const expandedResource = await fetchResourceForDownload(recordKey, nexus);
setFetchingResources(false);
localStorageRows = toLocalStorageResources(expandedResource, 'my-data');
localStorageRows = toLocalStorageResources(
expandedResource,
'my-data',
recordKey
);
} else {
localStorageRows = toLocalStorageResources(record, 'my-data');
localStorageRows = toLocalStorageResources(record, 'my-data', recordKey);
}
toLocalStorageResources(record, 'my-data');
// TODO: Check if needed
toLocalStorageResources(record, 'my-data', recordKey);
let selectedRowKeys = dataPanelLS?.selectedRowKeys || [];
let selectedRows = dataPanelLS?.selectedRows || [];

Expand Down Expand Up @@ -488,10 +493,15 @@ const MyDataTable: React.FC<TProps> = ({
const fetchedRow = await fetchResourceForDownload(row._self, nexus);
localStorageResources = toLocalStorageResources(
fetchedRow,
'my-data'
'my-data',
row._self
);
} else {
localStorageResources = toLocalStorageResources(row, 'my-data');
localStorageResources = toLocalStorageResources(
row,
'my-data',
row._self
);
}
return localStorageResources;
});
Expand Down
54 changes: 44 additions & 10 deletions src/shared/utils/__tests__/datapanel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ describe('datapanel utilities', () => {
it('serializes resources with no distribution correctly to local storage object', () => {
const actualLSResources = toLocalStorageResources(
resourceWithoutDistrition,
'studios'
'studios',
resourceWithoutDistrition._self
);
const expectedParentDistributionValue = {
hasDistribution: false,
Expand Down Expand Up @@ -49,7 +50,8 @@ describe('datapanel utilities', () => {
it('serializes resource of type file with no distribution correctly', () => {
const actualLSResources = toLocalStorageResources(
fileResourceWithNoDistribution,
'my-data'
'my-data',
fileResourceWithNoDistribution._self
);
expect(actualLSResources.length).toEqual(1);
const expectedParentDistributionValue = {
Expand All @@ -66,7 +68,11 @@ describe('datapanel utilities', () => {

it('serializes resources with distribution array correctly to local storage object', () => {
const resource = resourceWithDistributionArray;
const serializedItems = toLocalStorageResources(resource, 'studios');
const serializedItems = toLocalStorageResources(
resource,
'studios',
resource._self
);

expect(serializedItems.length).toEqual(5);
const actualParentDistributionValue = serializedItems[0].distribution;
Expand All @@ -88,15 +94,23 @@ describe('datapanel utilities', () => {
label: undefined,
name: ['Sterling', 'Malory', 'Archer'],
};
const serializedItems = toLocalStorageResources(resource, 'studios');
const serializedItems = toLocalStorageResources(
resource,
'studios',
resource._self
);

expect(serializedItems.length).toEqual(5);
expect(serializedItems[0].name).toEqual('Sterling-Malory-Archer');
});

it('serializes correct distribution value for each distribution item in array', () => {
const resource = resourceWithDistributionArray;
const serializedItems = toLocalStorageResources(resource, 'studios');
const serializedItems = toLocalStorageResources(
resource,
'studios',
resource._self
);

const originalDistItems = resource.distribution;
const serializedDistItems = serializedItems.slice(1);
Expand All @@ -119,7 +133,11 @@ describe('datapanel utilities', () => {

it('serializes resources with distribution objects correctly', () => {
const resource = resourceWithDistributionObject;
const actualSerializedItems = toLocalStorageResources(resource, 'studios');
const actualSerializedItems = toLocalStorageResources(
resource,
'studios',
resource._self
);

expect(actualSerializedItems.length).toEqual(2);

Expand Down Expand Up @@ -158,7 +176,11 @@ describe('datapanel utilities', () => {
contentSize: 123,
},
};
const actualSerializedItems = toLocalStorageResources(resource, 'studios');
const actualSerializedItems = toLocalStorageResources(
resource,
'studios',
resource._self
);

expect(actualSerializedItems[1].distribution?.contentSize).toEqual(123);
});
Expand All @@ -171,14 +193,22 @@ describe('datapanel utilities', () => {
contentSize: [10, 20],
},
};
const actualSerializedItems = toLocalStorageResources(resource, 'studios');
const actualSerializedItems = toLocalStorageResources(
resource,
'studios',
resource._self
);

expect(actualSerializedItems[1].distribution?.contentSize).toEqual(30);
});

it('serializes resources when distribution is empty array', () => {
const resource = { ...resourceWithoutDistrition, distribution: [] };
const actualSerializedItems = toLocalStorageResources(resource, 'studios');
const actualSerializedItems = toLocalStorageResources(
resource,
'studios',
resource._self
);

expect(actualSerializedItems.length).toEqual(1);
const expectedDistributionValue = {
Expand All @@ -195,7 +225,11 @@ describe('datapanel utilities', () => {

it('serializes resources when distribution is empty object', () => {
const resource = { ...resourceWithoutDistrition, distribution: {} };
const actualSerializedItems = toLocalStorageResources(resource, 'studios');
const actualSerializedItems = toLocalStorageResources(
resource,
'studios',
resource._self
);

expect(actualSerializedItems.length).toEqual(2);
expect(actualSerializedItems[0].distribution).toBeDefined();
Expand Down
16 changes: 9 additions & 7 deletions src/shared/utils/datapanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import { parseURL } from './nexusParse';
const baseLocalStorageObject = (
resource: Resource,
source: string,
key: string,
keySuffix?: string
): Omit<TDataSource, 'distribution'> => {
return {
source,
key: keySuffix ? `${resource._self}-${keySuffix}` : resource._self,
key: keySuffix ? `${key}-${keySuffix}` : key,
_self: resource._self,
id: resource['@id'],
name: getResourceLabel(resource),
Expand All @@ -31,15 +32,16 @@ const baseLocalStorageObject = (

export const toLocalStorageResources = (
resource: Resource,
source: string
source: string,
key: string
): TDataSource[] => {
const resourceName = getResourceLabel(resource);
try {
// Case 1 - Resource has no distribution
if (isNil(resource.distribution)) {
return [
{
...baseLocalStorageObject(resource, source),
...baseLocalStorageObject(resource, source, key),
localStorageType: 'resource',
distribution: {
hasDistribution: false,
Expand All @@ -63,7 +65,7 @@ export const toLocalStorageResources = (

// First store an object for the parent resource.
localStorageObjs.push({
...baseLocalStorageObject(resource, source),
...baseLocalStorageObject(resource, source, key),
localStorageType: 'resource',
distributionItemsLength: resource.distribution.length,
distribution: {
Expand All @@ -79,7 +81,7 @@ export const toLocalStorageResources = (
// Now store an object for each distribution item
resource.distribution.forEach((distItem, index) => {
localStorageObjs.push({
...baseLocalStorageObject(resource, source, `${index}`),
...baseLocalStorageObject(resource, source, key, `${index}`),
localStorageType: 'distribution',
distribution: {
hasDistribution: true, // So, we don't download the distribution twice
Expand All @@ -100,7 +102,7 @@ export const toLocalStorageResources = (
return [
// First store an object for the parent resource.
{
...baseLocalStorageObject(resource, source),
...baseLocalStorageObject(resource, source, key),
localStorageType: 'resource',
distribution: {
hasDistribution: true,
Expand All @@ -113,7 +115,7 @@ export const toLocalStorageResources = (
},
// Now store an object for the distribution item.
{
...baseLocalStorageObject(resource, source, '1'),
...baseLocalStorageObject(resource, source, key, '1'),
localStorageType: 'distribution',
distribution: {
hasDistribution: true,
Expand Down
5 changes: 3 additions & 2 deletions src/subapps/dataExplorer/DataExplorerTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ export const DataExplorerTable = forwardRef<HTMLDivElement, TDataExplorerTable>(

const localStorageRows = toLocalStorageResources(
record,
DATA_EXPLORER_NAMESPACE
DATA_EXPLORER_NAMESPACE,
recordKey
);
let selectedRowKeys = dataPanelLS?.selectedRowKeys || [];
let selectedRows = dataPanelLS?.selectedRows || [];
Expand Down Expand Up @@ -177,7 +178,7 @@ export const DataExplorerTable = forwardRef<HTMLDivElement, TDataExplorerTable>(

if (selected) {
const results = changeRows.map(row =>
toLocalStorageResources(row, DATA_EXPLORER_NAMESPACE)
toLocalStorageResources(row, DATA_EXPLORER_NAMESPACE, row._self)
);
selectedRows = [...selectedRows, ...results.flat()];
selectedRowKeys = [...selectedRowKeys, ...changeRows.map(t => t._self)];
Expand Down
4 changes: 2 additions & 2 deletions src/subapps/search/containers/SearchContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ const SearchContainer: React.FC = () => {
resetAll();
};
const handleSelect = (record: Resource, selected: any) => {
const newRecords = toLocalStorageResources(record, layout);
const recordKey = record._self;
const newRecords = toLocalStorageResources(record, layout, recordKey);
const dataPanelLS: TResourceTableData = JSON.parse(
localStorage.getItem(DATA_PANEL_STORAGE)!
);
Expand Down Expand Up @@ -195,7 +195,7 @@ const SearchContainer: React.FC = () => {
) => {
const changedRowsLS: TDataSource[] = [];
changeRows.forEach(row => {
const localStorageRows = toLocalStorageResources(row, layout);
const localStorageRows = toLocalStorageResources(row, layout, row._self);
changedRowsLS.push(...localStorageRows);
});

Expand Down

0 comments on commit e86138a

Please sign in to comment.