All files / app/assets/javascripts/ide/components/commit_sidebar form.vue

72.41% Statements 21/29
46.15% Branches 12/26
68.75% Functions 11/16
72.41% Lines 21/29

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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231      3x                                             37x                         44x     39x 11x     28x     41x     29x     30x   30x                     7x       7x 7x                           2x 1x     1x     40x 1x   39x                   2x 2x                                                                                                                                                                                                                                                    
<!-- eslint-disable vue/multi-word-component-names -->
<script>
import { GlModal, GlButton, GlTooltipDirective } from '@gitlab/ui';
// eslint-disable-next-line no-restricted-imports
import { mapState, mapActions, mapGetters } from 'vuex';
import SafeHtml from '~/vue_shared/directives/safe_html';
import { n__ } from '~/locale';
import { leftSidebarViews, MAX_WINDOW_HEIGHT_COMPACT } from '../../constants';
import { createUnexpectedCommitError } from '../../lib/errors';
import Actions from './actions.vue';
import CommitMessageField from './message_field.vue';
import SuccessMessage from './success_message.vue';
 
export default {
  components: {
    Actions,
    CommitMessageField,
    SuccessMessage,
    GlModal,
    GlButton,
  },
  directives: {
    SafeHtml,
    GlTooltip: GlTooltipDirective,
  },
  data() {
    return {
      isCompact: true,
      componentHeight: null,
      // Keep track of "lastCommitError" so we hold onto the value even when "commitError" is cleared.
      lastCommitError: createUnexpectedCommitError(),
    };
  },
  computed: {
    ...mapState(['changedFiles', 'stagedFiles', 'currentActivityView', 'lastCommitMsg']),
    ...mapState('commit', ['commitMessage', 'submitCommitLoading', 'commitError']),
    ...mapGetters(['someUncommittedChanges', 'canPushCodeStatus']),
    ...mapGetters('commit', ['discardDraftButtonDisabled', 'preBuiltCommitMessage']),
    commitButtonDisabled() {
      return !this.canPushCodeStatus.isAllowed || !this.someUncommittedChanges;
    },
    commitButtonTooltip() {
      if (!this.canPushCodeStatus.isAllowed) {
        return this.canPushCodeStatus.messageShort;
      }
 
      return '';
    },
    overviewText() {
      return n__('%d changed file', '%d changed files', this.stagedFiles.length);
    },
    currentViewIsCommitView() {
      return this.currentActivityView === leftSidebarViews.commit.name;
    },
    commitErrorPrimaryAction() {
      const { primaryAction } = this.lastCommitError || {};
 
      return {
        button: primaryAction ? { text: primaryAction.text } : undefined,
        callback: primaryAction?.callback?.bind(this, this.$store) || (() => {}),
      };
    },
  },
  watch: {
    currentActivityView: 'handleCompactState',
    someUncommittedChanges: 'handleCompactState',
    lastCommitMsg: 'handleCompactState',
    commitError(val) {
      Iif (!val) {
        return;
      }
 
      this.lastCommitError = val;
      this.$refs.commitErrorModal.show();
    },
  },
  methods: {
    ...mapActions(['updateActivityBarView']),
    ...mapActions('commit', [
      'updateCommitMessage',
      'discardDraft',
      'commitChanges',
      'updateCommitAction',
    ]),
    commit() {
      // Even though the submit button will be disabled, we need to disable the submission
      // since hitting enter on the branch name text input also submits the form.
      if (!this.canPushCodeStatus.isAllowed) {
        return false;
      }
 
      return this.commitChanges();
    },
    handleCompactState() {
      if (this.lastCommitMsg) {
        this.isCompact = false;
      } else {
        this.isCompact =
          !this.someUncommittedChanges ||
          !this.currentViewIsCommitView ||
          window.innerHeight < MAX_WINDOW_HEIGHT_COMPACT;
      }
    },
    toggleIsCompact() {
      this.isCompact = !this.isCompact;
    },
    beginCommit() {
      return this.updateActivityBarView(leftSidebarViews.commit.name).then(() => {
        this.isCompact = false;
      });
    },
    beforeEnterTransition() {
      const elHeight = this.isCompact
        ? this.$refs.formEl && this.$refs.formEl.offsetHeight
        : this.$refs.compactEl && this.$refs.compactEl.offsetHeight;
 
      this.componentHeight = elHeight;
    },
    enterTransition() {
      this.$nextTick(() => {
        const elHeight = this.isCompact
          ? this.$refs.compactEl && this.$refs.compactEl.offsetHeight
          : this.$refs.formEl && this.$refs.formEl.offsetHeight;
 
        this.componentHeight = elHeight;
      });
    },
    afterEndTransition() {
      this.componentHeight = null;
    },
  },
};
</script>
 
<template>
  <div
    :class="{
      'is-compact': isCompact,
      'is-full': !isCompact,
    }"
    :style="{
      height: componentHeight ? `${componentHeight}px` : null,
    }"
    class="multi-file-commit-form"
  >
    <transition
      name="commit-form-slide-up"
      @before-enter="beforeEnterTransition"
      @enter="enterTransition"
      @after-enter="afterEndTransition"
    >
      <div v-if="isCompact" ref="compactEl" class="commit-form-compact">
        <div
          v-gl-tooltip="{ title: commitButtonTooltip }"
          data-testid="begin-commit-button-tooltip"
        >
          <gl-button
            :disabled="commitButtonDisabled"
            category="primary"
            variant="confirm"
            block
            data-testid="begin-commit-button"
            @click="beginCommit"
          >
            {{ __('Create commit...') }}
          </gl-button>
        </div>
        <p class="text-center bold">{{ overviewText }}</p>
      </div>
      <form v-else ref="formEl" @submit.prevent.stop="commit">
        <transition name="fade"> <success-message v-show="lastCommitMsg" /> </transition>
        <commit-message-field
          :text="commitMessage"
          :placeholder="preBuiltCommitMessage"
          @input="updateCommitMessage"
          @submit="commit"
        />
        <div class="clearfix gl-mt-5">
          <actions />
          <div
            v-gl-tooltip="{ title: commitButtonTooltip }"
            class="float-left"
            data-testid="commit-button-tooltip"
          >
            <gl-button
              :disabled="commitButtonDisabled"
              :loading="submitCommitLoading"
              data-testid="commit-button"
              category="primary"
              variant="confirm"
              type="submit"
            >
              {{ __('Commit') }}
            </gl-button>
          </div>
          <gl-button
            v-if="!discardDraftButtonDisabled"
            class="gl-float-right"
            data-testid="discard-draft"
            @click="discardDraft"
          >
            {{ __('Discard draft') }}
          </gl-button>
          <gl-button
            v-else
            type="button"
            class="gl-float-right"
            category="secondary"
            variant="default"
            @click="toggleIsCompact"
          >
            {{ __('Collapse') }}
          </gl-button>
        </div>
        <gl-modal
          ref="commitErrorModal"
          modal-id="ide-commit-error-modal"
          :title="lastCommitError.title"
          :action-primary="commitErrorPrimaryAction.button"
          :action-cancel="/* eslint-disable @gitlab/vue-no-new-non-primitive-in-template */ {
            text: __('Cancel'),
          } /* eslint-enable @gitlab/vue-no-new-non-primitive-in-template */"
          @ok="commitErrorPrimaryAction.callback"
        >
          <div v-safe-html="lastCommitError.messageHTML"></div>
        </gl-modal>
      </form>
    </transition>
  </div>
</template>