All files / app/assets/javascripts/projects/commit/components form_modal.vue

80% Statements 8/10
100% Branches 1/1
100% Functions 5/5
80% Lines 8/10

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    2x                                                                                                 15x                                                                     2x     2x       2x     1x 1x 1x                                                                                                                                                        
<script>
import { GlModal, GlForm, GlFormCheckbox, GlSprintf, GlFormGroup } from '@gitlab/ui';
// eslint-disable-next-line no-restricted-imports
import { mapActions, mapState } from 'vuex';
import api from '~/api';
import { BV_SHOW_MODAL } from '~/lib/utils/constants';
import csrf from '~/lib/utils/csrf';
import eventHub from '../event_hub';
import BranchesDropdown from './branches_dropdown.vue';
import ProjectsDropdown from './projects_dropdown.vue';
 
export default {
  components: {
    BranchesDropdown,
    ProjectsDropdown,
    GlModal,
    GlForm,
    GlFormCheckbox,
    GlSprintf,
    GlFormGroup,
  },
  inject: {
    prependedText: {
      default: '',
    },
  },
  props: {
    i18n: {
      type: Object,
      required: true,
    },
    openModal: {
      type: String,
      required: true,
    },
    modalId: {
      type: String,
      required: true,
    },
    isCherryPick: {
      type: Boolean,
      required: false,
      default: false,
    },
    primaryActionEventName: {
      type: String,
      required: false,
      default: null,
    },
  },
  data() {
    return {
      checked: true,
      actionPrimary: {
        text: this.i18n.actionPrimaryText,
        attributes: {
          variant: 'confirm',
          category: 'primary',
          'data-testid': 'submit-commit',
        },
      },
      actionCancel: {
        text: this.i18n.actionCancelText,
        attributes: { 'data-testid': 'cancel-commit' },
      },
    };
  },
  computed: {
    ...mapState([
      'branch',
      'endpoint',
      'pushCode',
      'branchCollaboration',
      'modalTitle',
      'existingBranch',
      'targetProjectId',
      'targetProjectName',
      'branchesEndpoint',
    ]),
  },
  mounted() {
    eventHub.$on(this.openModal, this.show);
  },
  methods: {
    ...mapActions(['clearModal', 'setBranch', 'setSelectedBranch', 'setSelectedProject']),
    show() {
      this.$root.$emit(BV_SHOW_MODAL, this.modalId);
    },
    handlePrimary() {
      if (this.primaryActionEventName) {
        api.trackRedisHllUserEvent(this.primaryActionEventName);
      }
 
      this.$refs.form.$el.submit();
    },
    resetModalHandler() {
      this.clearModal();
      this.setSelectedBranch('');
      this.checked = true;
    },
  },
  csrf,
};
</script>
<template>
  <gl-modal
    v-bind="$attrs"
    data-testid="modal-commit"
    :modal-id="modalId"
    size="sm"
    :title="modalTitle"
    :action-cancel="actionCancel"
    :action-primary="actionPrimary"
    @hidden="resetModalHandler"
    @primary="handlePrimary"
  >
    <p v-if="prependedText.length" data-testid="prepended-text">
      <gl-sprintf :message="prependedText" />
    </p>
 
    <gl-form ref="form" :action="endpoint" method="post">
      <input type="hidden" name="authenticity_token" :value="$options.csrf.token" />
 
      <gl-form-group
        v-if="isCherryPick"
        :label="i18n.projectLabel"
        label-for="start_project"
        data-testid="dropdown-group"
      >
        <input
          id="target_project_id"
          type="hidden"
          name="target_project_id"
          :value="targetProjectId"
        />
 
        <projects-dropdown :value="targetProjectName" @input="setSelectedProject" />
      </gl-form-group>
 
      <gl-form-group
        :label="i18n.branchLabel"
        label-for="start_branch"
        data-testid="dropdown-group"
      >
        <input id="start_branch" type="hidden" name="start_branch" :value="branch" />
 
        <branches-dropdown :value="branch" @input="setBranch" />
      </gl-form-group>
 
      <gl-form-checkbox
        v-if="pushCode"
        v-model="checked"
        name="create_merge_request"
        class="gl-mt-3"
      >
        <gl-sprintf :message="i18n.startMergeRequest">
          <template #newMergeRequest>
            <strong>{{ i18n.newMergeRequest }}</strong>
          </template>
        </gl-sprintf>
      </gl-form-checkbox>
      <input v-else type="hidden" name="create_merge_request" value="1" />
    </gl-form>
 
    <p v-if="!pushCode" class="gl-mb-0 gl-mt-5" data-testid="appended-text">
      <gl-sprintf v-if="branchCollaboration" :message="i18n.existingBranch">
        <template #branchName>
          <strong>{{ existingBranch }}</strong>
        </template>
      </gl-sprintf>
      <gl-sprintf v-else :message="i18n.branchInFork" />
    </p>
  </gl-modal>
</template>