Skip to content

Commit

Permalink
feat(archive): Change data parsing for new data format (#1145)
Browse files Browse the repository at this point in the history
* feat(archive): Change data parsing for new data format

* feat(archive): Address comments
  • Loading branch information
Mingze authored and mergify[bot] committed Jan 8, 2020
1 parent 55c9044 commit 0ebd9ab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 65 deletions.
41 changes: 8 additions & 33 deletions src/lib/viewers/archive/ArchiveExplorer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import getProp from 'lodash/get';
import elementsMessages from 'box-elements-messages'; // eslint-disable-line
import intlLocaleData from 'react-intl-locale-data'; // eslint-disable-line
import Internationalize from 'box-ui-elements/es/elements/common/Internationalize';
Expand Down Expand Up @@ -32,27 +31,7 @@ class ArchiveExplorer extends React.Component {
name: PropTypes.string.isRequired,
modified_at: PropTypes.string.isRequired,
size: PropTypes.number.isRequired,
path_collection: PropTypes.shape({
total_count: PropTypes.number,
entries: PropTypes.arrayOf(
PropTypes.shape({
type: PropTypes.string,
absolute_path: PropTypes.string,
name: PropTypes.string,
}),
),
}),
parent: PropTypes.string,
item_collection: PropTypes.shape({
total_count: PropTypes.number,
entries: PropTypes.arrayOf(
PropTypes.shape({
type: PropTypes.string,
absolute_path: PropTypes.string.isRequired,
name: PropTypes.string,
}),
).isRequired,
}).isRequired,
item_collection: PropTypes.arrayOf(PropTypes.string),
}),
).isRequired,
};
Expand All @@ -68,7 +47,10 @@ class ArchiveExplorer extends React.Component {
addLocaleData(intlLocaleData);

this.state = {
fullPath: props.itemCollection.find(info => !info.parent).absolute_path,
// Trying to find the root folder
// The only way to tell what the root folder is
// is by comparing the name and absolute path, which differs by '/'
fullPath: props.itemCollection.find(info => info.name === info.absolute_path.slice(0, -1)).absolute_path,
searchQuery: '',
sortBy: '',
sortDirection: SortDirection.ASC,
Expand All @@ -84,14 +66,8 @@ class ArchiveExplorer extends React.Component {
* @return {Array<Object>} filtered itemlist for target folder
*/
getItemList = (itemCollection, fullPath) => {
const folderInfo = itemCollection.find(item => item.absolute_path === fullPath);
const subItems = getProp(folderInfo, 'item_collection.entries');
if (!subItems) {
return [];
}
const subItemsPath = subItems.map(item => item.absolute_path);

return itemCollection.filter(item => subItemsPath.includes(item.absolute_path));
const { item_collection: folderItems = [] } = itemCollection.find(item => item.absolute_path === fullPath);
return itemCollection.filter(item => folderItems.includes(item.absolute_path));
};

/**
Expand All @@ -115,8 +91,7 @@ class ArchiveExplorer extends React.Component {
'data-resin-target': type,
},
},
// TODO: fix when conversion changes it to standard date format
[KEY_MODIFIED_AT]: `20${modifiedAt}`,
[KEY_MODIFIED_AT]: modifiedAt,
[KEY_SIZE]: type === 'folder' ? null : size,
...rest,
};
Expand Down
46 changes: 14 additions & 32 deletions src/lib/viewers/archive/__tests__/ArchiveExplorer-test-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,30 @@ describe('lib/viewers/archive/ArchiveExplorer', () => {
name: 'test',
modified_at: '19-Dec-02 16:43',
size: 0,
path_collection: { total_count: 0, entries: [] },
parent: null,
item_collection: {
total_count: 3,
entries: [
{
type: 'file',
absolute_path: 'test/csv-level-1.csv',
name: 'csv-level-1.csv',
},
{
type: 'file',
absolute_path: 'test/test-level-1.jpg',
name: 'test-level-1.jpg',
},
],
},
item_collection: ['test/csv-level-1.csv', 'test/subfolder/'],
},
{
type: 'file',
absolute_path: 'test/csv-level-1.csv',
name: 'csv-level-1.csv',
modified_at: '19-Nov-04 16:11',
size: 133,
path_collection: {
total_count: 1,
entries: [{ type: 'folder', absolute_path: 'test/', name: 'test' }],
},
parent: 'test',
item_collection: null,
},
{
type: 'folder',
absolute_path: 'test/subfolder/',
name: 'subfolder',
modified_at: '19-Dec-02 16:43',
size: 0,
item_collection: ['test/test-level-2.jpg'],
},
{
type: 'file',
absolute_path: 'test/test-level-1.jpg',
absolute_path: 'test/test-level-2.jpg',
name: 'test-level-1.jpg',
modified_at: '19-Nov-08 15:08',
size: 57379,
path_collection: {
total_count: 1,
entries: [{ type: 'folder', absolute_path: 'test/', name: 'test' }],
},
parent: 'test',
item_collection: null,
},
];
Expand Down Expand Up @@ -122,7 +104,7 @@ describe('lib/viewers/archive/ArchiveExplorer', () => {
'data-resin-target': type,
},
},
[KEY_MODIFIED_AT]: `20${modifiedAt}`,
[KEY_MODIFIED_AT]: modifiedAt,
[KEY_SIZE]: type === 'folder' ? null : size,
...rest,
});
Expand Down Expand Up @@ -164,8 +146,8 @@ describe('lib/viewers/archive/ArchiveExplorer', () => {
const itemList = component.instance().getSearchResult(data, 'level-1');
const fuzzyList = component.instance().getSearchResult(data, 'leel1');

expect(itemList).to.eql([data[1], data[2]]);
expect(fuzzyList).to.eql([data[1], data[2]]);
expect(itemList).to.eql([data[1], data[3]]);
expect(fuzzyList).to.eql([data[1], data[3]]);
});
});

Expand All @@ -190,7 +172,7 @@ describe('lib/viewers/archive/ArchiveExplorer', () => {
instance.handleSort({ sortBy: 'size', sortDirection: 'ASC' });
const sortedList = instance.sortItemList(itemList);

expect(sortedList[0]).to.equal(data[1]);
expect(sortedList[0]).to.equal(data[2]);
});

it('should sort itemList by name and be in DESC order', () => {
Expand Down

0 comments on commit 0ebd9ab

Please sign in to comment.