Commit c883b526 authored by Phil Hughes's avatar Phil Hughes

renames file when template type is changed

parent c0923cbd
...@@ -47,7 +47,7 @@ export default { ...@@ -47,7 +47,7 @@ export default {
<template> <template>
<div class="d-flex align-items-center ide-file-templates"> <div class="d-flex align-items-center ide-file-templates">
<strong class="mr-2"> <strong class="append-right-default">
{{ __('File templates') }} {{ __('File templates') }}
</strong> </strong>
<dropdown <dropdown
...@@ -58,7 +58,7 @@ export default { ...@@ -58,7 +58,7 @@ export default {
/> />
<dropdown <dropdown
v-if="showTemplatesDropdown" v-if="showTemplatesDropdown"
:label="__('Choose a type...')" :label="__('Choose a template...')"
:async="true" :async="true"
:searchable="true" :searchable="true"
:title="__('File templates')" :title="__('File templates')"
...@@ -66,18 +66,14 @@ export default { ...@@ -66,18 +66,14 @@ export default {
@click="selecteTemplate" @click="selecteTemplate"
/> />
<transition name="fade"> <transition name="fade">
<div v-show="updateSuccess"> <button
<strong class="text-success mr-2"> v-show="updateSuccess"
{{ __('Template applied') }} type="button"
</strong> class="btn btn-default"
<button @click="undoFileTemplate"
type="button" >
class="btn btn-default" {{ __('Undo') }}
@click="undoFileTemplate" </button>
>
{{ __('Undo') }}
</button>
</div>
</transition> </transition>
</div> </div>
</template> </template>
......
...@@ -91,6 +91,7 @@ export default { ...@@ -91,6 +91,7 @@ export default {
:header-title-text="modalTitle" :header-title-text="modalTitle"
:footer-primary-button-text="buttonLabel" :footer-primary-button-text="buttonLabel"
footer-primary-button-variant="success" footer-primary-button-variant="success"
modal-size="lg"
@submit="submitForm" @submit="submitForm"
@open="focusInput" @open="focusInput"
@closed="closedModal" @closed="closedModal"
......
...@@ -32,9 +32,23 @@ export const fetchTemplateTypes = ({ dispatch, state }) => { ...@@ -32,9 +32,23 @@ export const fetchTemplateTypes = ({ dispatch, state }) => {
.catch(() => dispatch('receiveTemplateTypesError')); .catch(() => dispatch('receiveTemplateTypesError'));
}; };
export const setSelectedTemplateType = ({ commit }, type) => export const setSelectedTemplateType = ({ commit, dispatch, rootGetters }, type) => {
commit(types.SET_SELECTED_TEMPLATE_TYPE, type); commit(types.SET_SELECTED_TEMPLATE_TYPE, type);
if (rootGetters.activeFile.prevPath === type.name) {
dispatch('discardFileChanges', rootGetters.activeFile.path, { root: true });
} else if (rootGetters.activeFile.name !== type.name) {
dispatch(
'renameEntry',
{
path: rootGetters.activeFile.path,
name: type.name,
},
{ root: true },
);
}
};
export const receiveTemplateError = ({ dispatch }, template) => { export const receiveTemplateError = ({ dispatch }, template) => {
dispatch( dispatch(
'setErrorMessage', 'setErrorMessage',
...@@ -80,6 +94,10 @@ export const undoFileTemplate = ({ dispatch, commit, rootGetters }) => { ...@@ -80,6 +94,10 @@ export const undoFileTemplate = ({ dispatch, commit, rootGetters }) => {
commit(types.SET_UPDATE_SUCCESS, false); commit(types.SET_UPDATE_SUCCESS, false);
eventHub.$emit(`editor.update.model.new.content.${file.key}`, file.raw); eventHub.$emit(`editor.update.model.new.content.${file.key}`, file.raw);
if (file.prevPath) {
dispatch('discardFileChanges', file.path, { root: true });
}
}; };
// prevent babel-plugin-rewire from generating an invalid default during karma tests // prevent babel-plugin-rewire from generating an invalid default during karma tests
......
import { activityBarViews } from '../../../constants';
export const templateTypes = () => [ export const templateTypes = () => [
{ {
name: '.gitlab-ci.yml', name: '.gitlab-ci.yml',
...@@ -17,7 +19,8 @@ export const templateTypes = () => [ ...@@ -17,7 +19,8 @@ export const templateTypes = () => [
}, },
]; ];
export const showFileTemplatesBar = (_, getters) => name => export const showFileTemplatesBar = (_, getters, rootState) => name =>
getters.templateTypes.find(t => t.name === name); getters.templateTypes.find(t => t.name === name) &&
rootState.currentActivityView === activityBarViews.edit;
export default () => {}; export default () => {};
...@@ -245,6 +245,10 @@ export default { ...@@ -245,6 +245,10 @@ export default {
if (newEntry.type === 'blob') { if (newEntry.type === 'blob') {
state.changedFiles = state.changedFiles.concat(newEntry); state.changedFiles = state.changedFiles.concat(newEntry);
} }
if (state.entries[newPath].opened) {
state.openFiles.push(state.entries[newPath]);
}
}, },
...projectMutations, ...projectMutations,
...mergeRequestMutation, ...mergeRequestMutation,
......
...@@ -148,14 +148,66 @@ describe('IDE file templates actions', () => { ...@@ -148,14 +148,66 @@ describe('IDE file templates actions', () => {
}); });
describe('setSelectedTemplateType', () => { describe('setSelectedTemplateType', () => {
it('commits SET_SELECTED_TEMPLATE_TYPE', done => { it('commits SET_SELECTED_TEMPLATE_TYPE', () => {
testAction( const commit = jasmine.createSpy('commit');
actions.setSelectedTemplateType, const options = {
'test', commit,
state, dispatch() {},
[{ type: types.SET_SELECTED_TEMPLATE_TYPE, payload: 'test' }], rootGetters: {
[], activeFile: {
done, name: 'test',
prevPath: '',
},
},
};
actions.setSelectedTemplateType(options, { name: 'test' });
expect(commit).toHaveBeenCalledWith(types.SET_SELECTED_TEMPLATE_TYPE, { name: 'test' });
});
it('dispatches discardFileChanges if prevPath matches templates name', () => {
const dispatch = jasmine.createSpy('dispatch');
const options = {
commit() {},
dispatch,
rootGetters: {
activeFile: {
name: 'test',
path: 'test',
prevPath: 'test',
},
},
};
actions.setSelectedTemplateType(options, { name: 'test' });
expect(dispatch).toHaveBeenCalledWith('discardFileChanges', 'test', { root: true });
});
it('dispatches renameEntry if file name doesnt match', () => {
const dispatch = jasmine.createSpy('dispatch');
const options = {
commit() {},
dispatch,
rootGetters: {
activeFile: {
name: 'oldtest',
path: 'oldtest',
prevPath: '',
},
},
};
actions.setSelectedTemplateType(options, { name: 'test' });
expect(dispatch).toHaveBeenCalledWith(
'renameEntry',
{
path: 'oldtest',
name: 'test',
},
{ root: true },
); );
}); });
}); });
...@@ -332,5 +384,20 @@ describe('IDE file templates actions', () => { ...@@ -332,5 +384,20 @@ describe('IDE file templates actions', () => {
expect(commit).toHaveBeenCalledWith('SET_UPDATE_SUCCESS', false); expect(commit).toHaveBeenCalledWith('SET_UPDATE_SUCCESS', false);
}); });
it('dispatches discardFileChanges if file has prevPath', () => {
const dispatch = jasmine.createSpy('dispatch');
const rootGetters = {
activeFile: { path: 'test', prevPath: 'newtest', raw: 'raw content' },
};
actions.undoFileTemplate({ dispatch, commit() {}, rootGetters });
expect(dispatch.calls.mostRecent().args).toEqual([
'discardFileChanges',
'test',
{ root: true },
]);
});
}); });
}); });
import createState from '~/ide/stores/state';
import { activityBarViews } from '~/ide/constants';
import * as getters from '~/ide/stores/modules/file_templates/getters'; import * as getters from '~/ide/stores/modules/file_templates/getters';
describe('IDE file templates getters', () => { describe('IDE file templates getters', () => {
...@@ -8,22 +10,49 @@ describe('IDE file templates getters', () => { ...@@ -8,22 +10,49 @@ describe('IDE file templates getters', () => {
}); });
describe('showFileTemplatesBar', () => { describe('showFileTemplatesBar', () => {
it('finds template type by name', () => { let rootState;
beforeEach(() => {
rootState = createState();
});
it('returns true if template is found and currentActivityView is edit', () => {
rootState.currentActivityView = activityBarViews.edit;
expect(
getters.showFileTemplatesBar(
null,
{
templateTypes: getters.templateTypes(),
},
rootState,
)('LICENSE'),
).toBe(true);
});
it('returns false if template is found and currentActivityView is not edit', () => {
rootState.currentActivityView = activityBarViews.commit;
expect( expect(
getters.showFileTemplatesBar(null, { getters.showFileTemplatesBar(
templateTypes: getters.templateTypes(), null,
})('LICENSE'), {
).toEqual({ templateTypes: getters.templateTypes(),
name: 'LICENSE', },
key: 'licenses', rootState,
}); )('LICENSE'),
).toBe(false);
}); });
it('returns undefined if not found', () => { it('returns undefined if not found', () => {
expect( expect(
getters.showFileTemplatesBar(null, { getters.showFileTemplatesBar(
templateTypes: getters.templateTypes(), null,
})('test'), {
templateTypes: getters.templateTypes(),
},
rootState,
)('test'),
).toBe(undefined); ).toBe(undefined);
}); });
}); });
......
...@@ -339,5 +339,13 @@ describe('Multi-file store mutations', () => { ...@@ -339,5 +339,13 @@ describe('Multi-file store mutations', () => {
expect(localState.entries.parentPath.tree.length).toBe(1); expect(localState.entries.parentPath.tree.length).toBe(1);
}); });
it('adds to openFiles if previously opened', () => {
localState.entries.oldPath.opened = true;
mutations.RENAME_ENTRY(localState, { path: 'oldPath', name: 'newPath' });
expect(localState.openFiles).toEqual([localState.entries.newPath]);
});
}); });
}); });
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