2020-12-15 15:49:07 +01:00
|
|
|
/** AJAXApi class providing helpers to perform AJAX request */
|
2020-12-10 15:18:02 +01:00
|
|
|
class AJAXApi {
|
2020-12-15 10:40:49 +01:00
|
|
|
static genericRequestHeaders = {
|
2020-12-10 15:18:02 +01:00
|
|
|
'X-Requested-With': 'XMLHttpRequest'
|
2020-12-15 10:40:49 +01:00
|
|
|
};
|
2020-12-10 15:18:02 +01:00
|
|
|
static genericRequestConfigGET = {
|
2020-12-15 10:40:49 +01:00
|
|
|
headers: new Headers(Object.assign({}, AJAXApi.genericRequestHeaders))
|
2020-12-10 15:18:02 +01:00
|
|
|
}
|
|
|
|
static genericRequestConfigPOST = {
|
2020-12-15 10:40:49 +01:00
|
|
|
headers: new Headers(Object.assign({}, AJAXApi.genericRequestHeaders)),
|
2020-12-10 15:18:02 +01:00
|
|
|
redirect: 'manual',
|
|
|
|
method: 'POST',
|
|
|
|
}
|
2021-01-13 14:19:22 +01:00
|
|
|
static genericRequestConfigGETJSON = {
|
|
|
|
headers: new Headers(Object.assign({}, AJAXApi.genericRequestHeaders, {Accept: 'application/json'}))
|
|
|
|
}
|
2020-12-10 15:18:02 +01:00
|
|
|
|
2020-12-15 15:49:07 +01:00
|
|
|
/**
|
|
|
|
* @namespace
|
2021-01-18 09:48:12 +01:00
|
|
|
* @property {boolean} provideFeedback - Should a toast be used to provide feedback upon request fulfillment
|
|
|
|
* @property {(jQuery|string)} statusNode - The node on which the loading overlay should be placed (OverlayFactory.node)
|
|
|
|
* @property {Object} statusNodeOverlayConfig - The configuration (OverlayFactory.options) of the overlay applied on the status node
|
|
|
|
* @property {Object} errorToastOptions - The options supported by Toaster#defaultOptions
|
|
|
|
* @property {Object} successToastOptions - The options supported by Toaster#defaultOptions
|
2020-12-15 15:49:07 +01:00
|
|
|
*/
|
2020-12-10 15:18:02 +01:00
|
|
|
static defaultOptions = {
|
2020-12-11 10:38:43 +01:00
|
|
|
provideFeedback: true,
|
|
|
|
statusNode: false,
|
2021-01-18 09:48:12 +01:00
|
|
|
statusNodeOverlayConfig: {},
|
2021-01-13 14:19:22 +01:00
|
|
|
errorToastOptions: {
|
2020-12-15 10:40:49 +01:00
|
|
|
delay: 10000
|
2021-01-13 14:19:22 +01:00
|
|
|
},
|
|
|
|
successToastOptions: {
|
|
|
|
},
|
2020-12-10 15:18:02 +01:00
|
|
|
}
|
|
|
|
options = {}
|
|
|
|
loadingOverlay = false
|
|
|
|
|
2020-12-15 15:49:07 +01:00
|
|
|
/**
|
|
|
|
* Instantiate an AJAXApi object.
|
|
|
|
* @param {Object} options - The options supported by AJAXApi#defaultOptions
|
|
|
|
*/
|
2020-12-10 15:18:02 +01:00
|
|
|
constructor(options) {
|
|
|
|
this.mergeOptions(AJAXApi.defaultOptions)
|
|
|
|
this.mergeOptions(options)
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:49:07 +01:00
|
|
|
/**
|
|
|
|
* Based on the current configuration, provide feedback to the user via toast, console or do not
|
|
|
|
* @param {Object} toastOptions - The options supported by Toaster#defaultOptions
|
|
|
|
* @param {boolean} isError - If true and toast feedback is disable, write the feedback in the console
|
|
|
|
* @param {boolean} skip - If true, skip the feedback regardless of the configuration
|
|
|
|
*/
|
2020-12-15 10:40:49 +01:00
|
|
|
provideFeedback(toastOptions, isError=false, skip=false) {
|
2021-01-13 14:19:22 +01:00
|
|
|
const alteredToastOptions = Object.assign(
|
|
|
|
{},
|
|
|
|
isError ? AJAXApi.defaultOptions.errorToastOptions : AJAXApi.defaultOptions.successToastOptions,
|
|
|
|
toastOptions
|
|
|
|
)
|
2020-12-15 10:40:49 +01:00
|
|
|
if (!skip) {
|
|
|
|
if (this.options.provideFeedback) {
|
|
|
|
UI.toast(alteredToastOptions)
|
|
|
|
} else {
|
|
|
|
if (isError) {
|
|
|
|
console.error(alteredToastOptions.body)
|
|
|
|
}
|
2020-12-11 10:38:43 +01:00
|
|
|
}
|
2020-12-10 15:18:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:49:07 +01:00
|
|
|
/**
|
|
|
|
* Merge newOptions configuration into the current object
|
|
|
|
* @param {Object} The options supported by AJAXApi#defaultOptions
|
|
|
|
*/
|
2020-12-10 15:18:02 +01:00
|
|
|
mergeOptions(newOptions) {
|
|
|
|
this.options = Object.assign({}, this.options, newOptions)
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:49:07 +01:00
|
|
|
/**
|
2021-06-01 07:46:51 +02:00
|
|
|
*
|
2020-12-15 15:49:07 +01:00
|
|
|
* @param {FormData} formData - The data of a form
|
|
|
|
* @param {Object} dataToMerge - Data to be merge into formData
|
|
|
|
* @return {FormData} The form data merged with the additional dataToMerge data
|
|
|
|
*/
|
2020-12-10 15:18:02 +01:00
|
|
|
static mergeFormData(formData, dataToMerge) {
|
|
|
|
for (const [fieldName, value] of Object.entries(dataToMerge)) {
|
|
|
|
formData.set(fieldName, value)
|
|
|
|
}
|
|
|
|
return formData
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:49:07 +01:00
|
|
|
/**
|
2021-01-13 14:19:22 +01:00
|
|
|
* @param {string} url - The URL to fetch
|
2021-06-01 07:46:51 +02:00
|
|
|
* @param {Object} [options={}] - The options supported by AJAXApi#defaultOptions
|
2020-12-15 15:49:07 +01:00
|
|
|
* @return {Promise<string>} Promise object resolving to the fetched HTML
|
|
|
|
*/
|
2020-12-11 10:38:43 +01:00
|
|
|
static async quickFetchURL(url, options={}) {
|
|
|
|
const constAlteredOptions = Object.assign({}, {provideFeedback: false}, options)
|
|
|
|
const tmpApi = new AJAXApi(constAlteredOptions)
|
|
|
|
return tmpApi.fetchURL(url, constAlteredOptions.skipRequestHooks)
|
|
|
|
}
|
|
|
|
|
2021-01-13 14:19:22 +01:00
|
|
|
/**
|
|
|
|
* @param {string} url - The URL to fetch
|
2021-06-01 07:46:51 +02:00
|
|
|
* @param {Object} [options={}] - The options supported by AJAXApi#defaultOptions
|
2021-01-13 14:19:22 +01:00
|
|
|
* @return {Promise<Object>} Promise object resolving to the fetched HTML
|
|
|
|
*/
|
|
|
|
static async quickFetchJSON(url, options={}) {
|
|
|
|
const constAlteredOptions = Object.assign({}, {provideFeedback: false}, options)
|
|
|
|
const tmpApi = new AJAXApi(constAlteredOptions)
|
|
|
|
return tmpApi.fetchJSON(url, constAlteredOptions.skipRequestHooks)
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:49:07 +01:00
|
|
|
/**
|
|
|
|
* @param {string} url - The URL to fetch
|
2021-06-01 07:46:51 +02:00
|
|
|
* @param {Object} [options={}] - The options supported by AJAXApi#defaultOptions
|
2020-12-15 15:49:07 +01:00
|
|
|
* @return {Promise<HTMLFormElement>} Promise object resolving to the fetched form
|
|
|
|
*/
|
2020-12-11 10:38:43 +01:00
|
|
|
static async quickFetchForm(url, options={}) {
|
|
|
|
const constAlteredOptions = Object.assign({}, {provideFeedback: false}, options)
|
|
|
|
const tmpApi = new AJAXApi(constAlteredOptions)
|
|
|
|
return tmpApi.fetchForm(url, constAlteredOptions.skipRequestHooks)
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:49:07 +01:00
|
|
|
/**
|
|
|
|
* @param {HTMLFormElement} form - The form to be posted
|
|
|
|
* @param {Object} [dataToMerge={}] - Additional data to be integrated or modified in the form
|
2021-06-01 07:46:51 +02:00
|
|
|
* @param {Object} [options={}] - The options supported by AJAXApi#defaultOptions
|
2020-12-15 15:49:07 +01:00
|
|
|
* @return {Promise<Object>} Promise object resolving to the result of the POST operation
|
|
|
|
*/
|
2020-12-15 10:40:49 +01:00
|
|
|
static async quickPostForm(form, dataToMerge={}, options={}) {
|
|
|
|
const constAlteredOptions = Object.assign({}, {}, options)
|
|
|
|
const tmpApi = new AJAXApi(constAlteredOptions)
|
|
|
|
return tmpApi.postForm(form, dataToMerge, constAlteredOptions.skipRequestHooks)
|
|
|
|
}
|
|
|
|
|
2021-03-11 09:20:03 +01:00
|
|
|
/**
|
|
|
|
* @param {string} url - The URL to on which to execute the POST
|
|
|
|
* @param {Object} [data={}] - The data to be posted
|
|
|
|
* @param {Object} [options={}] - The options supported by AJAXApi#defaultOptions
|
|
|
|
* @return {Promise<Object>} Promise object resolving to the result of the POST operation
|
|
|
|
*/
|
|
|
|
static async quickPostData(url, data={}, options={}) {
|
|
|
|
const constAlteredOptions = Object.assign({}, {}, options)
|
|
|
|
const tmpApi = new AJAXApi(constAlteredOptions)
|
|
|
|
return tmpApi.postData(url, data, constAlteredOptions.skipRequestHooks)
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:49:07 +01:00
|
|
|
/**
|
|
|
|
* @param {string} url - The URL from which to fetch the form
|
|
|
|
* @param {Object} [dataToMerge={}] - Additional data to be integrated or modified in the form
|
|
|
|
* @return {Promise<Object>} Promise object resolving to the result of the POST operation
|
|
|
|
*/
|
2020-12-11 10:38:43 +01:00
|
|
|
static async quickFetchAndPostForm(url, dataToMerge={}, options={}) {
|
|
|
|
const constAlteredOptions = Object.assign({}, {}, options)
|
|
|
|
const tmpApi = new AJAXApi(constAlteredOptions)
|
2021-02-24 11:05:23 +01:00
|
|
|
return tmpApi.fetchAndPostForm(url, dataToMerge, constAlteredOptions.skipRequestHooks, constAlteredOptions.skipFeedback)
|
2020-12-11 10:38:43 +01:00
|
|
|
}
|
|
|
|
|
2020-12-15 15:49:07 +01:00
|
|
|
/**
|
|
|
|
* @param {string} url - The URL to fetch
|
|
|
|
* @param {boolean} [skipRequestHooks=false] - If true, default request hooks will be skipped
|
|
|
|
* @param {boolean} [skipFeedback=false] - Pass this value to the AJAXApi.provideFeedback function
|
|
|
|
* @return {Promise<string>} Promise object resolving to the fetched HTML
|
|
|
|
*/
|
2020-12-15 10:40:49 +01:00
|
|
|
async fetchURL(url, skipRequestHooks=false, skipFeedback=false) {
|
2020-12-10 15:18:02 +01:00
|
|
|
if (!skipRequestHooks) {
|
|
|
|
this.beforeRequest()
|
|
|
|
}
|
|
|
|
let toReturn
|
|
|
|
try {
|
|
|
|
const response = await fetch(url, AJAXApi.genericRequestConfigGET);
|
|
|
|
if (!response.ok) {
|
2021-01-11 12:48:58 +01:00
|
|
|
throw new Error(`Network response was not ok. \`${response.statusText}\``)
|
2020-12-10 15:18:02 +01:00
|
|
|
}
|
2020-12-15 15:49:07 +01:00
|
|
|
const dataHtml = await response.text();
|
2020-12-11 10:38:43 +01:00
|
|
|
this.provideFeedback({
|
|
|
|
variant: 'success',
|
|
|
|
title: 'URL fetched',
|
2020-12-15 10:40:49 +01:00
|
|
|
}, false, skipFeedback);
|
2020-12-15 15:49:07 +01:00
|
|
|
toReturn = dataHtml;
|
2020-12-10 15:18:02 +01:00
|
|
|
} catch (error) {
|
|
|
|
this.provideFeedback({
|
|
|
|
variant: 'danger',
|
|
|
|
title: 'There has been a problem with the operation',
|
2021-01-14 12:42:05 +01:00
|
|
|
body: error.message
|
2020-12-15 10:40:49 +01:00
|
|
|
}, true, skipFeedback);
|
2020-12-10 15:18:02 +01:00
|
|
|
toReturn = Promise.reject(error);
|
|
|
|
} finally {
|
|
|
|
if (!skipRequestHooks) {
|
|
|
|
this.afterRequest()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toReturn
|
|
|
|
}
|
|
|
|
|
2021-01-13 14:19:22 +01:00
|
|
|
/**
|
|
|
|
* @param {string} url - The URL to fetch
|
|
|
|
* @param {boolean} [skipRequestHooks=false] - If true, default request hooks will be skipped
|
|
|
|
* @param {boolean} [skipFeedback=false] - Pass this value to the AJAXApi.provideFeedback function
|
|
|
|
* @return {Promise<string>} Promise object resolving to the fetched JSON
|
|
|
|
*/
|
|
|
|
async fetchJSON(url, skipRequestHooks=false, skipFeedback=false) {
|
|
|
|
if (!skipRequestHooks) {
|
|
|
|
this.beforeRequest()
|
|
|
|
}
|
|
|
|
let toReturn
|
|
|
|
try {
|
|
|
|
const response = await fetch(url, AJAXApi.genericRequestConfigGETJSON);
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Network response was not ok. \`${response.statusText}\``)
|
|
|
|
}
|
|
|
|
const dataJson = await response.json();
|
|
|
|
this.provideFeedback({
|
|
|
|
variant: 'success',
|
|
|
|
title: 'URL fetched',
|
|
|
|
}, false, skipFeedback);
|
|
|
|
toReturn = dataJson;
|
|
|
|
} catch (error) {
|
|
|
|
this.provideFeedback({
|
|
|
|
variant: 'danger',
|
|
|
|
title: 'There has been a problem with the operation',
|
2021-01-14 12:42:05 +01:00
|
|
|
body: error.message
|
2021-01-13 14:19:22 +01:00
|
|
|
}, true, skipFeedback);
|
|
|
|
toReturn = Promise.reject(error);
|
|
|
|
} finally {
|
|
|
|
if (!skipRequestHooks) {
|
|
|
|
this.afterRequest()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toReturn
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:49:07 +01:00
|
|
|
/**
|
|
|
|
* @param {string} url - The URL to fetch
|
|
|
|
* @param {boolean} [skipRequestHooks=false] - If true, default request hooks will be skipped
|
|
|
|
* @param {boolean} [skipFeedback=false] - Pass this value to the AJAXApi.provideFeedback function
|
|
|
|
* @return {Promise<HTMLFormElement>} Promise object resolving to the fetched HTML
|
|
|
|
*/
|
2020-12-15 10:40:49 +01:00
|
|
|
async fetchForm(url, skipRequestHooks=false, skipFeedback=false) {
|
2020-12-10 15:18:02 +01:00
|
|
|
if (!skipRequestHooks) {
|
|
|
|
this.beforeRequest()
|
|
|
|
}
|
|
|
|
let toReturn
|
|
|
|
try {
|
|
|
|
const response = await fetch(url, AJAXApi.genericRequestConfigGET);
|
|
|
|
if (!response.ok) {
|
2021-01-11 12:48:58 +01:00
|
|
|
throw new Error(`Network response was not ok. \`${response.statusText}\``)
|
2020-12-10 15:18:02 +01:00
|
|
|
}
|
|
|
|
const formHtml = await response.text();
|
|
|
|
let tmpNode = document.createElement("div");
|
|
|
|
tmpNode.innerHTML = formHtml;
|
|
|
|
let form = tmpNode.getElementsByTagName('form');
|
|
|
|
if (form.length == 0) {
|
|
|
|
throw new Error('The server did not return a form element')
|
|
|
|
}
|
|
|
|
toReturn = form[0];
|
|
|
|
} catch (error) {
|
|
|
|
this.provideFeedback({
|
|
|
|
variant: 'danger',
|
|
|
|
title: 'There has been a problem with the operation',
|
2021-01-14 12:42:05 +01:00
|
|
|
body: error.message
|
2020-12-15 10:40:49 +01:00
|
|
|
}, true, skipFeedback);
|
2020-12-10 15:18:02 +01:00
|
|
|
toReturn = Promise.reject(error);
|
|
|
|
} finally {
|
|
|
|
if (!skipRequestHooks) {
|
|
|
|
this.afterRequest()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toReturn
|
|
|
|
}
|
2020-12-15 10:40:49 +01:00
|
|
|
|
2021-03-11 09:20:03 +01:00
|
|
|
/**
|
|
|
|
* @param {string} url - The URL to fetch
|
|
|
|
* @param {Object} dataToPost - data to be posted
|
|
|
|
* @param {boolean} [skipRequestHooks=false] - If true, default request hooks will be skipped
|
|
|
|
* @param {boolean} [skipFeedback=false] - Pass this value to the AJAXApi.provideFeedback function
|
|
|
|
* @return {Promise<string>} Promise object resolving to the result of the POST
|
|
|
|
*/
|
|
|
|
async postData(url, dataToPost, skipRequestHooks=false, skipFeedback=false) {
|
|
|
|
if (!skipRequestHooks) {
|
|
|
|
this.beforeRequest()
|
|
|
|
}
|
|
|
|
let toReturn
|
|
|
|
try {
|
|
|
|
let formData = new FormData()
|
|
|
|
formData = AJAXApi.mergeFormData(formData, dataToPost)
|
|
|
|
let requestConfig = AJAXApi.genericRequestConfigPOST
|
2021-03-15 22:47:13 +01:00
|
|
|
requestConfig.headers.append('AUTHORIZATION', '~HACKY-HACK~')
|
2021-03-11 09:20:03 +01:00
|
|
|
let options = {
|
|
|
|
...requestConfig,
|
|
|
|
body: formData,
|
|
|
|
};
|
|
|
|
const response = await fetch(url, options);
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Network response was not ok. \`${response.statusText}\``)
|
|
|
|
}
|
|
|
|
const data = await response.json()
|
|
|
|
if (data.success) {
|
|
|
|
this.provideFeedback({
|
|
|
|
variant: 'success',
|
|
|
|
body: data.message
|
|
|
|
}, false, skipFeedback);
|
|
|
|
toReturn = data;
|
|
|
|
} else {
|
|
|
|
this.provideFeedback({
|
|
|
|
variant: 'danger',
|
|
|
|
title: 'There has been a problem with the operation',
|
|
|
|
body: data.message
|
|
|
|
}, true, skipFeedback);
|
|
|
|
toReturn = Promise.reject(data.errors);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.provideFeedback({
|
|
|
|
variant: 'danger',
|
|
|
|
title: 'There has been a problem with the operation',
|
|
|
|
body: error.message
|
|
|
|
}, true, skipFeedback);
|
|
|
|
toReturn = Promise.reject(error);
|
|
|
|
} finally {
|
|
|
|
if (!skipRequestHooks) {
|
|
|
|
this.afterRequest()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toReturn
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:49:07 +01:00
|
|
|
/**
|
|
|
|
* @param {HTMLFormElement} form - The form to be posted
|
|
|
|
* @param {Object} [dataToMerge={}] - Additional data to be integrated or modified in the form
|
|
|
|
* @param {boolean} [skipRequestHooks=false] - If true, default request hooks will be skipped
|
|
|
|
* @param {boolean} [skipFeedback=false] - Pass this value to the AJAXApi.provideFeedback function
|
|
|
|
* @return {Promise<Object>} Promise object resolving to the result of the POST operation
|
|
|
|
*/
|
2020-12-15 10:40:49 +01:00
|
|
|
async postForm(form, dataToMerge={}, skipRequestHooks=false, skipFeedback=false) {
|
2020-12-10 15:18:02 +01:00
|
|
|
if (!skipRequestHooks) {
|
|
|
|
this.beforeRequest()
|
|
|
|
}
|
|
|
|
let toReturn
|
2020-12-15 10:40:49 +01:00
|
|
|
let feedbackShown = false
|
2020-12-10 15:18:02 +01:00
|
|
|
try {
|
|
|
|
try {
|
|
|
|
let formData = new FormData(form)
|
|
|
|
formData = AJAXApi.mergeFormData(formData, dataToMerge)
|
2020-12-15 10:40:49 +01:00
|
|
|
let requestConfig = AJAXApi.genericRequestConfigPOST
|
2020-12-10 15:18:02 +01:00
|
|
|
let options = {
|
2020-12-15 10:40:49 +01:00
|
|
|
...requestConfig,
|
2020-12-10 15:18:02 +01:00
|
|
|
body: formData,
|
|
|
|
};
|
|
|
|
const response = await fetch(form.action, options);
|
|
|
|
if (!response.ok) {
|
2021-01-11 12:48:58 +01:00
|
|
|
throw new Error(`Network response was not ok. \`${response.statusText}\``)
|
2020-12-10 15:18:02 +01:00
|
|
|
}
|
2020-12-15 10:40:49 +01:00
|
|
|
const clonedResponse = response.clone()
|
|
|
|
try {
|
|
|
|
const data = await response.json()
|
|
|
|
if (data.success) {
|
|
|
|
this.provideFeedback({
|
|
|
|
variant: 'success',
|
|
|
|
body: data.message
|
|
|
|
}, false, skipFeedback);
|
|
|
|
toReturn = data;
|
|
|
|
} else {
|
|
|
|
this.provideFeedback({
|
|
|
|
variant: 'danger',
|
|
|
|
title: 'There has been a problem with the operation',
|
2021-01-12 10:16:58 +01:00
|
|
|
body: data.message
|
2020-12-15 10:40:49 +01:00
|
|
|
}, true, skipFeedback);
|
|
|
|
feedbackShown = true
|
2021-01-12 10:21:54 +01:00
|
|
|
this.injectFormValidationFeedback(form, data.errors)
|
2020-12-15 10:40:49 +01:00
|
|
|
toReturn = Promise.reject(data.errors);
|
|
|
|
}
|
2021-01-12 10:16:58 +01:00
|
|
|
} catch (error) {
|
|
|
|
this.provideFeedback({
|
|
|
|
variant: 'danger',
|
|
|
|
title: 'There has been a problem with the operation',
|
2021-01-14 12:42:05 +01:00
|
|
|
body: error.message
|
2021-01-12 10:16:58 +01:00
|
|
|
}, true, feedbackShown);
|
|
|
|
toReturn = Promise.reject(error);
|
2020-12-10 15:18:02 +01:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.provideFeedback({
|
|
|
|
variant: 'danger',
|
|
|
|
title: 'There has been a problem with the operation',
|
2021-01-14 12:42:05 +01:00
|
|
|
body: error.message
|
2020-12-15 10:40:49 +01:00
|
|
|
}, true, feedbackShown);
|
2020-12-10 15:18:02 +01:00
|
|
|
toReturn = Promise.reject(error);
|
|
|
|
}
|
2021-01-12 10:16:58 +01:00
|
|
|
} catch (error) {
|
2020-12-10 15:18:02 +01:00
|
|
|
toReturn = Promise.reject(error);
|
|
|
|
} finally {
|
|
|
|
if (!skipRequestHooks) {
|
|
|
|
this.afterRequest()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toReturn
|
|
|
|
}
|
2021-06-01 07:46:51 +02:00
|
|
|
|
2020-12-15 15:49:07 +01:00
|
|
|
/**
|
|
|
|
* @param {string} url - The URL from which to fetch the form
|
|
|
|
* @param {Object} [dataToMerge={}] - Additional data to be integrated or modified in the form
|
|
|
|
* @param {boolean} [skipRequestHooks=false] - If true, default request hooks will be skipped
|
|
|
|
* @return {Promise<Object>} Promise object resolving to the result of the POST operation
|
|
|
|
*/
|
2021-02-24 11:05:23 +01:00
|
|
|
async fetchAndPostForm(url, dataToMerge={}, skipRequestHooks=false, skipFeedback=false) {
|
2020-12-15 10:40:49 +01:00
|
|
|
if (!skipRequestHooks) {
|
|
|
|
this.beforeRequest()
|
|
|
|
}
|
|
|
|
let toReturn
|
|
|
|
try {
|
|
|
|
const form = await this.fetchForm(url, true, true);
|
2021-02-24 11:05:23 +01:00
|
|
|
toReturn = await this.postForm(form, dataToMerge, true, skipFeedback)
|
2020-12-15 10:40:49 +01:00
|
|
|
} catch (error) {
|
|
|
|
toReturn = Promise.reject(error);
|
|
|
|
} finally {
|
|
|
|
if (!skipRequestHooks) {
|
|
|
|
this.afterRequest()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toReturn
|
|
|
|
}
|
2020-12-10 15:18:02 +01:00
|
|
|
|
2021-01-12 10:16:58 +01:00
|
|
|
/**
|
|
|
|
* @param {HTMLFormElement} form - The form form which the POST operation is coming from
|
|
|
|
* @param {Object} [validationErrors={}] - Validation errors reported by the server
|
|
|
|
*/
|
2021-01-12 10:21:54 +01:00
|
|
|
injectFormValidationFeedback(form, validationErrors) {
|
2021-01-13 14:19:22 +01:00
|
|
|
const formHelper = new FormValidationHelper(form)
|
2021-01-12 10:16:58 +01:00
|
|
|
formHelper.injectValidationErrors(validationErrors)
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:49:07 +01:00
|
|
|
/** Based on the configuration, show the loading overlay */
|
2020-12-10 15:18:02 +01:00
|
|
|
beforeRequest() {
|
|
|
|
if (this.options.statusNode !== false) {
|
|
|
|
this.toggleLoading(true)
|
|
|
|
}
|
|
|
|
}
|
2020-12-15 15:49:07 +01:00
|
|
|
|
|
|
|
/** Based on the configuration, hide the loading overlay */
|
2020-12-10 15:18:02 +01:00
|
|
|
afterRequest() {
|
|
|
|
if (this.options.statusNode !== false) {
|
|
|
|
this.toggleLoading(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:49:07 +01:00
|
|
|
/** Show or hide the loading overlay */
|
2020-12-10 15:18:02 +01:00
|
|
|
toggleLoading(loading) {
|
|
|
|
if (this.loadingOverlay === false) {
|
2021-01-18 09:48:12 +01:00
|
|
|
this.loadingOverlay = new OverlayFactory(this.options.statusNode, this.options.statusNodeOverlayConfig);
|
2020-12-10 15:18:02 +01:00
|
|
|
}
|
|
|
|
if (loading) {
|
|
|
|
this.loadingOverlay.show()
|
|
|
|
} else {
|
|
|
|
this.loadingOverlay.hide()
|
2021-06-01 07:46:51 +02:00
|
|
|
|
2020-12-10 15:18:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|