All files / app/assets/javascripts/notes/components comment_type_dropdown.vue

90.47% Statements 19/21
94.73% Branches 18/19
81.81% Functions 9/11
90% Lines 18/20

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          10x                                                                                                             67x   67x     67x   67x     67x   67x 4x   63x               67x   67x 4x       63x     67x   67x         67x                                 13x     1x                                                                    
<script>
import { GlButtonGroup, GlButton, GlCollapsibleListbox } from '@gitlab/ui';
 
import { sprintf, __ } from '~/locale';
import { COMMENT_FORM } from '~/notes/i18n';
import * as constants from '../constants';
 
export default {
  name: 'CommentTypeDropdown',
  i18n: {
    ...COMMENT_FORM,
    toggleSrText: __('Comment type'),
  },
  components: {
    GlButtonGroup,
    GlButton,
    GlCollapsibleListbox,
  },
  model: {
    prop: 'noteType',
    event: 'change',
  },
  props: {
    disabled: {
      type: Boolean,
      required: false,
      default: false,
    },
    trackingLabel: {
      type: String,
      required: false,
      default: undefined,
    },
    discussionsRequireResolution: {
      type: Boolean,
      required: false,
      default: false,
    },
    isInternalNote: {
      type: Boolean,
      required: false,
      default: false,
    },
    noteableDisplayName: {
      type: String,
      required: true,
    },
    noteType: {
      type: String,
      required: true,
    },
  },
  computed: {
    isNoteTypeComment() {
      return this.noteType === constants.COMMENT;
    },
    isNoteTypeDiscussion() {
      return this.noteType === constants.DISCUSSION;
    },
    dropdownCommentButtonTitle() {
      const { comment, internalComment } = this.$options.i18n.submitButton;
 
      return this.isInternalNote ? internalComment : comment;
    },
    dropdownStartThreadButtonTitle() {
      const { startThread, startInternalThread } = this.$options.i18n.submitButton;
 
      return this.isInternalNote ? startInternalThread : startThread;
    },
    commentButtonTitle() {
      const { comment, internalComment, startThread, startInternalThread } = this.$options.i18n;
 
      if (this.isInternalNote) {
        return this.noteType === constants.COMMENT ? internalComment : startInternalThread;
      }
      return this.noteType === constants.COMMENT ? comment : startThread;
    },
    startDiscussionDescription() {
      const {
        discussionThatNeedsResolution,
        internalDiscussionThatNeedsResolution,
        discussion,
        internalDiscussion,
      } = this.$options.i18n;
 
      if (this.isInternalNote) {
        return this.discussionsRequireResolution
          ? internalDiscussionThatNeedsResolution
          : internalDiscussion;
      }
      return this.discussionsRequireResolution ? discussionThatNeedsResolution : discussion;
    },
    commentDescription() {
      const { commentHelp, internalCommentHelp } = this.$options.i18n.submitButton;
 
      return sprintf(this.isInternalNote ? internalCommentHelp : commentHelp, {
        noteableDisplayName: this.noteableDisplayName,
      });
    },
    dropdownItems() {
      return [
        {
          text: this.dropdownCommentButtonTitle,
          description: this.commentDescription,
          value: constants.COMMENT,
        },
        {
          text: this.dropdownStartThreadButtonTitle,
          description: this.startDiscussionDescription,
          value: constants.DISCUSSION,
          testid: 'discussion-menu-item',
        },
      ];
    },
  },
  methods: {
    handleClick() {
      this.$emit('click');
    },
    setNoteType(value) {
      this.$emit('change', value);
    },
  },
};
</script>
 
<template>
  <gl-button-group
    class="js-comment-button js-comment-submit-button comment-type-dropdown gl-w-full gl-mb-3 gl-md-w-auto gl-md-mb-0"
    :data-track-label="trackingLabel"
    data-track-action="click_button"
    data-testid="comment-button"
  >
    <gl-button variant="confirm" :disabled="disabled" @click="handleClick">
      {{ commentButtonTitle }}
    </gl-button>
    <gl-collapsible-listbox
      variant="confirm"
      text-sr-only
      :toggle-text="$options.i18n.toggleSrText"
      :disabled="disabled"
      :items="dropdownItems"
      :selected="noteType"
      @select="setNoteType"
    >
      <template #list-item="{ item }">
        <div :data-testid="item.testid">
          <strong>{{ item.text }}</strong>
          <p class="gl-m-0">{{ item.description }}</p>
        </div>
      </template>
    </gl-collapsible-listbox>
  </gl-button-group>
</template>