Commit cb06d0b1 authored by Mark Florian's avatar Mark Florian

Merge branch '357830-refactoring' into 'master'

Refactor prop mutation in issue form

See merge request gitlab-org/gitlab!84445
parents 86773e31 40d7f9de
...@@ -327,9 +327,12 @@ export default { ...@@ -327,9 +327,12 @@ export default {
}); });
}, },
updateFormState(state) {
this.store.setFormState(state);
},
updateAndShowForm(templates = {}) { updateAndShowForm(templates = {}) {
if (!this.showForm) { if (!this.showForm) {
this.showForm = true;
this.store.setFormState({ this.store.setFormState({
title: this.state.titleText, title: this.state.titleText,
description: this.state.descriptionText, description: this.state.descriptionText,
...@@ -338,6 +341,7 @@ export default { ...@@ -338,6 +341,7 @@ export default {
updateLoading: false, updateLoading: false,
issuableTemplates: templates, issuableTemplates: templates,
}); });
this.showForm = true;
} }
}, },
...@@ -369,6 +373,10 @@ export default { ...@@ -369,6 +373,10 @@ export default {
}, },
updateIssuable() { updateIssuable() {
this.store.setFormState({
updateLoading: true,
});
const { const {
store: { formState }, store: { formState },
issueState, issueState,
...@@ -376,7 +384,9 @@ export default { ...@@ -376,7 +384,9 @@ export default {
const issuablePayload = issueState.isDirty const issuablePayload = issueState.isDirty
? { ...formState, issue_type: issueState.issueType } ? { ...formState, issue_type: issueState.issueType }
: formState; : formState;
this.clearFlash(); this.clearFlash();
return this.service return this.service
.updateIssuable(issuablePayload) .updateIssuable(issuablePayload)
.then((res) => res.data) .then((res) => res.data)
...@@ -473,6 +483,7 @@ export default { ...@@ -473,6 +483,7 @@ export default {
:can-attach-file="canAttachFile" :can-attach-file="canAttachFile"
:enable-autocomplete="enableAutocomplete" :enable-autocomplete="enableAutocomplete"
:issuable-type="issuableType" :issuable-type="issuableType"
@updateForm="updateFormState"
/> />
</div> </div>
<div v-else> <div v-else>
......
...@@ -288,17 +288,17 @@ export default { ...@@ -288,17 +288,17 @@ export default {
}" }"
class="md" class="md"
></div> ></div>
<!-- eslint-disable vue/no-mutating-props -->
<textarea <textarea
v-if="descriptionText" v-if="descriptionText"
v-model="descriptionText" :value="descriptionText"
:data-update-url="updateUrl" :data-update-url="updateUrl"
class="hidden js-task-list-field" class="hidden js-task-list-field"
dir="auto" dir="auto"
data-testid="textarea" data-testid="textarea"
> >
</textarea> </textarea>
<!-- eslint-enable vue/no-mutating-props -->
<gl-modal <gl-modal
ref="modal" ref="modal"
modal-id="create-task-modal" modal-id="create-task-modal"
......
...@@ -9,8 +9,8 @@ export default { ...@@ -9,8 +9,8 @@ export default {
}, },
mixins: [updateMixin], mixins: [updateMixin],
props: { props: {
formState: { value: {
type: Object, type: String,
required: true, required: true,
}, },
markdownPreviewPath: { markdownPreviewPath: {
...@@ -52,24 +52,23 @@ export default { ...@@ -52,24 +52,23 @@ export default {
:quick-actions-docs-path="quickActionsDocsPath" :quick-actions-docs-path="quickActionsDocsPath"
:can-attach-file="canAttachFile" :can-attach-file="canAttachFile"
:enable-autocomplete="enableAutocomplete" :enable-autocomplete="enableAutocomplete"
:textarea-value="formState.description" :textarea-value="value"
> >
<template #textarea> <template #textarea>
<!-- eslint-disable vue/no-mutating-props -->
<textarea <textarea
id="issue-description" id="issue-description"
ref="textarea" ref="textarea"
v-model="formState.description" :value="value"
class="note-textarea js-gfm-input js-autosize markdown-area qa-description-textarea" class="note-textarea js-gfm-input js-autosize markdown-area qa-description-textarea"
dir="auto" dir="auto"
data-supports-quick-actions="true" data-supports-quick-actions="true"
:aria-label="__('Description')" :aria-label="__('Description')"
:placeholder="__('Write a comment or drag your files here…')" :placeholder="__('Write a comment or drag your files here…')"
@input="$emit('input', $event.target.value)"
@keydown.meta.enter="updateIssuable" @keydown.meta.enter="updateIssuable"
@keydown.ctrl.enter="updateIssuable" @keydown.ctrl.enter="updateIssuable"
> >
</textarea> </textarea>
<!-- eslint-enable vue/no-mutating-props -->
</template> </template>
</markdown-field> </markdown-field>
</div> </div>
......
...@@ -8,8 +8,8 @@ export default { ...@@ -8,8 +8,8 @@ export default {
GlIcon, GlIcon,
}, },
props: { props: {
formState: { value: {
type: Object, type: String,
required: true, required: true,
}, },
issuableTemplates: { issuableTemplates: {
...@@ -39,10 +39,9 @@ export default { ...@@ -39,10 +39,9 @@ export default {
// Create the editor for the template // Create the editor for the template
const editor = document.querySelector('.detail-page-description .note-textarea') || {}; const editor = document.querySelector('.detail-page-description .note-textarea') || {};
editor.setValue = (val) => { editor.setValue = (val) => {
// eslint-disable-next-line vue/no-mutating-props this.$emit('input', val);
this.formState.description = val;
}; };
editor.getValue = () => this.formState.description; editor.getValue = () => this.value;
this.issuableTemplate = new IssuableTemplateSelectors({ this.issuableTemplate = new IssuableTemplateSelectors({
$dropdowns: $(this.$refs.toggle), $dropdowns: $(this.$refs.toggle),
......
...@@ -4,8 +4,8 @@ import updateMixin from '../../mixins/update'; ...@@ -4,8 +4,8 @@ import updateMixin from '../../mixins/update';
export default { export default {
mixins: [updateMixin], mixins: [updateMixin],
props: { props: {
formState: { value: {
type: Object, type: String,
required: true, required: true,
}, },
}, },
...@@ -15,19 +15,18 @@ export default { ...@@ -15,19 +15,18 @@ export default {
<template> <template>
<fieldset> <fieldset>
<label class="sr-only" for="issuable-title">{{ __('Title') }}</label> <label class="sr-only" for="issuable-title">{{ __('Title') }}</label>
<!-- eslint-disable vue/no-mutating-props -->
<input <input
id="issuable-title" id="issuable-title"
ref="input" ref="input"
v-model="formState.title" :value="value"
class="form-control qa-title-input gl-border-gray-200" class="form-control qa-title-input gl-border-gray-200"
dir="auto" dir="auto"
type="text" type="text"
:placeholder="__('Title')" :placeholder="__('Title')"
:aria-label="__('Title')" :aria-label="__('Title')"
@input="$emit('input', $event.target.value)"
@keydown.meta.enter="updateIssuable" @keydown.meta.enter="updateIssuable"
@keydown.ctrl.enter="updateIssuable" @keydown.ctrl.enter="updateIssuable"
/> />
<!-- eslint-enable vue/no-mutating-props -->
</fieldset> </fieldset>
</template> </template>
...@@ -86,6 +86,10 @@ export default { ...@@ -86,6 +86,10 @@ export default {
}, },
data() { data() {
return { return {
formData: {
title: this.formState.title,
description: this.formState.description,
},
showOutdatedDescriptionWarning: false, showOutdatedDescriptionWarning: false,
}; };
}, },
...@@ -100,6 +104,14 @@ export default { ...@@ -100,6 +104,14 @@ export default {
return this.issuableType === IssuableType.Issue; return this.issuableType === IssuableType.Issue;
}, },
}, },
watch: {
formData: {
handler(value) {
this.$emit('updateForm', value);
},
deep: true,
},
},
created() { created() {
eventHub.$on('delete.issuable', this.resetAutosave); eventHub.$on('delete.issuable', this.resetAutosave);
eventHub.$on('update.issuable', this.resetAutosave); eventHub.$on('update.issuable', this.resetAutosave);
...@@ -191,16 +203,17 @@ export default { ...@@ -191,16 +203,17 @@ export default {
> >
<div class="row gl-mb-3"> <div class="row gl-mb-3">
<div class="col-12"> <div class="col-12">
<issuable-title-field ref="title" :form-state="formState" /> <issuable-title-field ref="title" v-model="formData.title" />
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div v-if="isIssueType" class="col-12 col-md-4 pr-md-0"> <div v-if="isIssueType" class="col-12 col-md-4 pr-md-0">
<issuable-type-field ref="issue-type" /> <issuable-type-field ref="issue-type" />
</div> </div>
<div v-if="hasIssuableTemplates" class="col-12 col-md-4 pl-md-2"> <div v-if="hasIssuableTemplates" class="col-12 col-md-4 pl-md-2">
<description-template-field <description-template-field
:form-state="formState" v-model="formData.description"
:issuable-templates="issuableTemplates" :issuable-templates="issuableTemplates"
:project-path="projectPath" :project-path="projectPath"
:project-id="projectId" :project-id="projectId"
...@@ -208,14 +221,16 @@ export default { ...@@ -208,14 +221,16 @@ export default {
/> />
</div> </div>
</div> </div>
<description-field <description-field
ref="description" ref="description"
:form-state="formState" v-model="formData.description"
:markdown-preview-path="markdownPreviewPath" :markdown-preview-path="markdownPreviewPath"
:markdown-docs-path="markdownDocsPath" :markdown-docs-path="markdownDocsPath"
:can-attach-file="canAttachFile" :can-attach-file="canAttachFile"
:enable-autocomplete="enableAutocomplete" :enable-autocomplete="enableAutocomplete"
/> />
<edit-actions <edit-actions
:endpoint="endpoint" :endpoint="endpoint"
:form-state="formState" :form-state="formState"
......
...@@ -3,7 +3,6 @@ import eventHub from '../event_hub'; ...@@ -3,7 +3,6 @@ import eventHub from '../event_hub';
export default { export default {
methods: { methods: {
updateIssuable() { updateIssuable() {
this.formState.updateLoading = true;
eventHub.$emit('update.issuable'); eventHub.$emit('update.issuable');
}, },
}, },
......
...@@ -15,9 +15,7 @@ describe('Description field component', () => { ...@@ -15,9 +15,7 @@ describe('Description field component', () => {
markdownPreviewPath: '/', markdownPreviewPath: '/',
markdownDocsPath: '/', markdownDocsPath: '/',
quickActionsDocsPath: '/', quickActionsDocsPath: '/',
formState: { value: description,
description,
},
}, },
stubs: { stubs: {
MarkdownField, MarkdownField,
......
import Vue from 'vue'; import { shallowMount } from '@vue/test-utils';
import descriptionTemplate from '~/issues/show/components/fields/description_template.vue'; import descriptionTemplate from '~/issues/show/components/fields/description_template.vue';
describe('Issue description template component with templates as hash', () => { describe('Issue description template component with templates as hash', () => {
let vm; let wrapper;
let formState; const defaultOptions = {
propsData: {
value: 'test',
issuableTemplates: {
test: [{ name: 'test', id: 'test', project_path: '/', namespace_path: '/' }],
},
projectId: 1,
projectPath: '/',
namespacePath: '/',
projectNamespace: '/',
},
};
beforeEach(() => { const findIssuableSelector = () => wrapper.find('.js-issuable-selector');
const Component = Vue.extend(descriptionTemplate);
formState = {
description: 'test',
};
vm = new Component({ const createComponent = (options = defaultOptions) => {
propsData: { wrapper = shallowMount(descriptionTemplate, options);
formState, };
issuableTemplates: {
test: [{ name: 'test', id: 'test', project_path: '/', namespace_path: '/' }], afterEach(() => {
}, wrapper.destroy();
projectId: 1,
projectPath: '/',
namespacePath: '/',
projectNamespace: '/',
},
}).$mount();
}); });
it('renders templates as JSON hash in data attribute', () => { it('renders templates as JSON hash in data attribute', () => {
expect(vm.$el.querySelector('.js-issuable-selector').getAttribute('data-data')).toBe( createComponent();
expect(findIssuableSelector().attributes('data-data')).toBe(
'{"test":[{"name":"test","id":"test","project_path":"/","namespace_path":"/"}]}', '{"test":[{"name":"test","id":"test","project_path":"/","namespace_path":"/"}]}',
); );
}); });
it('updates formState when changing template', () => { it('emits input event', () => {
vm.issuableTemplate.editor.setValue('test new template'); createComponent();
wrapper.vm.issuableTemplate.editor.setValue('test new template');
expect(formState.description).toBe('test new template'); expect(wrapper.emitted('input')).toEqual([['test new template']]);
}); });
it('returns formState description with editor getValue', () => { it('returns value with editor getValue', () => {
formState.description = 'testing new template'; createComponent();
expect(wrapper.vm.issuableTemplate.editor.getValue()).toBe('test');
expect(vm.issuableTemplate.editor.getValue()).toBe('testing new template');
}); });
});
describe('Issue description template component with templates as array', () => {
let vm;
let formState;
beforeEach(() => { describe('Issue description template component with templates as array', () => {
const Component = Vue.extend(descriptionTemplate); it('renders templates as JSON array in data attribute', () => {
formState = { createComponent({
description: 'test', propsData: {
}; value: 'test',
issuableTemplates: [{ name: 'test', id: 'test', project_path: '/', namespace_path: '/' }],
vm = new Component({ projectId: 1,
propsData: { projectPath: '/',
formState, namespacePath: '/',
issuableTemplates: [{ name: 'test', id: 'test', project_path: '/', namespace_path: '/' }], projectNamespace: '/',
projectId: 1, },
projectPath: '/', });
namespacePath: '/', expect(findIssuableSelector().attributes('data-data')).toBe(
projectNamespace: '/', '[{"name":"test","id":"test","project_path":"/","namespace_path":"/"}]',
}, );
}).$mount(); });
});
it('renders templates as JSON array in data attribute', () => {
expect(vm.$el.querySelector('.js-issuable-selector').getAttribute('data-data')).toBe(
'[{"name":"test","id":"test","project_path":"/","namespace_path":"/"}]',
);
}); });
}); });
...@@ -12,9 +12,7 @@ describe('Title field component', () => { ...@@ -12,9 +12,7 @@ describe('Title field component', () => {
wrapper = shallowMount(TitleField, { wrapper = shallowMount(TitleField, {
propsData: { propsData: {
formState: { value: 'test',
title: 'test',
},
}, },
}); });
}); });
......
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