2020-04-22 19:07:51 +05:30
|
|
|
<script>
|
|
|
|
import { GlButton, GlLoadingIcon } from '@gitlab/ui';
|
|
|
|
|
|
|
|
import Flash from '~/flash';
|
|
|
|
import { __, sprintf } from '~/locale';
|
|
|
|
import TitleField from '~/vue_shared/components/form/title.vue';
|
2020-07-28 23:09:34 +05:30
|
|
|
import { redirectTo } from '~/lib/utils/url_utility';
|
2020-04-22 19:07:51 +05:30
|
|
|
import FormFooterActions from '~/vue_shared/components/form/form_footer_actions.vue';
|
|
|
|
|
|
|
|
import UpdateSnippetMutation from '../mutations/updateSnippet.mutation.graphql';
|
|
|
|
import CreateSnippetMutation from '../mutations/createSnippet.mutation.graphql';
|
|
|
|
import { getSnippetMixin } from '../mixins/snippets';
|
2020-06-23 00:09:42 +05:30
|
|
|
import {
|
|
|
|
SNIPPET_VISIBILITY_PRIVATE,
|
|
|
|
SNIPPET_CREATE_MUTATION_ERROR,
|
|
|
|
SNIPPET_UPDATE_MUTATION_ERROR,
|
2020-07-28 23:09:34 +05:30
|
|
|
SNIPPET_BLOB_ACTION_CREATE,
|
|
|
|
SNIPPET_BLOB_ACTION_UPDATE,
|
|
|
|
SNIPPET_BLOB_ACTION_MOVE,
|
2020-06-23 00:09:42 +05:30
|
|
|
} from '../constants';
|
2020-04-22 19:07:51 +05:30
|
|
|
import SnippetBlobEdit from './snippet_blob_edit.vue';
|
|
|
|
import SnippetVisibilityEdit from './snippet_visibility_edit.vue';
|
|
|
|
import SnippetDescriptionEdit from './snippet_description_edit.vue';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
components: {
|
|
|
|
SnippetDescriptionEdit,
|
|
|
|
SnippetVisibilityEdit,
|
|
|
|
SnippetBlobEdit,
|
|
|
|
TitleField,
|
|
|
|
FormFooterActions,
|
|
|
|
GlButton,
|
|
|
|
GlLoadingIcon,
|
|
|
|
},
|
|
|
|
mixins: [getSnippetMixin],
|
|
|
|
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 {
|
2020-07-28 23:09:34 +05:30
|
|
|
blobsActions: {},
|
2020-04-22 19:07:51 +05:30
|
|
|
isUpdating: false,
|
|
|
|
newSnippet: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
2020-07-28 23:09:34 +05:30
|
|
|
getActionsEntries() {
|
|
|
|
return Object.values(this.blobsActions);
|
|
|
|
},
|
|
|
|
allBlobsHaveContent() {
|
|
|
|
const entries = this.getActionsEntries;
|
|
|
|
return entries.length > 0 && !entries.find(action => !action.content);
|
|
|
|
},
|
|
|
|
allBlobChangesRegistered() {
|
|
|
|
const entries = this.getActionsEntries;
|
|
|
|
return entries.length > 0 && !entries.find(action => action.action === '');
|
|
|
|
},
|
2020-04-22 19:07:51 +05:30
|
|
|
updatePrevented() {
|
2020-07-28 23:09:34 +05:30
|
|
|
return this.snippet.title === '' || !this.allBlobsHaveContent || 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-07-28 23:09:34 +05:30
|
|
|
files: this.getActionsEntries.filter(entry => entry.action !== ''),
|
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) {
|
|
|
|
return this.projectPath ? `/${this.projectPath}/snippets` : `/snippets`;
|
|
|
|
}
|
|
|
|
return this.snippet.webUrl;
|
2020-04-22 19:07:51 +05:30
|
|
|
},
|
|
|
|
titleFieldId() {
|
|
|
|
return `${this.isProjectSnippet ? 'project' : 'personal'}_snippet_title`;
|
|
|
|
},
|
|
|
|
descriptionFieldId() {
|
|
|
|
return `${this.isProjectSnippet ? 'project' : 'personal'}_snippet_description`;
|
|
|
|
},
|
|
|
|
},
|
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?');
|
|
|
|
|
|
|
|
if (!this.allBlobChangesRegistered) return undefined;
|
|
|
|
|
|
|
|
Object.assign(e, { returnValue });
|
|
|
|
return returnValue;
|
|
|
|
},
|
|
|
|
updateBlobActions(args = {}) {
|
|
|
|
// `_constants` is the internal prop that
|
|
|
|
// should not be sent to the mutation. Hence we filter it out from
|
|
|
|
// the argsToUpdateAction that is the data-basis for the mutation.
|
|
|
|
const { _constants: blobConstants, ...argsToUpdateAction } = args;
|
|
|
|
const { previousPath, filePath, content } = argsToUpdateAction;
|
|
|
|
let actionEntry = this.blobsActions[blobConstants.id] || {};
|
|
|
|
let tunedActions = {
|
|
|
|
action: '',
|
|
|
|
previousPath,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.newSnippet) {
|
|
|
|
// new snippet, hence new blob
|
|
|
|
tunedActions = {
|
|
|
|
action: SNIPPET_BLOB_ACTION_CREATE,
|
|
|
|
previousPath: '',
|
|
|
|
};
|
|
|
|
} else if (previousPath && filePath) {
|
|
|
|
// renaming of a blob + renaming & content update
|
|
|
|
const renamedToOriginal = filePath === blobConstants.originalPath;
|
|
|
|
tunedActions = {
|
|
|
|
action: renamedToOriginal ? SNIPPET_BLOB_ACTION_UPDATE : SNIPPET_BLOB_ACTION_MOVE,
|
|
|
|
previousPath: !renamedToOriginal ? blobConstants.originalPath : '',
|
|
|
|
};
|
|
|
|
} else if (content !== blobConstants.originalContent) {
|
|
|
|
// content update only
|
|
|
|
tunedActions = {
|
|
|
|
action: SNIPPET_BLOB_ACTION_UPDATE,
|
|
|
|
previousPath: '',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
actionEntry = { ...actionEntry, ...argsToUpdateAction, ...tunedActions };
|
|
|
|
|
|
|
|
this.$set(this.blobsActions, blobConstants.id, actionEntry);
|
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
|
|
|
},
|
|
|
|
onNewSnippetFetched() {
|
|
|
|
this.newSnippet = true;
|
|
|
|
this.snippet = this.$options.newSnippetSchema;
|
|
|
|
},
|
|
|
|
onExistingSnippetFetched() {
|
|
|
|
this.newSnippet = false;
|
|
|
|
},
|
|
|
|
onSnippetFetch(snippetRes) {
|
|
|
|
if (snippetRes.data.snippets.edges.length === 0) {
|
|
|
|
this.onNewSnippetFetched();
|
|
|
|
} else {
|
|
|
|
this.onExistingSnippetFetched();
|
|
|
|
}
|
|
|
|
},
|
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 {
|
2020-07-28 23:09:34 +05:30
|
|
|
this.originalContent = this.content;
|
2020-06-23 00:09:42 +05:30
|
|
|
redirectTo(baseObj.snippet.webUrl);
|
2020-04-22 19:07:51 +05:30
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
this.flashAPIFailure(e);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
newSnippetSchema: {
|
|
|
|
title: '',
|
|
|
|
description: '',
|
|
|
|
visibilityLevel: SNIPPET_VISIBILITY_PRIVATE,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<form
|
|
|
|
class="snippet-form js-requires-input js-quick-submit common-note-form"
|
|
|
|
: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"
|
|
|
|
class="loading-animation prepend-top-20 append-bottom-20"
|
|
|
|
/>
|
|
|
|
<template v-else>
|
2020-05-24 23:13:21 +05:30
|
|
|
<title-field
|
|
|
|
:id="titleFieldId"
|
|
|
|
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
|
|
|
|
:id="descriptionFieldId"
|
|
|
|
v-model="snippet.description"
|
|
|
|
:markdown-preview-path="markdownPreviewPath"
|
|
|
|
:markdown-docs-path="markdownDocsPath"
|
|
|
|
/>
|
2020-07-28 23:09:34 +05:30
|
|
|
<template v-if="blobs.length">
|
|
|
|
<snippet-blob-edit
|
|
|
|
v-for="blob in blobs"
|
|
|
|
:key="blob.name"
|
|
|
|
:blob="blob"
|
|
|
|
@blob-updated="updateBlobActions"
|
|
|
|
/>
|
|
|
|
</template>
|
|
|
|
<snippet-blob-edit v-else @blob-updated="updateBlobActions" />
|
|
|
|
|
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>
|