summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/projects/gke_cluster_dropdowns/components/gke_zone_dropdown.vue
blob: 9a99d986d05cca5bdea4b9d5f5789fc0eb58e8a6 (plain)
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
<script>
import { s__ } from '~/locale';
import { mapState, mapGetters, mapActions } from 'vuex';

import gkeDropdownMixin from './gke_dropdown_mixin';

export default {
  name: 'GkeZoneDropdown',
  mixins: [gkeDropdownMixin],
  data() {
    return {
      searchPlaceholderText: s__('ClusterIntegration|Search zones'),
      noSearchResultsText: s__('ClusterIntegration|No zones matched your search'),
    };
  },
  computed: {
    ...mapState(['selectedProject', 'selectedZone']),
    ...mapState({ items: 'zones' }),
    ...mapGetters(['hasProject']),
    isDisabled() {
      return !this.hasProject;
    },
    toggleText() {
      if (this.isLoading) {
        return s__('ClusterIntegration|Fetching zones');
      }

      if (this.selectedZone) {
        return this.selectedZone;
      }

      return !this.hasProject
        ? s__('ClusterIntegration|Select project to choose zone')
        : s__('ClusterIntegration|Select zone');
    },
  },
  watch: {
    selectedProject() {
      this.isLoading = true;

      this.getZones()
        .then(this.fetchSuccessHandler)
        .catch(this.fetchFailureHandler);
    },
  },
  methods: {
    ...mapActions(['getZones']),
    ...mapActions({ setItem: 'setZone' }),
  },
};
</script>

<template>
  <div
    class="js-gcp-zone-dropdown dropdown"
    :class="{ 'gl-show-field-errors': hasErrors }"
  >
    <dropdown-hidden-input
      :name="fieldName"
      :value="selectedZone"
    />
    <dropdown-button
      :class="{ 'gl-field-error-outline': hasErrors }"
      :is-disabled="isDisabled"
      :is-loading="isLoading"
      :toggle-text="toggleText"
    />
    <div class="dropdown-menu dropdown-select">
      <dropdown-search-input
        v-model="searchQuery"
        :placeholder-text="searchPlaceholderText"
      />
      <div class="dropdown-content">
        <ul>
          <li v-show="!results.length">
            <span class="menu-item">{{ noSearchResultsText }}</span>
          </li>
          <li
            v-for="result in results"
            :key="result.id"
          >
            <button @click.prevent="setItem(result.name)">
              {{ result.name }}
            </button>
          </li>
        </ul>
      </div>
      <div class="dropdown-loading">
        <loading-icon />
      </div>
    </div>
  </div>
</template>