Commit 3d899a7d authored by Jacob Schatz's avatar Jacob Schatz

Merge branch 'acet-repo-editor-prevent-extra-request' into 'master'

RepoEditor: Prevent extra network call for already opened files.

Closes #38256

See merge request gitlab-org/gitlab-ce!14489
parents 313fbd5f c25e83b5
...@@ -37,9 +37,15 @@ export default { ...@@ -37,9 +37,15 @@ export default {
let file = clickedFile; let file = clickedFile;
if (file.loading) return; if (file.loading) return;
file.loading = true; file.loading = true;
if (file.type === 'tree' && file.opened) { if (file.type === 'tree' && file.opened) {
file = Store.removeChildFilesOfTree(file); file = Store.removeChildFilesOfTree(file);
file.loading = false; file.loading = false;
} else {
const openFile = Helper.getFileFromPath(file.url);
if (openFile) {
file.loading = false;
Store.setActiveFiles(openFile);
} else { } else {
Service.url = file.url; Service.url = file.url;
Helper.getContent(file) Helper.getContent(file)
...@@ -49,6 +55,7 @@ export default { ...@@ -49,6 +55,7 @@ export default {
}) })
.catch(Helper.loadingError); .catch(Helper.loadingError);
} }
}
}, },
goToPreviousDirectoryClicked(prevURL) { goToPreviousDirectoryClicked(prevURL) {
......
...@@ -263,6 +263,10 @@ const RepoHelper = { ...@@ -263,6 +263,10 @@ const RepoHelper = {
return Store.openedFiles.find(openedFile => Store.activeFile.url === openedFile.url); return Store.openedFiles.find(openedFile => Store.activeFile.url === openedFile.url);
}, },
getFileFromPath(path) {
return Store.openedFiles.find(file => file.url === path);
},
loadingError() { loadingError() {
Flash('Unable to load this content at this time.'); Flash('Unable to load this content at this time.');
}, },
......
...@@ -79,6 +79,20 @@ describe('RepoSidebar', () => { ...@@ -79,6 +79,20 @@ describe('RepoSidebar', () => {
expect(Helper.getContent).toHaveBeenCalledWith(file1); expect(Helper.getContent).toHaveBeenCalledWith(file1);
}); });
it('should not fetch data for already opened files', () => {
const file = {
id: 42,
url: 'foo',
};
spyOn(Helper, 'getFileFromPath').and.returnValue(file);
spyOn(RepoStore, 'setActiveFiles');
const vm = createComponent();
vm.fileClicked(file);
expect(RepoStore.setActiveFiles).toHaveBeenCalledWith(file);
});
it('should hide files in directory if already open', () => { it('should hide files in directory if already open', () => {
spyOn(RepoStore, 'removeChildFilesOfTree').and.callThrough(); spyOn(RepoStore, 'removeChildFilesOfTree').and.callThrough();
const file1 = { const file1 = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment