diff options
author | Phil Hughes <me@iamphill.com> | 2018-03-28 11:19:14 +0100 |
---|---|---|
committer | Phil Hughes <me@iamphill.com> | 2018-03-28 14:08:36 +0100 |
commit | af20c4423c85c263f34a09c276a7bb917e3554a5 (patch) | |
tree | 94ec7a83723596ebe40b71ed54372ee29d1da784 | |
parent | c5a591e61d3476531b67eac4315af6c60efc8228 (diff) | |
download | gitlab-ce-af20c4423c85c263f34a09c276a7bb917e3554a5.tar.gz |
refactor ADD_PENDING_TAB to stop multiple state changes
-rw-r--r-- | app/assets/javascripts/ide/stores/actions/file.js | 2 | ||||
-rw-r--r-- | app/assets/javascripts/ide/stores/mutations/file.js | 59 | ||||
-rw-r--r-- | spec/javascripts/ide/stores/mutations/file_spec.js | 8 |
3 files changed, 26 insertions, 43 deletions
diff --git a/app/assets/javascripts/ide/stores/actions/file.js b/app/assets/javascripts/ide/stores/actions/file.js index 2847fceaf4e..7aacec89116 100644 --- a/app/assets/javascripts/ide/stores/actions/file.js +++ b/app/assets/javascripts/ide/stores/actions/file.js @@ -143,7 +143,7 @@ export const openPendingTab = ({ commit, getters, dispatch, state }, file) => { return false; } - commit(types.ADD_PENDING_TAB, file); + commit(types.ADD_PENDING_TAB, { file }); dispatch('scrollToTab'); diff --git a/app/assets/javascripts/ide/stores/mutations/file.js b/app/assets/javascripts/ide/stores/mutations/file.js index 25faac7c828..f8d0262e120 100644 --- a/app/assets/javascripts/ide/stores/mutations/file.js +++ b/app/assets/javascripts/ide/stores/mutations/file.js @@ -96,50 +96,33 @@ export default { changed, }); }, - [types.ADD_PENDING_TAB](state, file) { + [types.ADD_PENDING_TAB](state, { file, keyPrefix = 'pending' }) { const pendingTab = state.openFiles.find(f => f.path === file.path && f.pending); + let openFiles = state.openFiles.map(f => + Object.assign(f, { active: f.path === file.path, opened: false }), + ); - Object.assign(state, { - openFiles: state.openFiles.map(f => Object.assign(f, { active: false })), - }); + if (!pendingTab) { + const openFile = openFiles.find(f => f.path === file.path); - if (pendingTab) { - Object.assign(state, { - openFiles: state.openFiles.map(f => { - if (f.pending && f.path === file.path) { - return Object.assign(f, { active: true }); - } + openFiles = openFiles.concat(openFile ? null : file).reduce((acc, f) => { + if (!f) return acc; - return f; - }), - }); - } else { - const openFile = state.openFiles.find(f => f.path === file.path); - const openFiles = state.openFiles - .concat(openFile ? null : file) - .filter(f => f) - .reduce((acc, f) => { - if (f.path === file.path) { - return acc.concat({ - ...f, - active: true, - pending: true, - key: `pending-${f.key}`, - }); - } + if (f.path === file.path) { + return acc.concat({ + ...f, + active: true, + pending: true, + opened: true, + key: `${keyPrefix}-${f.key}`, + }); + } - return acc.concat(f); - }, []); - - Object.assign(state, { - entries: Object.assign(state.entries, { - [file.path]: Object.assign(state.entries[file.path], { - opened: false, - }), - }), - openFiles, - }); + return acc.concat(f); + }, []); } + + Object.assign(state, { openFiles }); }, [types.REMOVE_PENDING_TAB](state, file) { Object.assign(state, { diff --git a/spec/javascripts/ide/stores/mutations/file_spec.js b/spec/javascripts/ide/stores/mutations/file_spec.js index 9978f22f44a..3f379ff219b 100644 --- a/spec/javascripts/ide/stores/mutations/file_spec.js +++ b/spec/javascripts/ide/stores/mutations/file_spec.js @@ -183,7 +183,7 @@ describe('Multi-file store file mutations', () => { }); it('adds file into openFiles as pending', () => { - mutations.ADD_PENDING_TAB(localState, localFile); + mutations.ADD_PENDING_TAB(localState, { file: localFile }); expect(localState.openFiles.length).toBe(2); expect(localState.openFiles[1].pending).toBe(true); @@ -191,7 +191,7 @@ describe('Multi-file store file mutations', () => { }); it('updates open file to pending', () => { - mutations.ADD_PENDING_TAB(localState, localState.openFiles[0]); + mutations.ADD_PENDING_TAB(localState, { file: localState.openFiles[0] }); expect(localState.openFiles.length).toBe(1); }); @@ -202,14 +202,14 @@ describe('Multi-file store file mutations', () => { pending: true, }); - mutations.ADD_PENDING_TAB(localState, localFile); + mutations.ADD_PENDING_TAB(localState, { file: localFile }); expect(localState.openFiles[1].pending).toBe(true); expect(localState.openFiles[1].active).toBe(true); }); it('sets all openFiles to not active', () => { - mutations.ADD_PENDING_TAB(localState, localFile); + mutations.ADD_PENDING_TAB(localState, { file: localFile }); expect(localState.openFiles.length).toBe(2); |