chg: [js:api] new functionalities and refacto
parent
35165129b9
commit
1a08670671
|
@ -11,19 +11,24 @@ class AJAXApi {
|
||||||
redirect: 'manual',
|
redirect: 'manual',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
}
|
}
|
||||||
|
static genericRequestConfigGETJSON = {
|
||||||
|
headers: new Headers(Object.assign({}, AJAXApi.genericRequestHeaders, {Accept: 'application/json'}))
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @namespace
|
* @namespace
|
||||||
* @property {boolean} provideFeedback - The ID to be used for the toast's container
|
* @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 {(jQuery|string)} statusNode - The node on which the loading overlay should be placed (OverlayFactory.node)
|
||||||
* @property {Object} errorToast - The options supported by Toaster#defaultOptions
|
* @property {Object} errorToastOptions - The options supported by Toaster#defaultOptions
|
||||||
*/
|
*/
|
||||||
static defaultOptions = {
|
static defaultOptions = {
|
||||||
provideFeedback: true,
|
provideFeedback: true,
|
||||||
statusNode: false,
|
statusNode: false,
|
||||||
errorToast: {
|
errorToastOptions: {
|
||||||
delay: 10000
|
delay: 10000
|
||||||
}
|
},
|
||||||
|
successToastOptions: {
|
||||||
|
},
|
||||||
}
|
}
|
||||||
options = {}
|
options = {}
|
||||||
loadingOverlay = false
|
loadingOverlay = false
|
||||||
|
@ -44,7 +49,11 @@ class AJAXApi {
|
||||||
* @param {boolean} skip - If true, skip the feedback regardless of the configuration
|
* @param {boolean} skip - If true, skip the feedback regardless of the configuration
|
||||||
*/
|
*/
|
||||||
provideFeedback(toastOptions, isError=false, skip=false) {
|
provideFeedback(toastOptions, isError=false, skip=false) {
|
||||||
const alteredToastOptions = isError ? Object.assign({}, AJAXApi.defaultOptions.errorToast, toastOptions) : toastOptions
|
const alteredToastOptions = Object.assign(
|
||||||
|
{},
|
||||||
|
isError ? AJAXApi.defaultOptions.errorToastOptions : AJAXApi.defaultOptions.successToastOptions,
|
||||||
|
toastOptions
|
||||||
|
)
|
||||||
if (!skip) {
|
if (!skip) {
|
||||||
if (this.options.provideFeedback) {
|
if (this.options.provideFeedback) {
|
||||||
UI.toast(alteredToastOptions)
|
UI.toast(alteredToastOptions)
|
||||||
|
@ -78,7 +87,7 @@ class AJAXApi {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} url - The URL to fetch
|
* @param {string} url - The URL to fetch
|
||||||
* @param {Object} [options={}] - The options supported by AJAXApi#defaultOptions
|
* @param {Object} [options={}] - The options supported by AJAXApi#defaultOptions
|
||||||
* @return {Promise<string>} Promise object resolving to the fetched HTML
|
* @return {Promise<string>} Promise object resolving to the fetched HTML
|
||||||
*/
|
*/
|
||||||
|
@ -88,6 +97,17 @@ class AJAXApi {
|
||||||
return tmpApi.fetchURL(url, constAlteredOptions.skipRequestHooks)
|
return tmpApi.fetchURL(url, constAlteredOptions.skipRequestHooks)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} url - The URL to fetch
|
||||||
|
* @param {Object} [options={}] - The options supported by AJAXApi#defaultOptions
|
||||||
|
* @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)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} url - The URL to fetch
|
* @param {string} url - The URL to fetch
|
||||||
* @param {Object} [options={}] - The options supported by AJAXApi#defaultOptions
|
* @param {Object} [options={}] - The options supported by AJAXApi#defaultOptions
|
||||||
|
@ -159,6 +179,43 @@ class AJAXApi {
|
||||||
return toReturn
|
return toReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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',
|
||||||
|
body: error
|
||||||
|
}, true, skipFeedback);
|
||||||
|
toReturn = Promise.reject(error);
|
||||||
|
} finally {
|
||||||
|
if (!skipRequestHooks) {
|
||||||
|
this.afterRequest()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return toReturn
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} url - The URL to fetch
|
* @param {string} url - The URL to fetch
|
||||||
* @param {boolean} [skipRequestHooks=false] - If true, default request hooks will be skipped
|
* @param {boolean} [skipRequestHooks=false] - If true, default request hooks will be skipped
|
||||||
|
@ -298,7 +355,7 @@ class AJAXApi {
|
||||||
* @param {Object} [validationErrors={}] - Validation errors reported by the server
|
* @param {Object} [validationErrors={}] - Validation errors reported by the server
|
||||||
*/
|
*/
|
||||||
injectFormValidationFeedback(form, validationErrors) {
|
injectFormValidationFeedback(form, validationErrors) {
|
||||||
const formHelper = new FormHelper(form)
|
const formHelper = new FormValidationHelper(form)
|
||||||
formHelper.injectValidationErrors(validationErrors)
|
formHelper.injectValidationErrors(validationErrors)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue