2018-01-29 13:00:15 +00:00
|
|
|
import axios from '../lib/utils/axios_utils';
|
|
|
|
import flash from '../flash';
|
2017-05-25 08:47:43 +00:00
|
|
|
|
|
|
|
export default class IntegrationSettingsForm {
|
|
|
|
constructor(formSelector) {
|
|
|
|
this.$form = $(formSelector);
|
|
|
|
|
|
|
|
// Form Metadata
|
2017-05-29 06:46:34 +00:00
|
|
|
this.canTestService = this.$form.data('can-test');
|
2017-05-31 07:17:07 +00:00
|
|
|
this.testEndPoint = this.$form.data('test-url');
|
2017-05-25 08:47:43 +00:00
|
|
|
|
|
|
|
// Form Child Elements
|
|
|
|
this.$serviceToggle = this.$form.find('#service_active');
|
|
|
|
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() {
|
|
|
|
// Initialize View
|
2017-05-25 09:35:19 +00:00
|
|
|
this.toggleServiceState(this.$serviceToggle.is(':checked'));
|
2017-05-25 08:47:43 +00:00
|
|
|
|
|
|
|
// Bind Event Listeners
|
2017-05-31 07:17:07 +00:00
|
|
|
this.$serviceToggle.on('change', e => this.handleServiceToggle(e));
|
|
|
|
this.$submitBtn.on('click', e => this.handleSettingsSave(e));
|
2017-05-25 08:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSettingsSave(e) {
|
2017-05-31 07:17:07 +00:00
|
|
|
// 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
|
|
|
|
if (!this.$serviceToggle.is(':checked')) {
|
|
|
|
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.
|
|
|
|
if (this.$form.get(0).checkValidity() && this.canTestService) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.testSettings(this.$form.serialize());
|
2017-05-25 08:47:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleServiceToggle(e) {
|
2017-05-25 09:35:19 +00:00
|
|
|
this.toggleServiceState($(e.currentTarget).is(':checked'));
|
|
|
|
}
|
|
|
|
|
2017-05-30 11:28:09 +00:00
|
|
|
/**
|
|
|
|
* Change Form's validation enforcement based on service status (active/inactive)
|
|
|
|
*/
|
2017-05-25 09:35:19 +00:00
|
|
|
toggleServiceState(serviceActive) {
|
2017-05-31 07:17:07 +00:00
|
|
|
this.toggleSubmitBtnLabel(serviceActive);
|
2017-05-25 09:35:19 +00:00
|
|
|
if (serviceActive) {
|
|
|
|
this.$form.removeAttr('novalidate');
|
|
|
|
} else if (!this.$form.attr('novalidate')) {
|
|
|
|
this.$form.attr('novalidate', 'novalidate');
|
|
|
|
}
|
2017-05-25 08:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-30 11:28:09 +00:00
|
|
|
* Toggle Submit button label based on Integration status and ability to test service
|
2017-05-25 08:47:43 +00:00
|
|
|
*/
|
2017-05-31 07:17:07 +00:00
|
|
|
toggleSubmitBtnLabel(serviceActive) {
|
|
|
|
let btnLabel = 'Save changes';
|
|
|
|
|
|
|
|
if (serviceActive && this.canTestService) {
|
|
|
|
btnLabel = 'Test settings and save changes';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$submitBtnLabel.text(btnLabel);
|
2017-05-25 08:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
2017-05-31 07:17:07 +00:00
|
|
|
this.$submitBtnLoader.addClass('hidden');
|
2017-05-25 08:47:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* eslint-disable promise/catch-or-return, no-new */
|
|
|
|
/**
|
|
|
|
* Test Integration config
|
|
|
|
*/
|
|
|
|
testSettings(formData) {
|
|
|
|
this.toggleSubmitBtnState(true);
|
2018-01-29 13:00:15 +00:00
|
|
|
|
|
|
|
return axios.put(this.testEndPoint, formData)
|
|
|
|
.then(({ data }) => {
|
|
|
|
if (data.error) {
|
|
|
|
flash(`${data.message} ${data.service_response}`, 'alert', document, {
|
|
|
|
title: 'Save anyway',
|
|
|
|
clickHandler: (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
this.$form.submit();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.$form.submit();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.toggleSubmitBtnState(false);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
flash('Something went wrong on our end.');
|
|
|
|
this.toggleSubmitBtnState(false);
|
|
|
|
});
|
2017-05-25 08:47:43 +00:00
|
|
|
}
|
|
|
|
}
|