Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Save fetched files so details button works after pagination #1221

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Array.reduce would be better here

Not that it matters.

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
41 changes: 41 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,43 @@ 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} />);
const wrapper2 = shallow(<AssetAdmin {...newProps} />);
expect(wrapper.instance().findFile(1).id).toEqual(1);
expect(wrapper.instance().findFile(3)).toBeFalsy();
expect(wrapper2.instance().findFile(1).id).toEqual(1);
expect(wrapper2.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);
// (don't setProps on wrapper2)
expect(wrapper.instance().findFile(1).id).toEqual(1);
expect(wrapper.instance().findFile(3).id).toEqual(3);
expect(wrapper2.instance().findFile(1).id).toEqual(1);
expect(wrapper2.instance().findFile(3)).toBeFalsy();
});
});

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