From 0ebd9ab969d3b6b0853f600ec103a412e8769c4f Mon Sep 17 00:00:00 2001 From: Mingze Date: Tue, 7 Jan 2020 17:43:23 -0800 Subject: [PATCH] feat(archive): Change data parsing for new data format (#1145) * feat(archive): Change data parsing for new data format * feat(archive): Address comments --- src/lib/viewers/archive/ArchiveExplorer.js | 41 ++++------------- .../__tests__/ArchiveExplorer-test-react.js | 46 ++++++------------- 2 files changed, 22 insertions(+), 65 deletions(-) diff --git a/src/lib/viewers/archive/ArchiveExplorer.js b/src/lib/viewers/archive/ArchiveExplorer.js index 8ea43cba7..5c4dfffdd 100644 --- a/src/lib/viewers/archive/ArchiveExplorer.js +++ b/src/lib/viewers/archive/ArchiveExplorer.js @@ -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'; @@ -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, }; @@ -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, @@ -84,14 +66,8 @@ class ArchiveExplorer extends React.Component { * @return {Array} 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)); }; /** @@ -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, }; diff --git a/src/lib/viewers/archive/__tests__/ArchiveExplorer-test-react.js b/src/lib/viewers/archive/__tests__/ArchiveExplorer-test-react.js index 7b6f53bd0..732433bac 100644 --- a/src/lib/viewers/archive/__tests__/ArchiveExplorer-test-react.js +++ b/src/lib/viewers/archive/__tests__/ArchiveExplorer-test-react.js @@ -18,23 +18,7 @@ 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', @@ -42,24 +26,22 @@ describe('lib/viewers/archive/ArchiveExplorer', () => { 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, }, ]; @@ -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, }); @@ -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]]); }); }); @@ -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', () => {