All files / app/assets/javascripts/badges/components badge_list.vue

81.81% Statements 9/11
50% Branches 3/6
87.5% Functions 7/8
81.81% Lines 9/11

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                    2x                                                     18x                   18x     20x     18x         18x                                                       48x 48x           57x                                                                                                                                                                        
<script>
import {
  GlBadge,
  GlLoadingIcon,
  GlTable,
  GlTooltipDirective,
  GlPagination,
  GlButton,
  GlModalDirective,
} from '@gitlab/ui';
// eslint-disable-next-line no-restricted-imports
import { mapActions, mapState } from 'vuex';
import { __, s__ } from '~/locale';
import { GROUP_BADGE, PROJECT_BADGE, INITIAL_PAGE, PAGE_SIZE } from '../constants';
import Badge from './badge.vue';
 
export default {
  PAGE_SIZE,
  INITIAL_PAGE,
  name: 'BadgeList',
  components: {
    Badge,
    GlBadge,
    GlLoadingIcon,
    GlTable,
    GlPagination,
    GlButton,
  },
  directives: {
    GlModal: GlModalDirective,
    GlTooltip: GlTooltipDirective,
  },
  i18n: {
    emptyGroupMessage: s__('Badges|This group has no badges. Add an existing badge or create one.'),
    emptyProjectMessage: s__('Badges|This project has no badges. Start by adding a new badge.'),
  },
  data() {
    return {
      currentPage: INITIAL_PAGE,
    };
  },
  computed: {
    ...mapState(['badges', 'isLoading', 'kind']),
    hasNoBadges() {
      return !this.isLoading && (!this.badges || !this.badges.length);
    },
    isGroupBadge() {
      return this.kind === GROUP_BADGE;
    },
    showPagination() {
      return this.badges.length > PAGE_SIZE;
    },
    emptyMessage() {
      return this.isGroupBadge
        ? this.$options.i18n.emptyGroupMessage
        : this.$options.i18n.emptyProjectMessage;
    },
    fields() {
      return [
        {
          key: 'name',
          label: __('Name'),
          tdClass: 'gl-vertical-align-middle!',
        },
        {
          key: 'badge',
          label: __('Badge'),
          tdClass: 'gl-vertical-align-middle!',
        },
        {
          key: 'url',
          label: __('URL'),
          tdClass: 'gl-vertical-align-middle!',
        },
        {
          key: 'actions',
          label: __('Actions'),
          thClass: 'gl-text-right',
          tdClass: 'gl-text-right',
        },
      ];
    },
  },
  methods: {
    ...mapActions(['editBadge', 'updateBadgeInModal']),
    badgeKindText(item) {
      if (item.kind === PROJECT_BADGE) {
        return s__('Badges|Project Badge');
      }
 
      return s__('Badges|Group Badge');
    },
    canEditBadge(item) {
      return item.kind === this.kind;
    },
  },
};
</script>
 
<template>
  <div>
    <gl-loading-icon v-show="isLoading" size="md" />
    <div data-testid="badge-list-content">
      <gl-table
        :empty-text="emptyMessage"
        :fields="fields"
        :items="badges"
        :per-page="$options.PAGE_SIZE"
        :current-page="currentPage"
        stacked="md"
        show-empty
        class="b-table-fixed"
        data-testid="badge-list"
      >
        <template #cell(name)="{ item }">
          <label v-gl-tooltip class="label-bold str-truncated mb-0" :title="item.name">{{
            item.name
          }}</label>
          <gl-badge size="sm">{{ badgeKindText(item) }}</gl-badge>
        </template>
 
        <template #cell(badge)="{ item }">
          <div class="overflow-hidden">
            <badge :image-url="item.renderedImageUrl" :link-url="item.renderedLinkUrl" />
          </div>
        </template>
 
        <template #cell(url)="{ item }">
          <span v-gl-tooltip :title="item.linkUrl" class="str-truncated">
            {{ item.linkUrl }}
          </span>
        </template>
 
        <template #cell(actions)="{ item }">
          <div v-if="canEditBadge(item)" class="table-action-buttons" data-testid="badge-actions">
            <gl-button
              v-gl-modal.edit-badge-modal
              :disabled="item.isDeleting"
              class="gl-mr-3"
              variant="default"
              icon="pencil"
              size="medium"
              :aria-label="__('Edit')"
              data-testid="edit-badge-button"
              @click="editBadge(item)"
            />
            <gl-button
              v-gl-modal.delete-badge-modal
              :disabled="item.isDeleting"
              category="secondary"
              variant="danger"
              icon="remove"
              size="medium"
              :aria-label="__('Delete')"
              data-testid="delete-badge"
              @click="updateBadgeInModal(item)"
            />
            <gl-loading-icon v-show="item.isDeleting" size="sm" :inline="true" />
          </div>
        </template>
      </gl-table>
 
      <gl-pagination
        v-if="showPagination"
        v-model="currentPage"
        :per-page="$options.PAGE_SIZE"
        :total-items="badges.length"
        :prev-text="__('Prev')"
        :next-text="__('Next')"
        :label-next-page="__('Go to next page')"
        :label-prev-page="__('Go to previous page')"
        align="center"
        class="gl-mt-5"
      />
    </div>
  </div>
</template>