All files / app/assets/javascripts/ide/components/branches search_list.vue

72.72% Statements 8/11
66.66% Branches 2/3
77.77% Functions 7/9
72.72% Lines 8/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      7x                     6x               5x     4x                 6x         7x     1x                   10x                                                                                            
<script>
import { GlLoadingIcon, GlIcon } from '@gitlab/ui';
import { debounce } from 'lodash';
// eslint-disable-next-line no-restricted-imports
import { mapActions, mapState } from 'vuex';
import Item from './item.vue';
 
export default {
  components: {
    Item,
    GlIcon,
    GlLoadingIcon,
  },
  data() {
    return {
      search: '',
    };
  },
  computed: {
    ...mapState('branches', ['branches', 'isLoading']),
    ...mapState(['currentBranchId', 'currentProjectId']),
    hasBranches() {
      return this.branches.length !== 0;
    },
    hasNoSearchResults() {
      return this.search !== '' && !this.hasBranches;
    },
  },
  watch: {
    isLoading: {
      handler: 'focusSearch',
    },
  },
  mounted() {
    this.loadBranches();
  },
  methods: {
    ...mapActions('branches', ['fetchBranches']),
    loadBranches() {
      this.fetchBranches({ search: this.search });
    },
    searchBranches: debounce(function debounceSearch() {
      this.loadBranches();
    }, 250),
    focusSearch() {
      Iif (!this.isLoading) {
        this.$nextTick(() => {
          this.$refs.searchInput.focus();
        });
      }
    },
    isActiveBranch(item) {
      return item.name === this.currentBranchId;
    },
  },
};
</script>
 
<template>
  <div>
    <label
      class="dropdown-input gl-pt-3 gl-pb-5 gl-mb-0 gl-border-b-1 gl-border-b-solid gl-display-block"
      @click.stop
    >
      <input
        ref="searchInput"
        v-model="search"
        :placeholder="__('Search branches')"
        type="search"
        class="form-control dropdown-input-field"
        @input="searchBranches"
      />
      <gl-icon name="search" class="gl-ml-5 gl-mt-1 input-icon" />
    </label>
    <div class="dropdown-content ide-merge-requests-dropdown-content d-flex">
      <gl-loading-icon
        v-if="isLoading"
        size="lg"
        class="mt-3 mb-3 align-self-center ml-auto mr-auto"
      />
      <ul v-else class="mb-0 gl-w-full">
        <template v-if="hasBranches">
          <li v-for="item in branches" :key="item.name">
            <item :item="item" :project-id="currentProjectId" :is-active="isActiveBranch(item)" />
          </li>
        </template>
        <li
          v-else
          class="ide-search-list-empty d-flex gl-align-items-center justify-content-center"
        >
          <template v-if="hasNoSearchResults">
            {{ __('No branches found') }}
          </template>
        </li>
      </ul>
    </div>
  </div>
</template>