2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2018-01-29 08:00:15 -05:00
|
|
|
import axios from '../lib/utils/axios_utils';
|
2020-09-15 14:09:43 -04:00
|
|
|
import { __, s__ } from '~/locale';
|
|
|
|
import toast from '~/vue_shared/plugins/global_toast';
|
2020-04-15 08:09:18 -04:00
|
|
|
import initForm from './edit';
|
|
|
|
import eventHub from './edit/event_hub';
|
2017-05-25 04:47:43 -04:00
|
|
|
|
|
|
|
export default class IntegrationSettingsForm {
|
|
|
|
constructor(formSelector) {
|
|
|
|
this.$form = $(formSelector);
|
2020-04-15 08:09:18 -04:00
|
|
|
this.formActive = false;
|
2017-05-25 04:47:43 -04:00
|
|
|
|
2020-09-15 14:09:43 -04:00
|
|
|
this.vue = null;
|
|
|
|
|
2017-05-25 04:47:43 -04:00
|
|
|
// Form Metadata
|
2018-02-20 17:20:48 -05:00
|
|
|
this.testEndPoint = this.$form.data('testUrl');
|
2017-05-25 04:47:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
2020-04-15 08:09:18 -04:00
|
|
|
// Init Vue component
|
2020-09-15 14:09:43 -04:00
|
|
|
this.vue = initForm(
|
2020-07-16 23:09:14 -04:00
|
|
|
document.querySelector('.js-vue-integration-settings'),
|
2020-09-17 05:09:32 -04:00
|
|
|
document.querySelector('.js-vue-default-integration-settings'),
|
2020-07-16 23:09:14 -04:00
|
|
|
);
|
2020-04-15 08:09:18 -04:00
|
|
|
eventHub.$on('toggle', active => {
|
|
|
|
this.formActive = active;
|
2020-09-15 14:09:43 -04:00
|
|
|
this.toggleServiceState();
|
|
|
|
});
|
|
|
|
eventHub.$on('testIntegration', () => {
|
|
|
|
this.testIntegration();
|
|
|
|
});
|
|
|
|
eventHub.$on('saveIntegration', () => {
|
|
|
|
this.saveIntegration();
|
2020-04-15 08:09:18 -04:00
|
|
|
});
|
2017-05-25 04:47:43 -04:00
|
|
|
}
|
|
|
|
|
2020-09-15 14:09:43 -04:00
|
|
|
saveIntegration() {
|
|
|
|
// Service was marked active so now we check;
|
|
|
|
// 1) If form contents are valid
|
|
|
|
// 2) If this service can be saved
|
|
|
|
// If both conditions are true, we override form submission
|
|
|
|
// and save the service using provided configuration.
|
|
|
|
if (this.$form.get(0).checkValidity()) {
|
|
|
|
this.$form.submit();
|
|
|
|
} else {
|
|
|
|
eventHub.$emit('validateForm');
|
|
|
|
this.vue.$store.dispatch('setIsSaving', false);
|
2017-05-31 03:17:07 -04:00
|
|
|
}
|
2020-09-15 14:09:43 -04:00
|
|
|
}
|
2017-05-31 03:17:07 -04:00
|
|
|
|
2020-09-15 14:09:43 -04:00
|
|
|
testIntegration() {
|
2017-05-31 03:17:07 -04:00
|
|
|
// 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-05 14:08:19 -04:00
|
|
|
if (this.$form.get(0).checkValidity()) {
|
2020-09-15 14:09:43 -04:00
|
|
|
// eslint-disable-next-line no-jquery/no-serialize
|
|
|
|
this.testSettings(this.$form.serialize());
|
2020-06-05 14:08:19 -04:00
|
|
|
} else {
|
|
|
|
eventHub.$emit('validateForm');
|
2020-09-15 14:09:43 -04:00
|
|
|
this.vue.$store.dispatch('setIsTesting', false);
|
2017-05-25 04:47:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 07:28:09 -04:00
|
|
|
/**
|
|
|
|
* Change Form's validation enforcement based on service status (active/inactive)
|
|
|
|
*/
|
2020-04-15 08:09:18 -04:00
|
|
|
toggleServiceState() {
|
|
|
|
if (this.formActive) {
|
2017-05-25 05:35:19 -04:00
|
|
|
this.$form.removeAttr('novalidate');
|
|
|
|
} else if (!this.$form.attr('novalidate')) {
|
|
|
|
this.$form.attr('novalidate', 'novalidate');
|
|
|
|
}
|
2017-05-25 04:47:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test Integration config
|
|
|
|
*/
|
|
|
|
testSettings(formData) {
|
2018-10-24 15:17:03 -04:00
|
|
|
return axios
|
|
|
|
.put(this.testEndPoint, formData)
|
2018-01-29 08:00:15 -05:00
|
|
|
.then(({ data }) => {
|
|
|
|
if (data.error) {
|
2020-09-15 14:09:43 -04:00
|
|
|
toast(`${data.message} ${data.service_response}`);
|
2018-01-29 08:00:15 -05:00
|
|
|
} else {
|
2020-09-15 14:09:43 -04:00
|
|
|
toast(s__('Integrations|Connection successful.'));
|
2018-01-29 08:00:15 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {
|
2020-09-15 14:09:43 -04:00
|
|
|
toast(__('Something went wrong on our end.'));
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
this.vue.$store.dispatch('setIsTesting', false);
|
2018-01-29 08:00:15 -05:00
|
|
|
});
|
2017-05-25 04:47:43 -04:00
|
|
|
}
|
|
|
|
}
|