All files / app/assets/javascripts/pages/admin/projects/components namespace_select.vue

95.65% Statements 22/23
75% Branches 3/4
100% Functions 12/12
95.65% Lines 22/23

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          1x                                                                   13x                   21x         8x 2x     8x       13x       15x 15x   15x 15x 15x       15x       15x 30x             6x 6x     30x     2x 2x     2x 2x                                                              
<script>
import { debounce } from 'lodash';
import { GlCollapsibleListbox } from '@gitlab/ui';
import Api from '~/api';
import { DEFAULT_DEBOUNCE_AND_THROTTLE_MS } from '~/lib/utils/constants';
import { __ } from '~/locale';
 
export default {
  i18n: {
    headerText: __('Namespaces'),
    searchPlaceholder: __('Search for Namespace'),
    reset: __('Clear'),
  },
  components: {
    GlCollapsibleListbox,
  },
  props: {
    origSelectedId: {
      type: String,
      required: false,
      default: '',
    },
    origSelectedText: {
      type: String,
      required: false,
      default: '',
    },
    toggleTextPlaceholder: {
      type: String,
      required: false,
      default: __('Namespace'),
    },
    fieldName: {
      type: String,
      required: false,
      default: null,
    },
  },
  data() {
    return {
      namespaceOptions: [],
      selectedNamespaceId: this.origSelectedId,
      selectedNamespaceText: this.origSelectedText,
      searchTerm: '',
      isLoading: false,
    };
  },
  computed: {
    toggleText() {
      return this.selectedNamespaceText || this.toggleTextPlaceholder;
    },
  },
  watch: {
    selectedNamespaceId(val) {
      if (!val) {
        this.selectedNamespaceText = null;
      }
 
      this.selectedNamespaceText = this.namespaceOptions.find(({ value }) => value === val)?.text;
    },
  },
  mounted() {
    this.fetchNamespaces();
  },
  methods: {
    fetchNamespaces() {
      this.isLoading = true;
      this.namespaceOptions = [];
 
      return Api.namespaces(this.searchTerm, (namespaces) => {
        this.namespaceOptions = this.formatNamespaceOptions(namespaces);
        this.isLoading = false;
      });
    },
    formatNamespaceOptions(namespaces) {
      Iif (!namespaces) {
        return [];
      }
 
      return namespaces.map((namespace) => {
        return {
          value: String(namespace.id),
          text: this.getNamespaceString(namespace),
        };
      });
    },
    selectNamespace(value) {
      this.selectedNamespaceId = value;
      this.$emit('setNamespace', this.selectedNamespaceId);
    },
    getNamespaceString(namespace) {
      return `${namespace.kind}: ${namespace.full_path}`;
    },
    search: debounce(function debouncedSearch(searchQuery) {
      this.searchTerm = searchQuery?.trim();
      this.fetchNamespaces();
    }, DEFAULT_DEBOUNCE_AND_THROTTLE_MS),
    onReset() {
      this.selectedNamespaceId = null;
      this.$emit('setNamespace', null);
    },
  },
};
</script>
 
<template>
  <div class="gl-display-flex gl-w-full">
    <input
      v-if="fieldName"
      :name="fieldName"
      :value="selectedNamespaceId"
      type="hidden"
      data-testid="hidden-input"
    />
    <gl-collapsible-listbox
      :items="namespaceOptions"
      :header-text="$options.i18n.headerText"
      :reset-button-label="$options.i18n.reset"
      :toggle-text="toggleText"
      :search-placeholder="$options.i18n.searchPlaceholder"
      :searching="isLoading"
      :selected="selectedNamespaceId"
      toggle-class="gl-w-full gl-flex-direction-column gl-align-items-stretch!"
      searchable
      @reset="onReset"
      @search="search"
      @select="selectNamespace"
    />
  </div>
</template>