Skip to content

Commit

Permalink
FIX Save fetched files so details button works after pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jul 2, 2021
1 parent a287291 commit 8c7dd70
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions client/src/containers/AssetAdmin/AssetAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class AssetAdmin extends Component {
this.handleUploadQueue = this.handleUploadQueue.bind(this);
this.handleCreateFolder = this.handleCreateFolder.bind(this);
this.handleMoveFilesSuccess = this.handleMoveFilesSuccess.bind(this);
// this is used as a fallback to the regular graphql query which is limited to
// current pagination limit this is used when we need to access previously fetched
// data after a second subsequent graphql query has updated props.files
this.fetchedFiles = {};
}

componentWillReceiveProps(props) {
Expand All @@ -60,6 +64,14 @@ class AssetAdmin extends Component {
}
}

componentDidUpdate(prevProps) {
// Save file data in case we need to use it later after paginating the gallery
prevProps.files.forEach(file => {
this.fetchedFiles[file.id] = file;
});
return true;
}

/**
* Get logical folder ID to display
*/
Expand Down Expand Up @@ -524,8 +536,11 @@ class AssetAdmin extends Component {
*/
findFile(fileId) {
const allFiles = this.getFiles();

return allFiles.find((item) => item.id === parseInt(fileId, 10));
const file = allFiles.find((item) => item.id === parseInt(fileId, 10));
if (file) {
return file;
}
return this.fetchedFiles.hasOwnProperty(fileId) ? this.fetchedFiles[fileId] : undefined;
}

handleUpload() {
Expand Down
35 changes: 35 additions & 0 deletions client/src/containers/AssetAdmin/tests/AssetAdmin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ jest.mock('containers/BulkDeleteConfirmation/BulkDeleteConfirmation');
import React from 'react';
import ReactTestUtils from 'react-dom/test-utils';
import { Component as AssetAdmin } from '../AssetAdmin';
import Enzyme, { shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16/build/index';

Enzyme.configure({ adapter: new Adapter() });

function getMockFile(id) {
return {
Expand Down Expand Up @@ -274,6 +278,37 @@ describe('AssetAdmin', () => {
});
});

describe('findFiles', () => {
beforeEach(() => {
props.files = [];
props.queuedFiles = { items: [] };
props.folderId = 99;
props.onReplaceUrl = () => {};
});
it('saves files between updates', () => {
let fileProps = {
files: [
{ id: 1, name: 'file one', type: 'image/jpeg', parent: { id: 99 } },
{ id: 2, name: 'file two', type: 'image/jpeg', parent: { id: 99 } }
]
};
let newProps = { ...props, ...fileProps };
const wrapper = shallow(<AssetAdmin {...newProps} />);
expect(wrapper.instance().findFile(1).id).toEqual(1);
expect(wrapper.instance().findFile(3)).toBeFalsy();
fileProps = {
files: [
{ id: 3, name: 'file three', type: 'image/jpeg', parent: { id: 99 } },
{ id: 4, name: 'file four', type: 'image/jpeg', parent: { id: 99 } }
]
};
newProps = { ...props, ...fileProps };
wrapper.setProps(newProps);
expect(wrapper.instance().findFile(1).id).toEqual(1);
expect(wrapper.instance().findFile(3).id).toEqual(3);
});
});

describe('getFiles', () => {
const deriveFilesIDs = (_props) => {
props = { ...props, ..._props };
Expand Down

0 comments on commit 8c7dd70

Please sign in to comment.