summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/projects/gke_cluster_dropdowns/components/gke_machine_type_dropdown.vue
blob: 2ab1e48488cf866c69b864d9bb82aa300d0acdb3 (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
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
<script>
import _ from 'underscore';
import { s__ } from '~/locale';
import { mapState, mapGetters, mapActions } from 'vuex';
import LoadingIcon from '~/vue_shared/components/loading_icon.vue';
import DropdownSearchInput from '~/vue_shared/components/dropdown/dropdown_search_input.vue';
import DropdownHiddenInput from '~/vue_shared/components/dropdown/dropdown_hidden_input.vue';
import DropdownButton from '~/vue_shared/components/dropdown/dropdown_button.vue';

import eventHub from '../eventhub';
import store from '../stores';

export default {
  name: 'GkeMachineTypeDropdown',
  store,
  components: {
    LoadingIcon,
    DropdownButton,
    DropdownSearchInput,
    DropdownHiddenInput,
  },
  props: {
    fieldId: {
      type: String,
      required: true,
    },
    fieldName: {
      type: String,
      required: true,
    },
    defaultValue: {
      type: String,
      required: false,
      default: '',
    },
  },
  data() {
    return {
      isLoading: false,
      hasErrors: false,
      searchQuery: '',
    };
  },
  computed: {
    ...mapState(['selectedProject', 'selectedZone', 'selectedMachineType', 'machineTypes']),
    ...mapGetters(['hasProject', 'hasZone', 'hasMachineType']),
    isDisabled() {
      return !this.selectedProject || !this.selectedZone;
    },
    results() {
      return this.machineTypes.filter(
        item => item.name.toLowerCase().indexOf(this.searchQuery) > -1,
      );
    },
    toggleText() {
      if (this.isLoading) {
        return s__('ClusterIntegration|Fetching machine types');
      }

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

      if (!this.hasProject && !this.hasZone) {
        return s__('ClusterIntegration|Select project and zone to choose machine type');
      }

      return !this.hasZone
        ? s__('ClusterIntegration|Select zone to choose machine type')
        : s__('ClusterIntegration|Select machine type');
    },
    searchPlaceholderText() {
      return s__('ClusterIntegration|Search machine types');
    },
    noSearchResultsText() {
      return s__('ClusterIntegration|No machine types matched your search');
    },
  },
  created() {
    eventHub.$on('zoneSelected', this.fetchMachineTypes);
    eventHub.$on('machineTypeSelected', this.enableSubmit);
  },
  methods: {
    ...mapActions(['setMachineType', 'getMachineTypes']),
    fetchMachineTypes() {
      this.isLoading = true;

      this.getMachineTypes()
        .then(() => {
          if (this.defaultValue) {
            const machineTypeToSelect = _.find(
              this.machineTypes,
              item => item.name === this.defaultValue,
            );

            if (machineTypeToSelect) {
              this.setMachineType(machineTypeToSelect.name);
            }
          }

          this.isLoading = false;
          this.hasErrors = false;
        })
        .catch(() => {
          this.isLoading = false;
          this.hasErrors = true;
        });
    },
    enableSubmit() {
      if (this.hasProject && this.hasZone && this.hasMachineType) {
        const submitButtonEl = document.querySelector('.js-gke-cluster-creation-submit');

        if (submitButtonEl) {
          submitButtonEl.removeAttribute('disabled');
        }
      }
    },
  },
};
</script>

<template>
  <div
    class="js-gcp-machine-type-dropdown dropdown"
    :class="{ 'gl-show-field-errors': hasErrors }"
  >
    <dropdown-hidden-input
      :name="fieldName"
      :value="selectedMachineType"
    />
    <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="setMachineType(result.name)">
              {{ result.name }}
            </button>
          </li>
        </ul>
      </div>
      <div class="dropdown-loading">
        <loading-icon />
      </div>
    </div>
  </div>
</template>