All files / app/assets/javascripts/sidebar/components/lock issuable_lock_form.vue

65.38% Statements 17/26
46.15% Branches 6/13
81.81% Functions 18/22
65.38% Lines 17/26

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              5x                                                                                             2x               2x     2x     2x         2x           2x     2x     1x         1x         1x         1x         1x         1x                                           2x   2x                               2x                                                                                            
<script>
import {
  GlLoadingIcon,
  GlDisclosureDropdownItem,
  GlTooltipDirective,
  GlOutsideDirective as Outside,
} from '@gitlab/ui';
// eslint-disable-next-line no-restricted-imports
import { mapGetters, mapActions } from 'vuex';
import { TYPE_ISSUE } from '~/issues/constants';
import { __, sprintf } from '~/locale';
import { createAlert } from '~/alert';
import toast from '~/vue_shared/plugins/global_toast';
import eventHub from '../../event_hub';
 
export default {
  locked: {
    icon: 'lock',
    class: 'value',
    displayText: __('Locked'),
  },
  unlocked: {
    class: ['no-value hide-collapsed'],
    icon: 'lock-open',
    displayText: __('Unlocked'),
  },
  components: {
    GlLoadingIcon,
    GlDisclosureDropdownItem,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
    Outside,
  },
  inject: ['fullPath'],
  props: {
    isEditable: {
      required: true,
      type: Boolean,
    },
  },
  i18n: {
    issue: __('issue'),
    issueCapitalized: __('Issue'),
    mergeRequest: __('merge request'),
    mergeRequestCapitalized: __('Merge request'),
    lockingMergeRequest: __('Locking discussion'),
    unlockingMergeRequest: __('Unlocking discussion'),
    lockMergeRequest: __('Lock discussion'),
    unlockMergeRequest: __('Unlock discussion'),
    lockedMessage: __('Discussion locked.'),
    unlockedMessage: __('Discussion unlocked.'),
  },
  data() {
    return {
      isLoading: false,
      isLockDialogOpen: false,
    };
  },
  computed: {
    ...mapGetters(['getNoteableData']),
    isIssuable() {
      return this.getNoteableData.targetType === TYPE_ISSUE;
    },
    issuableDisplayName() {
      return this.isIssuable ? this.$options.i18n.issue : this.$options.i18n.mergeRequest;
    },
    issuableDisplayNameCapitalized() {
      return this.isIssuable
        ? this.$options.i18n.issueCapitalized
        : this.$options.i18n.mergeRequestCapitalized;
    },
    isLocked() {
      return this.getNoteableData.discussion_locked;
    },
    lockStatus() {
      return this.isLocked ? this.$options.locked : this.$options.unlocked;
    },
    lockToggleInProgressText() {
      return this.isLocked ? this.unlockingMergeRequestText : this.lockingMergeRequestText;
    },
    lockToggleText() {
      return this.isLocked ? this.unlockMergeRequestText : this.lockMergeRequestText;
    },
    lockingMergeRequestText() {
      return sprintf(this.$options.i18n.lockingMergeRequest, {
        issuableDisplayName: this.issuableDisplayName,
      });
    },
    unlockingMergeRequestText() {
      return sprintf(this.$options.i18n.unlockingMergeRequest, {
        issuableDisplayName: this.issuableDisplayName,
      });
    },
    lockMergeRequestText() {
      return sprintf(this.$options.i18n.lockMergeRequest, {
        issuableDisplayName: this.issuableDisplayName,
      });
    },
    unlockMergeRequestText() {
      return sprintf(this.$options.i18n.unlockMergeRequest, {
        issuableDisplayName: this.issuableDisplayName,
      });
    },
    lockedMessageText() {
      return sprintf(this.$options.i18n.lockedMessage, {
        issuableDisplayName: this.issuableDisplayNameCapitalized,
      });
    },
    unlockedMessageText() {
      return sprintf(this.$options.i18n.unlockedMessage, {
        issuableDisplayName: this.issuableDisplayNameCapitalized,
      });
    },
  },
 
  created() {
    eventHub.$on('closeLockForm', this.toggleForm);
  },
 
  beforeDestroy() {
    eventHub.$off('closeLockForm', this.toggleForm);
  },
 
  methods: {
    ...mapActions(['updateLockedAttribute']),
    toggleForm() {
      Iif (this.isEditable) {
        this.isLockDialogOpen = !this.isLockDialogOpen;
      }
    },
    toggleLocked() {
      this.isLoading = true;
 
      this.updateLockedAttribute({
        locked: !this.isLocked,
        fullPath: this.fullPath,
      })
        .then(() => {
          toast(this.isLocked ? this.lockedMessageText : this.unlockedMessageText);
        })
        .catch(() => {
          const alertMessage = __(
            'Something went wrong trying to change the locked state of the discussion',
          );
          createAlert({
            message: sprintf(alertMessage, { issuableDisplayName: this.issuableDisplayName }),
          });
        })
        .finally(() => {
          this.isLoading = false;
        });
    },
    closeForm() {
      this.isLockDialogOpen = false;
    },
  },
};
</script>
 
<template>
  <li v-if="isIssuable" class="gl-new-dropdown-item">
    <button
      type="button"
      class="gl-new-dropdown-item-content"
      data-testid="issuable-lock"
      @click="toggleLocked"
    >
      <span class="gl-new-dropdown-item-text-wrapper">
        <template v-if="isLoading">
          <gl-loading-icon inline size="sm" /> {{ lockToggleInProgressText }}
        </template>
        <template v-else>
          {{ lockToggleText }}
        </template>
      </span>
    </button>
  </li>
  <gl-disclosure-dropdown-item v-else>
    <button
      type="button"
      class="gl-new-dropdown-item-content"
      data-testid="issuable-lock"
      @click="toggleLocked"
    >
      <span class="gl-new-dropdown-item-text-wrapper">
        <template v-if="isLoading">
          <gl-loading-icon inline size="sm" /> {{ lockToggleInProgressText }}
        </template>
        <template v-else>
          {{ lockToggleText }}
        </template>
      </span>
    </button>
  </gl-disclosure-dropdown-item>
</template>