debian-mirror-gitlab/app/assets/javascripts/integrations/integration_settings_form.js

141 lines
3.8 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-03-17 18:26:18 +05:30
import axios from '../lib/utils/axios_utils';
import flash from '../flash';
2019-07-31 22:56:46 +05:30
import { __ } from '~/locale';
2020-04-22 19:07:51 +05:30
import initForm from './edit';
import eventHub from './edit/event_hub';
2017-09-10 17:25:29 +05:30
export default class IntegrationSettingsForm {
constructor(formSelector) {
this.$form = $(formSelector);
2020-04-22 19:07:51 +05:30
this.formActive = false;
2017-09-10 17:25:29 +05:30
// Form Metadata
2018-03-27 19:54:05 +05:30
this.canTestService = this.$form.data('canTest');
this.testEndPoint = this.$form.data('testUrl');
2017-09-10 17:25:29 +05:30
// Form Child Elements
this.$submitBtn = this.$form.find('button[type="submit"]');
this.$submitBtnLoader = this.$submitBtn.find('.js-btn-spinner');
this.$submitBtnLabel = this.$submitBtn.find('.js-btn-label');
}
init() {
2020-04-22 19:07:51 +05:30
// Init Vue component
initForm(document.querySelector('.js-vue-integration-settings'));
eventHub.$on('toggle', active => {
this.formActive = active;
this.handleServiceToggle();
});
2017-09-10 17:25:29 +05:30
// Bind Event Listeners
this.$submitBtn.on('click', e => this.handleSettingsSave(e));
}
handleSettingsSave(e) {
// Check if Service is marked active, as if not marked active,
// We can skip testing it and directly go ahead to allow form to
// be submitted
2020-04-22 19:07:51 +05:30
if (!this.formActive) {
2017-09-10 17:25:29 +05:30
return;
}
// Service was marked active so now we check;
// 1) If form contents are valid
// 2) If this service can be tested
// If both conditions are true, we override form submission
// and test the service using provided configuration.
2020-06-23 00:09:42 +05:30
if (this.$form.get(0).checkValidity()) {
if (this.canTestService) {
e.preventDefault();
// eslint-disable-next-line no-jquery/no-serialize
this.testSettings(this.$form.serialize());
}
} else {
2017-09-10 17:25:29 +05:30
e.preventDefault();
2020-06-23 00:09:42 +05:30
eventHub.$emit('validateForm');
2017-09-10 17:25:29 +05:30
}
}
2020-04-22 19:07:51 +05:30
handleServiceToggle() {
this.toggleServiceState();
2017-09-10 17:25:29 +05:30
}
/**
* Change Form's validation enforcement based on service status (active/inactive)
*/
2020-04-22 19:07:51 +05:30
toggleServiceState() {
this.toggleSubmitBtnLabel();
if (this.formActive) {
2017-09-10 17:25:29 +05:30
this.$form.removeAttr('novalidate');
} else if (!this.$form.attr('novalidate')) {
this.$form.attr('novalidate', 'novalidate');
}
}
/**
* Toggle Submit button label based on Integration status and ability to test service
*/
2020-04-22 19:07:51 +05:30
toggleSubmitBtnLabel() {
2019-07-31 22:56:46 +05:30
let btnLabel = __('Save changes');
2017-09-10 17:25:29 +05:30
2020-04-22 19:07:51 +05:30
if (this.formActive && this.canTestService) {
2019-07-31 22:56:46 +05:30
btnLabel = __('Test settings and save changes');
2017-09-10 17:25:29 +05:30
}
this.$submitBtnLabel.text(btnLabel);
}
/**
* Toggle Submit button state based on provided boolean value of `saveTestActive`
* When enabled, it does two things, and reverts back when disabled
*
* 1. It shows load spinner on submit button
* 2. Makes submit button disabled
*/
toggleSubmitBtnState(saveTestActive) {
if (saveTestActive) {
this.$submitBtn.disable();
this.$submitBtnLoader.removeClass('hidden');
} else {
this.$submitBtn.enable();
this.$submitBtnLoader.addClass('hidden');
}
}
/**
* Test Integration config
*/
testSettings(formData) {
this.toggleSubmitBtnState(true);
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
return axios
.put(this.testEndPoint, formData)
2018-03-17 18:26:18 +05:30
.then(({ data }) => {
if (data.error) {
2018-11-08 19:23:39 +05:30
let flashActions;
if (data.test_failed) {
flashActions = {
2019-07-31 22:56:46 +05:30
title: __('Save anyway'),
2018-12-13 13:39:08 +05:30
clickHandler: e => {
2018-11-08 19:23:39 +05:30
e.preventDefault();
this.$form.submit();
},
};
}
flash(`${data.message} ${data.service_response}`, 'alert', document, flashActions);
2018-03-17 18:26:18 +05:30
} else {
this.$form.submit();
}
this.toggleSubmitBtnState(false);
})
.catch(() => {
2019-07-31 22:56:46 +05:30
flash(__('Something went wrong on our end.'));
2018-03-17 18:26:18 +05:30
this.toggleSubmitBtnState(false);
});
2017-09-10 17:25:29 +05:30
}
}