All files / app/assets/javascripts/sidebar/components/reviewers uncollapsed_reviewer_list.vue

100% Statements 4/4
100% Branches 0/0
100% Functions 0/0
100% Lines 4/4

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            134x 134x 134x   134x                                                                                                                                                                                                                                                                                                                                                                                              
<script>
import { GlButton, GlTooltipDirective, GlIcon } from '@gitlab/ui';
import { TYPE_ISSUE } from '~/issues/constants';
import { __, sprintf, s__ } from '~/locale';
import ReviewerAvatarLink from './reviewer_avatar_link.vue';
 
const LOADING_STATE = 'loading';
const SUCCESS_STATE = 'success';
const JUST_APPROVED = 'approved';
 
const REVIEW_STATE_ICONS = {
  APPROVED: {
    name: 'check-circle',
    class: 'gl-text-green-500',
    title: __('Reviewer approved changes'),
  },
  REQUESTED_CHANGES: {
    name: 'error',
    class: 'gl-text-red-500',
    title: __('Reviewer requested changes'),
  },
  REVIEWED: {
    name: 'comment-lines',
    class: 'gl-text-blue-500',
    title: __('Reviewer commented'),
  },
  UNREVIEWED: {
    name: 'dash-circle',
    title: __('Awaiting review'),
  },
};
 
export default {
  i18n: {
    reRequestReview: __('Re-request review'),
  },
  components: {
    GlButton,
    GlIcon,
    ReviewerAvatarLink,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  props: {
    users: {
      type: Array,
      required: true,
    },
    rootPath: {
      type: String,
      required: true,
    },
    issuableType: {
      type: String,
      required: false,
      default: TYPE_ISSUE,
    },
  },
  data() {
    return {
      showLess: true,
      loadingStates: {},
    };
  },
  watch: {
    users: {
      handler(users, previousUsers) {
        this.loadingStates = users.reduce(
          (acc, user) => ({
            ...acc,
            [user.id]: acc[user.id] || null,
          }),
          this.loadingStates,
        );
        if (previousUsers) {
          users.forEach((user) => {
            const userPreviousState = previousUsers.find(({ id }) => id === user.id);
            if (
              userPreviousState &&
              user.mergeRequestInteraction.approved &&
              !userPreviousState.mergeRequestInteraction.approved
            ) {
              this.showApprovalAnimation(user.id);
            }
          });
        }
      },
      immediate: true,
    },
  },
  methods: {
    showApprovalAnimation(userId) {
      this.loadingStates[userId] = JUST_APPROVED;
 
      setTimeout(() => {
        this.loadingStates[userId] = null;
      }, 1500);
    },
    reviewedButNotApprovedTooltip(user) {
      return sprintf(s__('MergeRequest|Reviewed by @%{username} but not yet approved'), user);
    },
    toggleShowLess() {
      this.showLess = !this.showLess;
    },
    reRequestReview(userId) {
      this.loadingStates[userId] = LOADING_STATE;
      this.$emit('request-review', { userId, callback: this.requestReviewComplete });
    },
 
    requestReviewComplete(userId, success) {
      if (success) {
        this.loadingStates[userId] = SUCCESS_STATE;
 
        setTimeout(() => {
          this.loadingStates[userId] = null;
        }, 1500);
      } else {
        this.loadingStates[userId] = null;
      }
    },
    reviewStateIcon(user) {
      if (user.mergeRequestInteraction.approved) {
        return {
          ...REVIEW_STATE_ICONS.APPROVED,
          class: [
            REVIEW_STATE_ICONS.APPROVED.class,
            this.loadingStates[user.id] === JUST_APPROVED && 'merge-request-approved-icon',
          ],
        };
      }
      return (
        REVIEW_STATE_ICONS[user.mergeRequestInteraction.reviewState] ||
        REVIEW_STATE_ICONS.UNREVIEWED
      );
    },
    showRequestReviewButton(user) {
      if (!user.mergeRequestInteraction.approved) {
        return !['UNREVIEWED', 'UNAPPROVED'].includes(user.mergeRequestInteraction.reviewState);
      }
 
      return true;
    },
  },
  LOADING_STATE,
  SUCCESS_STATE,
};
</script>
 
<template>
  <div>
    <div
      v-for="(user, index) in users"
      :key="user.id"
      :class="{
        'gl-mb-3': index !== users.length - 1,
      }"
      class="gl-display-grid gl-align-items-center reviewer-grid gl-mr-2"
      data-testid="reviewer"
    >
      <reviewer-avatar-link
        :user="user"
        :root-path="rootPath"
        :issuable-type="issuableType"
        class="gl-word-break-word gl-mr-2"
        data-css-area="user"
      >
        <div class="gl-ml-3 gl-line-height-normal gl-display-grid gl-align-items-center">
          {{ user.name }}
        </div>
      </reviewer-avatar-link>
      <gl-button
        v-if="user.mergeRequestInteraction.canUpdate && showRequestReviewButton(user)"
        v-gl-tooltip.left
        :title="$options.i18n.reRequestReview"
        :aria-label="$options.i18n.reRequestReview"
        :loading="loadingStates[user.id] === $options.LOADING_STATE"
        class="gl-float-right gl-text-gray-500! gl-mr-2"
        size="small"
        icon="redo"
        variant="link"
        data-testid="re-request-button"
        @click="reRequestReview(user.id)"
      />
      <span
        v-gl-tooltip.top.viewport
        :title="reviewStateIcon(user).title"
        :class="reviewStateIcon(user).class"
        class="gl-float-right gl-my-2 gl-ml-auto gl-flex-shrink-0"
        data-testid="reviewer-state-icon-parent"
      >
        <gl-icon
          :size="reviewStateIcon(user).size || 16"
          :name="reviewStateIcon(user).name"
          :aria-label="reviewStateIcon(user).title"
          data-testid="reviewer-state-icon"
        />
      </span>
    </div>
  </div>
</template>