Commit 2ea263ea authored by Dennis Tang's avatar Dennis Tang

refactoring!

parent 28a45b02
<script> <script>
import _ from 'underscore';
import Flash from '~/flash'; import Flash from '~/flash';
import { s__ } from '~/locale'; import { s__, sprintf } from '~/locale';
import { mapActions } from 'vuex'; import { mapState, mapGetters, mapActions } from 'vuex';
import Icon from '~/vue_shared/components/icon.vue'; import Icon from '~/vue_shared/components/icon.vue';
import LoadingIcon from '~/vue_shared/components/loading_icon.vue'; import LoadingIcon from '~/vue_shared/components/loading_icon.vue';
import DropdownSearchInput from '~/vue_shared/components/dropdown/dropdown_search_input.vue'; import DropdownSearchInput from '~/vue_shared/components/dropdown/dropdown_search_input.vue';
...@@ -10,7 +11,6 @@ import DropdownHiddenInput from '~/vue_shared/components/dropdown/dropdown_hidde ...@@ -10,7 +11,6 @@ import DropdownHiddenInput from '~/vue_shared/components/dropdown/dropdown_hidde
import eventHub from '../eventhub'; import eventHub from '../eventhub';
import store from '../stores'; import store from '../stores';
import DropdownButton from './dropdown_button.vue'; import DropdownButton from './dropdown_button.vue';
// TODO: Fall back to default us-central1-a or first option
export default { export default {
name: 'GkeMachineTypeDropdown', name: 'GkeMachineTypeDropdown',
...@@ -35,40 +35,43 @@ export default { ...@@ -35,40 +35,43 @@ export default {
type: String, type: String,
required: true, required: true,
}, },
inputValue: {
type: String,
required: false,
default: '',
},
}, },
data() { data() {
return { return {
isLoading: false, isLoading: false,
hasErrors: false, hasErrors: false,
searchQuery: '', searchQuery: '',
selectedItem: '',
items: [], items: [],
}; };
}, },
computed: { computed: {
...mapState(['selectedProject', 'selectedZone', 'selectedMachineType']),
...mapGetters(['hasProject', 'hasZone']),
isDisabled() { isDisabled() {
return ( return !this.selectedProject || !this.selectedZone;
this.$store.state.selectedProject.length === 0 ||
this.$store.state.selectedZone.length === 0
);
}, },
results() { searchResults() {
return this.items.filter(item => item.name.toLowerCase().indexOf(this.searchQuery) > -1); return this.items.filter(item => item.name.toLowerCase().indexOf(this.searchQuery) > -1);
}, },
toggleText() { toggleText() {
if (this.$store.state.selectedMachineType) {
return this.$store.state.selectedMachineType;
}
if (this.isLoading) { if (this.isLoading) {
return s__('ClusterIntegration|Fetching machine types'); return s__('ClusterIntegration|Fetching machine types');
} }
if (!this.$store.state.selectedProject) { if (this.selectedMachineType) {
return this.selectedMachineType;
}
if (!this.hasProject && !this.hasZone) {
return s__('ClusterIntegration|Select project and zone to choose machine type.'); return s__('ClusterIntegration|Select project and zone to choose machine type.');
} }
return this.$store.state.selectedZone return this.hasZone
? s__('ClusterIntegration|Select machine type') ? s__('ClusterIntegration|Select machine type')
: s__('ClusterIntegration|Select zone to choose machine type'); : s__('ClusterIntegration|Select zone to choose machine type');
}, },
...@@ -85,34 +88,22 @@ export default { ...@@ -85,34 +88,22 @@ export default {
fetchItems() { fetchItems() {
this.isLoading = true; this.isLoading = true;
const request = this.service.machineTypes.list({ const request = this.service.machineTypes.list({
project: this.$store.state.selectedProject.projectId, project: this.selectedProject.projectId,
zone: this.$store.state.selectedZone, zone: this.selectedZone,
}); });
return request.then( return request.then(
resp => { resp => {
let machineTypeToSelect;
this.items = resp.result.items; this.items = resp.result.items;
// Cause error if (this.inputValue) {
// this.items = data; machineTypeToSelect = _.find(this.items, item => item.name === this.inputValue);
this.setMachineType(machineTypeToSelect.name);
// Single state
// this.items = [
// {
// create_time: '2018-01-16T15:55:02.992Z',
// lifecycle_state: 'ACTIVE',
// name: 'NaturalInterface',
// item_id: 'naturalinterface-192315',
// item_number: 840816084083,
// },
// ];
if (this.items.length === 1) {
this.isDisabled = true;
this.setMachineType(this.items[0].name);
} }
this.isLoading = false; this.isLoading = false;
this.hasErrors = false;
}, },
() => { () => {
this.isLoading = false; this.isLoading = false;
...@@ -120,9 +111,10 @@ export default { ...@@ -120,9 +111,10 @@ export default {
if (resp.result.error) { if (resp.result.error) {
Flash( Flash(
`${s__( sprintf(
'ClusterIntegration|An error occured while trying to fetch zone machine types:', 'ClusterIntegration|An error occured while trying to fetch zone machine types: %{error}',
)} ${resp.result.error.message}`, { error: resp.result.error.message },
),
); );
} }
}, },
...@@ -130,7 +122,7 @@ export default { ...@@ -130,7 +122,7 @@ export default {
); );
}, },
enableSubmit() { enableSubmit() {
document.querySelector('input[type=submit]').removeAttribute('disabled'); document.querySelector('.js-gke-cluster-creation-submit').removeAttribute('disabled');
}, },
}, },
}; };
...@@ -143,7 +135,7 @@ export default { ...@@ -143,7 +135,7 @@ export default {
> >
<dropdown-hidden-input <dropdown-hidden-input
:name="fieldName" :name="fieldName"
:value="$store.state.selectedMachineType" :value="selectedMachineType"
/> />
<dropdown-button <dropdown-button
:class="{ 'gl-field-error-outline': hasErrors }" :class="{ 'gl-field-error-outline': hasErrors }"
...@@ -159,7 +151,7 @@ export default { ...@@ -159,7 +151,7 @@ export default {
<div class="dropdown-content"> <div class="dropdown-content">
<ul> <ul>
<li <li
v-for="result in results" v-for="result in searchResults"
:key="result.id" :key="result.id"
> >
<a <a
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import _ from 'underscore'; import _ from 'underscore';
import Flash from '~/flash'; import Flash from '~/flash';
import { s__, sprintf } from '~/locale'; import { s__, sprintf } from '~/locale';
import { mapActions } from 'vuex'; import { mapState, mapGetters, mapActions } from 'vuex';
import Icon from '~/vue_shared/components/icon.vue'; import Icon from '~/vue_shared/components/icon.vue';
import LoadingIcon from '~/vue_shared/components/loading_icon.vue'; import LoadingIcon from '~/vue_shared/components/loading_icon.vue';
import DropdownSearchInput from '~/vue_shared/components/dropdown/dropdown_search_input.vue'; import DropdownSearchInput from '~/vue_shared/components/dropdown/dropdown_search_input.vue';
...@@ -39,32 +39,38 @@ export default { ...@@ -39,32 +39,38 @@ export default {
type: String, type: String,
required: true, required: true,
}, },
inputValue: {
type: String,
required: false,
default: '',
},
}, },
data() { data() {
return { return {
isLoading: true, isLoading: true,
hasErrors: false, hasErrors: false,
searchQuery: '', searchQuery: '',
selectedItem: '',
items: [], items: [],
}; };
}, },
computed: { computed: {
...mapState(['selectedProject']),
...mapGetters(['hasProject']),
isDisabled() { isDisabled() {
return this.items.length < 2; return this.items.length < 2;
}, },
results() { searchResults() {
return this.items.filter(item => item.name.toLowerCase().indexOf(this.searchQuery) > -1); return this.items.filter(item => item.name.toLowerCase().indexOf(this.searchQuery) > -1);
}, },
toggleText() { toggleText() {
if (this.$store.state.selectedProject.name) {
return this.$store.state.selectedProject.name;
}
if (this.isLoading) { if (this.isLoading) {
return s__('ClusterIntegration|Fetching projects'); return s__('ClusterIntegration|Fetching projects');
} }
if (this.hasProject) {
return this.selectedProject.name;
}
return this.items.length return this.items.length
? s__('ClusterIntegration|Select project') ? s__('ClusterIntegration|Select project')
: s__('ClusterIntegration|No projects found'); : s__('ClusterIntegration|No projects found');
...@@ -105,24 +111,34 @@ export default { ...@@ -105,24 +111,34 @@ export default {
return request.then( return request.then(
resp => { resp => {
let projectToSelect;
this.items = resp.result.projects; this.items = resp.result.projects;
this.isLoading = false; if (this.inputValue) {
if (this.items.length === 1) { projectToSelect = _.find(this.items, item => item.projectId === this.inputValue);
this.setProject(this.items[0]); this.setProject(projectToSelect);
} else if (this.items.length === 1) {
projectToSelect = this.items[0];
this.setProject(projectToSelect);
} }
this.isLoading = false;
this.hasErrors = false;
}, },
resp => { resp => {
this.isLoading = false;
this.hasErrors = true;
if (resp.result.error) { if (resp.result.error) {
Flash( Flash(
`${s__('ClusterIntegration|An error occured while trying to fetch your projects:')} ${ sprintf(
resp.result.error.message 'ClusterIntegration|An error occured while trying to fetch your projects: %{error}',
}`, {
error: resp.result.error.message,
},
),
); );
} }
this.isLoading = false;
this.hasErrors = true;
}, },
this, this,
); );
...@@ -139,7 +155,7 @@ export default { ...@@ -139,7 +155,7 @@ export default {
> >
<dropdown-hidden-input <dropdown-hidden-input
:name="fieldName" :name="fieldName"
:value="$store.state.selectedProject.projectId" :value="selectedProject.projectId"
/> />
<dropdown-button <dropdown-button
:class="{ 'gl-field-error-outline': hasErrors }" :class="{ 'gl-field-error-outline': hasErrors }"
...@@ -155,7 +171,7 @@ export default { ...@@ -155,7 +171,7 @@ export default {
<div class="dropdown-content"> <div class="dropdown-content">
<ul> <ul>
<li <li
v-for="result in results" v-for="result in searchResults"
:key="result.project_number" :key="result.project_number"
> >
<a <a
......
<script> <script>
import _ from 'underscore';
import Flash from '~/flash'; import Flash from '~/flash';
import { s__ } from '~/locale'; import { s__, sprintf } from '~/locale';
import { mapActions } from 'vuex'; import { mapState, mapGetters, mapActions } from 'vuex';
import Icon from '~/vue_shared/components/icon.vue'; import Icon from '~/vue_shared/components/icon.vue';
import LoadingIcon from '~/vue_shared/components/loading_icon.vue'; import LoadingIcon from '~/vue_shared/components/loading_icon.vue';
import DropdownSearchInput from '~/vue_shared/components/dropdown/dropdown_search_input.vue'; import DropdownSearchInput from '~/vue_shared/components/dropdown/dropdown_search_input.vue';
...@@ -10,7 +11,6 @@ import DropdownHiddenInput from '~/vue_shared/components/dropdown/dropdown_hidde ...@@ -10,7 +11,6 @@ import DropdownHiddenInput from '~/vue_shared/components/dropdown/dropdown_hidde
import eventHub from '../eventhub'; import eventHub from '../eventhub';
import store from '../stores'; import store from '../stores';
import DropdownButton from './dropdown_button.vue'; import DropdownButton from './dropdown_button.vue';
// TODO: Fall back to default us-central1-a or first option
export default { export default {
name: 'GkeZoneDropdown', name: 'GkeZoneDropdown',
...@@ -35,33 +35,39 @@ export default { ...@@ -35,33 +35,39 @@ export default {
type: String, type: String,
required: true, required: true,
}, },
inputValue: {
type: String,
required: false,
default: '',
},
}, },
data() { data() {
return { return {
isLoading: false, isLoading: false,
hasErrors: false, hasErrors: false,
searchQuery: '', searchQuery: '',
selectedItem: '',
items: [], items: [],
}; };
}, },
computed: { computed: {
...mapState(['selectedProject', 'selectedZone']),
...mapGetters(['hasProject']),
isDisabled() { isDisabled() {
return this.$store.state.selectedProject.projectId.length === 0; return !this.hasProject;
}, },
results() { searchResults() {
return this.items.filter(item => item.name.toLowerCase().indexOf(this.searchQuery) > -1); return this.items.filter(item => item.name.toLowerCase().indexOf(this.searchQuery) > -1);
}, },
toggleText() { toggleText() {
if (this.$store.state.selectedZone) {
return this.$store.state.selectedZone;
}
if (this.isLoading) { if (this.isLoading) {
return s__('ClusterIntegration|Fetching zones'); return s__('ClusterIntegration|Fetching zones');
} }
return this.$store.state.selectedProject if (this.selectedZone) {
return this.selectedZone;
}
return this.$store.state.selectedProject.projectId.length
? s__('ClusterIntegration|Select zone') ? s__('ClusterIntegration|Select zone')
: s__('ClusterIntegration|Select project to choose zone'); : s__('ClusterIntegration|Select project to choose zone');
}, },
...@@ -77,33 +83,21 @@ export default { ...@@ -77,33 +83,21 @@ export default {
fetchItems() { fetchItems() {
this.isLoading = true; this.isLoading = true;
const request = this.service.zones.list({ const request = this.service.zones.list({
project: this.$store.state.selectedProject.projectId, project: this.selectedProject.projectId,
}); });
return request.then( return request.then(
resp => { resp => {
let zoneToSelect;
this.items = resp.result.items; this.items = resp.result.items;
// Cause error if (this.inputValue) {
// this.items = data; zoneToSelect = _.find(this.items, item => item.name === this.inputValue);
this.setZone(zoneToSelect.name);
// Single state
// this.items = [
// {
// create_time: '2018-01-16T15:55:02.992Z',
// lifecycle_state: 'ACTIVE',
// name: 'NaturalInterface',
// item_id: 'naturalinterface-192315',
// item_number: 840816084083,
// },
// ];
if (this.items.length === 1) {
this.isDisabled = true;
this.setZone(this.items[0].name);
} }
this.isLoading = false; this.isLoading = false;
this.hasErrors = false;
}, },
resp => { resp => {
this.isLoading = false; this.isLoading = false;
...@@ -111,9 +105,10 @@ export default { ...@@ -111,9 +105,10 @@ export default {
if (resp.result.error) { if (resp.result.error) {
Flash( Flash(
`${s__('ClusterIntegration|An error occured while trying to fetch project zones:')} ${ sprintf(
resp.result.error.message 'ClusterIntegration|An error occured while trying to fetch project zones: %{error}',
}`, { error: resp.result.error.message },
),
); );
} }
}, },
...@@ -131,7 +126,7 @@ export default { ...@@ -131,7 +126,7 @@ export default {
> >
<dropdown-hidden-input <dropdown-hidden-input
:name="fieldName" :name="fieldName"
:value="$store.state.selectedZone" :value="selectedZone"
/> />
<dropdown-button <dropdown-button
:class="{ 'gl-field-error-outline': hasErrors }" :class="{ 'gl-field-error-outline': hasErrors }"
...@@ -147,7 +142,7 @@ export default { ...@@ -147,7 +142,7 @@ export default {
<div class="dropdown-content"> <div class="dropdown-content">
<ul> <ul>
<li <li
v-for="result in results" v-for="result in searchResults"
:key="result.id" :key="result.id"
> >
<a <a
......
...@@ -24,6 +24,7 @@ const mountGkeProjectIdDropdown = () => { ...@@ -24,6 +24,7 @@ const mountGkeProjectIdDropdown = () => {
service: gapi.client.cloudresourcemanager, service: gapi.client.cloudresourcemanager,
fieldName: hiddenInput.getAttribute('name'), fieldName: hiddenInput.getAttribute('name'),
fieldId: hiddenInput.getAttribute('id'), fieldId: hiddenInput.getAttribute('id'),
inputValue: hiddenInput.value,
}, },
}), }),
}); });
...@@ -46,6 +47,7 @@ const mountGkeZoneDropdown = () => { ...@@ -46,6 +47,7 @@ const mountGkeZoneDropdown = () => {
service: gapi.client.compute, service: gapi.client.compute,
fieldName: hiddenInput.getAttribute('name'), fieldName: hiddenInput.getAttribute('name'),
fieldId: hiddenInput.getAttribute('id'), fieldId: hiddenInput.getAttribute('id'),
inputValue: hiddenInput.value,
}, },
}), }),
}); });
...@@ -68,6 +70,7 @@ const mountGkeMachineTypeDropdown = () => { ...@@ -68,6 +70,7 @@ const mountGkeMachineTypeDropdown = () => {
service: gapi.client.compute, service: gapi.client.compute,
fieldName: hiddenInput.getAttribute('name'), fieldName: hiddenInput.getAttribute('name'),
fieldId: hiddenInput.getAttribute('id'), fieldId: hiddenInput.getAttribute('id'),
inputValue: hiddenInput.value,
}, },
}), }),
}); });
...@@ -78,9 +81,10 @@ const gkeDropdownErrorHandler = () => { ...@@ -78,9 +81,10 @@ const gkeDropdownErrorHandler = () => {
}; };
const initializeGapiClient = () => { const initializeGapiClient = () => {
const el = document.getElementById('new_cluster'); const el = document.querySelector('.js-gke-cluster-creation');
gapi.client.setToken({ access_token: el.dataset.token }); gapi.client.setToken({ access_token: el.dataset.token });
delete el.dataset.token;
gapi.client gapi.client
.load(CONSTANTS.GCP_API_CLOUD_RESOURCE_MANAGER_ENDPOINT) .load(CONSTANTS.GCP_API_CLOUD_RESOURCE_MANAGER_ENDPOINT)
......
export const hasProject = state => !!state.selectedProject.projectId;
export const hasZone = state => !!state.selectedZone;
export const hasMachineType = state => !!state.selectedMachineType;
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
} }
} }
#new_cluster { .js-gke-cluster-creation {
.dropdown-menu-toggle { .dropdown-menu-toggle {
.loading-container { .loading-container {
.fa { .fa {
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
- link_to_help_page = link_to(s_('ClusterIntegration|help page'), help_page_path('user/project/clusters/index'), target: '_blank', rel: 'noopener noreferrer') - link_to_help_page = link_to(s_('ClusterIntegration|help page'), help_page_path('user/project/clusters/index'), target: '_blank', rel: 'noopener noreferrer')
= s_('ClusterIntegration|Read our %{link_to_help_page} on Kubernetes cluster integration.').html_safe % { link_to_help_page: link_to_help_page} = s_('ClusterIntegration|Read our %{link_to_help_page} on Kubernetes cluster integration.').html_safe % { link_to_help_page: link_to_help_page}
= form_for @cluster, html: { class: 'prepend-top-20', data: { token: @token_in_session } }, url: gcp_namespace_project_clusters_path(@project.namespace, @project), as: :cluster do |field| = form_for @cluster, html: { class: 'js-gke-cluster-creation prepend-top-20', data: { token: @token_in_session } }, url: gcp_namespace_project_clusters_path(@project.namespace, @project), as: :cluster do |field|
= form_errors(@cluster) = form_errors(@cluster)
.form-group .form-group
= field.label :name, s_('ClusterIntegration|Kubernetes cluster name') = field.label :name, s_('ClusterIntegration|Kubernetes cluster name')
...@@ -51,4 +51,4 @@ ...@@ -51,4 +51,4 @@
= icon('chevron-down') = icon('chevron-down')
.form-group .form-group
= field.submit s_('ClusterIntegration|Create Kubernetes cluster'), class: 'btn btn-success', disabled: true = field.submit s_('ClusterIntegration|Create Kubernetes cluster'), class: 'js-gke-cluster-creation-submit btn btn-success', disabled: true
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment