debian-mirror-gitlab/app/assets/javascripts/snippets/components/edit.vue

233 lines
6.5 KiB
Vue
Raw Normal View History

2020-04-22 19:07:51 +05:30
<script>
import { GlButton, GlLoadingIcon } from '@gitlab/ui';
2020-10-24 23:57:45 +05:30
import { deprecatedCreateFlash as Flash } from '~/flash';
2020-04-22 19:07:51 +05:30
import { __, sprintf } from '~/locale';
import TitleField from '~/vue_shared/components/form/title.vue';
2020-11-24 15:15:51 +05:30
import { redirectTo, joinPaths } from '~/lib/utils/url_utility';
2020-04-22 19:07:51 +05:30
import FormFooterActions from '~/vue_shared/components/form/form_footer_actions.vue';
2021-01-03 14:25:43 +05:30
import {
SNIPPET_MARK_EDIT_APP_START,
SNIPPET_MEASURE_BLOBS_CONTENT,
2021-01-29 00:20:46 +05:30
} from '~/performance/constants';
2021-01-03 14:25:43 +05:30
import eventHub from '~/blob/components/eventhub';
2021-01-29 00:20:46 +05:30
import { performanceMarkAndMeasure } from '~/performance/utils';
2020-04-22 19:07:51 +05:30
import UpdateSnippetMutation from '../mutations/updateSnippet.mutation.graphql';
import CreateSnippetMutation from '../mutations/createSnippet.mutation.graphql';
import { getSnippetMixin } from '../mixins/snippets';
2021-01-29 00:20:46 +05:30
import { SNIPPET_CREATE_MUTATION_ERROR, SNIPPET_UPDATE_MUTATION_ERROR } from '../constants';
2021-01-03 14:25:43 +05:30
import { markBlobPerformance } from '../utils/blob';
2020-11-24 15:15:51 +05:30
2020-10-24 23:57:45 +05:30
import SnippetBlobActionsEdit from './snippet_blob_actions_edit.vue';
2020-04-22 19:07:51 +05:30
import SnippetVisibilityEdit from './snippet_visibility_edit.vue';
import SnippetDescriptionEdit from './snippet_description_edit.vue';
2021-01-03 14:25:43 +05:30
eventHub.$on(SNIPPET_MEASURE_BLOBS_CONTENT, markBlobPerformance);
2020-04-22 19:07:51 +05:30
export default {
components: {
SnippetDescriptionEdit,
SnippetVisibilityEdit,
2020-10-24 23:57:45 +05:30
SnippetBlobActionsEdit,
2020-04-22 19:07:51 +05:30
TitleField,
FormFooterActions,
GlButton,
GlLoadingIcon,
},
mixins: [getSnippetMixin],
2021-01-29 00:20:46 +05:30
inject: ['selectedLevel'],
2020-04-22 19:07:51 +05:30
props: {
markdownPreviewPath: {
type: String,
required: true,
},
markdownDocsPath: {
type: String,
required: true,
},
visibilityHelpLink: {
type: String,
default: '',
required: false,
},
projectPath: {
type: String,
default: '',
required: false,
},
},
data() {
return {
isUpdating: false,
2020-10-24 23:57:45 +05:30
actions: [],
2021-01-29 00:20:46 +05:30
snippet: {
title: '',
description: '',
visibilityLevel: this.selectedLevel,
},
2020-04-22 19:07:51 +05:30
};
},
computed: {
2020-10-24 23:57:45 +05:30
hasBlobChanges() {
return this.actions.length > 0;
2020-07-28 23:09:34 +05:30
},
2020-10-24 23:57:45 +05:30
hasValidBlobs() {
return this.actions.every(x => x.content);
2020-07-28 23:09:34 +05:30
},
2020-04-22 19:07:51 +05:30
updatePrevented() {
2020-10-24 23:57:45 +05:30
return this.snippet.title === '' || !this.hasValidBlobs || this.isUpdating;
2020-04-22 19:07:51 +05:30
},
isProjectSnippet() {
return Boolean(this.projectPath);
},
apiData() {
return {
id: this.snippet.id,
title: this.snippet.title,
description: this.snippet.description,
visibilityLevel: this.snippet.visibilityLevel,
2020-10-24 23:57:45 +05:30
blobActions: this.actions,
2020-04-22 19:07:51 +05:30
};
},
saveButtonLabel() {
if (this.newSnippet) {
return __('Create snippet');
}
return this.isUpdating ? __('Saving') : __('Save changes');
},
cancelButtonHref() {
2020-05-24 23:13:21 +05:30
if (this.newSnippet) {
2020-11-24 15:15:51 +05:30
return joinPaths('/', gon.relative_url_root, this.projectPath, '-/snippets');
2020-05-24 23:13:21 +05:30
}
return this.snippet.webUrl;
2020-04-22 19:07:51 +05:30
},
},
2020-10-24 23:57:45 +05:30
beforeCreate() {
2021-01-03 14:25:43 +05:30
performanceMarkAndMeasure({ mark: SNIPPET_MARK_EDIT_APP_START });
2020-10-24 23:57:45 +05:30
},
2020-07-28 23:09:34 +05:30
created() {
window.addEventListener('beforeunload', this.onBeforeUnload);
},
destroyed() {
window.removeEventListener('beforeunload', this.onBeforeUnload);
},
2020-04-22 19:07:51 +05:30
methods: {
2020-07-28 23:09:34 +05:30
onBeforeUnload(e = {}) {
const returnValue = __('Are you sure you want to lose unsaved changes?');
2020-10-24 23:57:45 +05:30
if (!this.hasBlobChanges || this.isUpdating) return undefined;
2020-07-28 23:09:34 +05:30
Object.assign(e, { returnValue });
return returnValue;
},
2020-04-22 19:07:51 +05:30
flashAPIFailure(err) {
2020-06-23 00:09:42 +05:30
const defaultErrorMsg = this.newSnippet
? SNIPPET_CREATE_MUTATION_ERROR
: SNIPPET_UPDATE_MUTATION_ERROR;
Flash(sprintf(defaultErrorMsg, { err }));
this.isUpdating = false;
2020-04-22 19:07:51 +05:30
},
2020-06-23 00:09:42 +05:30
getAttachedFiles() {
const fileInputs = Array.from(this.$el.querySelectorAll('[name="files[]"]'));
return fileInputs.map(node => node.value);
},
createMutation() {
return {
mutation: CreateSnippetMutation,
variables: {
input: {
...this.apiData,
uploadedFiles: this.getAttachedFiles(),
projectPath: this.projectPath,
},
},
};
},
updateMutation() {
return {
mutation: UpdateSnippetMutation,
variables: {
input: this.apiData,
},
};
},
2020-04-22 19:07:51 +05:30
handleFormSubmit() {
this.isUpdating = true;
this.$apollo
2020-06-23 00:09:42 +05:30
.mutate(this.newSnippet ? this.createMutation() : this.updateMutation())
2020-04-22 19:07:51 +05:30
.then(({ data }) => {
const baseObj = this.newSnippet ? data?.createSnippet : data?.updateSnippet;
const errors = baseObj?.errors;
if (errors.length) {
this.flashAPIFailure(errors[0]);
2020-06-23 00:09:42 +05:30
} else {
redirectTo(baseObj.snippet.webUrl);
2020-04-22 19:07:51 +05:30
}
})
.catch(e => {
this.flashAPIFailure(e);
});
},
2020-10-24 23:57:45 +05:30
updateActions(actions) {
this.actions = actions;
},
2020-04-22 19:07:51 +05:30
},
};
</script>
<template>
<form
2021-01-29 00:20:46 +05:30
class="snippet-form js-quick-submit common-note-form"
2020-04-22 19:07:51 +05:30
:data-snippet-type="isProjectSnippet ? 'project' : 'personal'"
2020-06-23 00:09:42 +05:30
data-testid="snippet-edit-form"
@submit.prevent="handleFormSubmit"
2020-04-22 19:07:51 +05:30
>
<gl-loading-icon
v-if="isLoading"
:label="__('Loading snippet')"
size="lg"
2020-11-24 15:15:51 +05:30
class="loading-animation prepend-top-20 gl-mb-6"
2020-04-22 19:07:51 +05:30
/>
<template v-else>
2020-05-24 23:13:21 +05:30
<title-field
2021-01-03 14:25:43 +05:30
id="snippet-title"
2020-05-24 23:13:21 +05:30
v-model="snippet.title"
2020-06-23 00:09:42 +05:30
data-qa-selector="snippet_title_field"
2020-05-24 23:13:21 +05:30
required
:autofocus="true"
/>
2020-04-22 19:07:51 +05:30
<snippet-description-edit
v-model="snippet.description"
:markdown-preview-path="markdownPreviewPath"
:markdown-docs-path="markdownDocsPath"
/>
2020-10-24 23:57:45 +05:30
<snippet-blob-actions-edit :init-blobs="blobs" @actions="updateActions" />
2020-07-28 23:09:34 +05:30
2020-04-22 19:07:51 +05:30
<snippet-visibility-edit
v-model="snippet.visibilityLevel"
:help-link="visibilityHelpLink"
:is-project-snippet="isProjectSnippet"
/>
<form-footer-actions>
<template #prepend>
<gl-button
category="primary"
2020-06-23 00:09:42 +05:30
type="submit"
2020-04-22 19:07:51 +05:30
variant="success"
:disabled="updatePrevented"
2020-05-24 23:13:21 +05:30
data-qa-selector="submit_button"
2020-06-23 00:09:42 +05:30
data-testid="snippet-submit-btn"
2020-04-22 19:07:51 +05:30
>{{ saveButtonLabel }}</gl-button
>
</template>
<template #append>
2020-06-23 00:09:42 +05:30
<gl-button type="cancel" data-testid="snippet-cancel-btn" :href="cancelButtonHref">{{
2020-05-24 23:13:21 +05:30
__('Cancel')
}}</gl-button>
2020-04-22 19:07:51 +05:30
</template>
</form-footer-actions>
</template>
</form>
</template>