All files / app/assets/javascripts/branches/components delete_branch_modal.vue

90.47% Statements 19/21
100% Branches 9/9
100% Functions 13/13
90.47% Lines 19/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193        1x                       11x                       28x       28x     17x         6x         22x         17x         8x     19x                     19x 19x 19x 19x 19x   19x     3x 2x       1x                                                                                                                                                                                                                          
<script>
import { GlButton, GlFormInput, GlModal, GlSprintf, GlAlert } from '@gitlab/ui';
import csrf from '~/lib/utils/csrf';
import { sprintf, s__ } from '~/locale';
import eventHub from '../event_hub';
 
export default {
  csrf,
  components: {
    GlModal,
    GlButton,
    GlFormInput,
    GlSprintf,
    GlAlert,
  },
  data() {
    return {
      isProtectedBranch: false,
      branchName: '',
      defaultBranchName: '',
      deletePath: '',
      merged: false,
      enteredBranchName: '',
      modalId: 'delete-branch-modal',
    };
  },
  computed: {
    title() {
      const modalTitle = this.isProtectedBranch
        ? this.$options.i18n.modalTitleProtectedBranch
        : this.$options.i18n.modalTitle;
 
      return sprintf(modalTitle, { branchName: this.branchName });
    },
    modalMessage() {
      return this.isProtectedBranch
        ? this.$options.i18n.modalMessageProtectedBranch
        : this.$options.i18n.modalMessage;
    },
    undoneWarning() {
      return sprintf(this.$options.i18n.undoneWarning, {
        buttonText: this.buttonText,
      });
    },
    confirmationText() {
      return sprintf(this.$options.i18n.confirmationText, {
        branchName: this.branchName,
      });
    },
    buttonText() {
      return this.isProtectedBranch
        ? this.$options.i18n.deleteButtonTextProtectedBranch
        : this.$options.i18n.deleteButtonText;
    },
    branchNameConfirmed() {
      return this.enteredBranchName === this.branchName;
    },
    deleteButtonDisabled() {
      return this.isProtectedBranch && !this.branchNameConfirmed;
    },
  },
  mounted() {
    eventHub.$on('openModal', this.openModal);
  },
  destroyed() {
    eventHub.$off('openModal', this.openModal);
  },
  methods: {
    openModal({ isProtectedBranch, branchName, defaultBranchName, deletePath, merged }) {
      this.isProtectedBranch = isProtectedBranch;
      this.branchName = branchName;
      this.defaultBranchName = defaultBranchName;
      this.deletePath = deletePath;
      this.merged = merged;
 
      this.$refs.modal.show();
    },
    submitForm() {
      if (!this.deleteButtonDisabled) {
        this.$refs.form.submit();
      }
    },
    closeModal() {
      this.$refs.modal.hide();
    },
  },
  i18n: {
    modalTitle: s__('Branches|Delete branch. Are you ABSOLUTELY SURE?'),
    modalTitleProtectedBranch: s__('Branches|Delete protected branch. Are you ABSOLUTELY SURE?'),
    modalMessage: s__("Branches|You're about to permanently delete the branch %{branchName}."),
    modalMessageProtectedBranch: s__(
      "Branches|You're about to permanently delete the protected branch %{branchName}.",
    ),
    unmergedWarning: s__(
      "Branches|This branch hasn't been merged into %{defaultBranchName}. To avoid data loss, consider merging this branch before deleting it.",
    ),
    undoneWarning: s__(
      'Branches|After you confirm and select %{strongStart}%{buttonText},%{strongEnd} you cannot recover this branch.',
    ),
    cancelButtonText: s__('Branches|Cancel, keep branch'),
    confirmationText: s__(
      'Branches|Deleting the %{strongStart}%{branchName}%{strongEnd} branch cannot be undone. Are you sure?',
    ),
    confirmationTextProtectedBranch: s__('Branches|Please type the following to confirm:'),
    deleteButtonText: s__('Branches|Yes, delete branch'),
    deleteButtonTextProtectedBranch: s__('Branches|Yes, delete protected branch'),
  },
};
</script>
 
<template>
  <gl-modal ref="modal" size="sm" :modal-id="modalId" :title="title">
    <gl-alert class="gl-mb-5" variant="danger" :dismissible="false">
      <div data-testid="modal-message">
        <gl-sprintf :message="modalMessage">
          <template #branchName>
            <strong>
              <code class="gl-white-space-pre-wrap">{{ branchName }}</code>
            </strong>
          </template>
        </gl-sprintf>
        <p v-if="!merged" class="gl-mb-0 gl-mt-4">
          <gl-sprintf :message="$options.i18n.unmergedWarning">
            <template #defaultBranchName>
              <code class="gl-white-space-pre-wrap">{{ defaultBranchName }}</code>
            </template>
          </gl-sprintf>
        </p>
      </div>
    </gl-alert>
 
    <form ref="form" :action="deletePath" method="post" @submit.prevent>
      <div v-if="isProtectedBranch" class="gl-mt-4">
        <p>
          <gl-sprintf :message="undoneWarning">
            <template #strong="{ content }">
              <strong> {{ content }} </strong>
            </template>
          </gl-sprintf>
        </p>
        <p>
          <gl-sprintf :message="$options.i18n.confirmationTextProtectedBranch">
            <template #strong="{ content }">
              {{ content }}
            </template>
          </gl-sprintf>
          <code class="gl-white-space-pre-wrap">{{ branchName }}</code>
          <gl-form-input
            v-model="enteredBranchName"
            name="delete_branch_input"
            type="text"
            class="gl-mt-4"
            aria-labelledby="input-label"
            autocomplete="off"
            @keyup.enter="submitForm"
          />
        </p>
      </div>
      <div v-else>
        <p class="gl-mt-4">
          <gl-sprintf :message="confirmationText">
            <template #strong="{ content }">
              <strong>
                {{ content }}
              </strong>
            </template>
          </gl-sprintf>
        </p>
      </div>
 
      <input ref="method" type="hidden" name="_method" value="delete" />
      <input :value="$options.csrf.token" type="hidden" name="authenticity_token" />
    </form>
 
    <template #modal-footer>
      <div class="gl-display-flex gl-flex-direction-row gl-justify-content-end gl-flex-wrap gl-m-0">
        <gl-button data-testid="delete-branch-cancel-button" @click="closeModal">
          {{ $options.i18n.cancelButtonText }}
        </gl-button>
        <div class="gl-mr-3"></div>
        <gl-button
          ref="deleteBranchButton"
          :disabled="deleteButtonDisabled"
          variant="danger"
          data-testid="delete-branch-confirmation-button"
          @click="submitForm"
          >{{ buttonText }}</gl-button
        >
      </div>
    </template>
  </gl-modal>
</template>