From 3257efbdbc0c1f9cb0fdc36fdc4bb5dd71f547c7 Mon Sep 17 00:00:00 2001 From: Samantha Ming Date: Wed, 21 Jul 2021 17:11:05 -0700 Subject: [PATCH] Add validation to delete blob modal Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/335743 --- .../components/delete_blob_modal.vue | 65 ++++++++++++++--- .../components/delete_blob_modal_spec.js | 72 +++++++++++++++---- 2 files changed, 116 insertions(+), 21 deletions(-) diff --git a/app/assets/javascripts/repository/components/delete_blob_modal.vue b/app/assets/javascripts/repository/components/delete_blob_modal.vue index c9b5a9ba4cb88c..394f1e7995abe4 100644 --- a/app/assets/javascripts/repository/components/delete_blob_modal.vue +++ b/app/assets/javascripts/repository/components/delete_blob_modal.vue @@ -2,6 +2,7 @@ import { GlModal, GlFormGroup, GlFormInput, GlFormTextarea, GlToggle, GlForm } from '@gitlab/ui'; import csrf from '~/lib/utils/csrf'; import { __ } from '~/locale'; +import validation from '~/vue_shared/directives/validation'; import { SECONDARY_OPTIONS_TEXT, COMMIT_LABEL, @@ -9,6 +10,13 @@ import { TOGGLE_CREATE_MR_LABEL, } from '../constants'; +const initFormField = ({ value, required = true, skipValidation = false }) => ({ + value, + required, + state: skipValidation ? true : null, + feedback: null, +}); + export default { csrf, components: { @@ -26,6 +34,9 @@ export default { TARGET_BRANCH_LABEL, TOGGLE_CREATE_MR_LABEL, }, + directives: { + validation: validation(), + }, props: { modalId: { type: String, @@ -61,12 +72,20 @@ export default { }, }, data() { + const form = { + state: false, + showValidation: false, + fields: { + // fields key must match case of form name for validation directive to work + commit_message: initFormField({ value: this.commitMessage }), + branch_name: initFormField({ value: this.targetBranch }), + }, + }; return { loading: false, - commit: this.commitMessage, - target: this.targetBranch, createNewMr: true, error: '', + form, }; }, computed: { @@ -77,7 +96,7 @@ export default { { variant: 'danger', loading: this.loading, - disabled: !this.formCompleted || this.loading, + disabled: this.loading || !this.form.state, }, ], }; @@ -92,17 +111,26 @@ export default { ], }; }, + /* eslint-disable dot-notation */ showCreateNewMrToggle() { - return this.canPushCode && this.target !== this.originalBranch; + return this.canPushCode && this.form.fields['branch_name'].value !== this.originalBranch; }, formCompleted() { - return this.commit && this.target; + return this.form.fields['commit_message'].value && this.form.fields['branch_name'].value; }, + /* eslint-enable dot-notation */ }, methods: { submitForm(e) { e.preventDefault(); // Prevent modal from closing + this.form.showValidation = true; + + if (!this.form.state) { + return; + } + this.loading = true; + this.form.showValidation = false; this.$refs.form.$el.submit(); }, }, @@ -119,7 +147,7 @@ export default { :action-cancel="cancelOptions" @primary="submitForm" > - +