summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/projects/gke_cluster_dropdowns/components/gke_zone_dropdown.vue
blob: fd5d5f8640130204b944a775cedf9e90aed64ed4 (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
94
95
96
97
98
99
100
101
102
103
<script>
import { sprintf, s__ } from '~/locale';
import { mapState, mapActions } from 'vuex';

import gkeDropdownMixin from './gke_dropdown_mixin';

export default {
  name: 'GkeZoneDropdown',
  mixins: [gkeDropdownMixin],
  computed: {
    ...mapState([
      'selectedProject',
      'selectedZone',
      'projects',
      'isValidatingProjectBilling',
      'projectHasBillingEnabled',
    ]),
    ...mapState({ items: 'zones' }),
    isDisabled() {
      return this.isLoading || this.isValidatingProjectBilling || !this.projectHasBillingEnabled;
    },
    toggleText() {
      if (this.isLoading) {
        return s__('ClusterIntegration|Fetching zones');
      }

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

      return !this.projectHasBillingEnabled
        ? s__('ClusterIntegration|Select project to choose zone')
        : s__('ClusterIntegration|Select zone');
    },
    errorMessage() {
      return sprintf(
        s__('ClusterIntegration|An error occurred while trying to fetch project zones: %{error}'),
        { error: this.gapiError },
      );
    },
  },
  watch: {
    isValidatingProjectBilling(isValidating) {
      this.hasErrors = false;

      if (!isValidating && this.projectHasBillingEnabled) {
        this.isLoading = true;

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

<template>
  <div>
    <div class="js-gcp-zone-dropdown dropdown">
      <dropdown-hidden-input :name="fieldName" :value="selectedZone" />
      <dropdown-button
        :class="{ 'border-danger': hasErrors }"
        :is-disabled="isDisabled"
        :is-loading="isLoading"
        :toggle-text="toggleText"
      />
      <div class="dropdown-menu dropdown-select">
        <dropdown-search-input
          v-model="searchQuery"
          :placeholder-text="s__('ClusterIntegration|Search zones')"
        />
        <div class="dropdown-content">
          <ul>
            <li v-show="!results.length">
              <span class="menu-item">
                {{ s__('ClusterIntegration|No zones matched your search') }}
              </span>
            </li>
            <li v-for="result in results" :key="result.id">
              <button type="button" @click.prevent="setItem(result.name)">{{ result.name }}</button>
            </li>
          </ul>
        </div>
        <div class="dropdown-loading"><gl-loading-icon /></div>
      </div>
    </div>
    <span
      v-if="hasErrors"
      :class="{
        'text-danger': hasErrors,
        'text-muted': !hasErrors,
      }"
      class="form-text"
    >
      {{ errorMessage }}
    </span>
  </div>
</template>