chg: [helpers:api-helpers] Slight rework on notifications
parent
ef86e77e41
commit
34fc7ad027
|
@ -68,6 +68,49 @@ class AJAXApi {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
provideSuccessFeedback(response, toastOptions, skip=false) {
|
||||||
|
let alteredToastOptions = Object.assign(
|
||||||
|
{
|
||||||
|
'variant': 'success'
|
||||||
|
},
|
||||||
|
AJAXApi.defaultOptions.successToastOptions,
|
||||||
|
toastOptions
|
||||||
|
)
|
||||||
|
alteredToastOptions.body = response.message
|
||||||
|
if (!skip && this.options.provideFeedback) {
|
||||||
|
UI.toast(alteredToastOptions)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
provideFailureFeedback(response, toastOptions, skip=false) {
|
||||||
|
let alteredToastOptions = Object.assign(
|
||||||
|
{
|
||||||
|
'variant': 'danger'
|
||||||
|
},
|
||||||
|
AJAXApi.defaultOptions.errorToastOptions,
|
||||||
|
toastOptions
|
||||||
|
)
|
||||||
|
if (response.message && response.errors) {
|
||||||
|
if (Array.isArray(response.errors)) {
|
||||||
|
alteredToastOptions.title = response.message
|
||||||
|
alteredToastOptions.body = response.errors.join(', ')
|
||||||
|
} else if (typeof response.errors === 'string') {
|
||||||
|
alteredToastOptions.title = response.message
|
||||||
|
alteredToastOptions.body = response.errors
|
||||||
|
} else {
|
||||||
|
alteredToastOptions.title = 'There has been a problem with the operation'
|
||||||
|
alteredToastOptions.body = response.message
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
alteredToastOptions.title = 'There has been a problem with the operation'
|
||||||
|
alteredToastOptions.body = response.message
|
||||||
|
}
|
||||||
|
if (!skip && this.options.provideFeedback) {
|
||||||
|
UI.toast(alteredToastOptions)
|
||||||
|
console.warn(alteredToastOptions.body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Merge newOptions configuration into the current object
|
* Merge newOptions configuration into the current object
|
||||||
* @param {Object} The options supported by AJAXApi#defaultOptions
|
* @param {Object} The options supported by AJAXApi#defaultOptions
|
||||||
|
@ -175,17 +218,10 @@ class AJAXApi {
|
||||||
throw new Error(`Network response was not ok. \`${response.statusText}\``)
|
throw new Error(`Network response was not ok. \`${response.statusText}\``)
|
||||||
}
|
}
|
||||||
const dataHtml = await response.text();
|
const dataHtml = await response.text();
|
||||||
this.provideFeedback({
|
this.provideSuccessFeedback({message: 'URL fetched'}, {}, skipFeedback)
|
||||||
variant: 'success',
|
|
||||||
title: 'URL fetched',
|
|
||||||
}, false, skipFeedback);
|
|
||||||
toReturn = dataHtml;
|
toReturn = dataHtml;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.provideFeedback({
|
this.provideFailureFeedback(error, {}, skipFeedback)
|
||||||
variant: 'danger',
|
|
||||||
title: 'There has been a problem with the operation',
|
|
||||||
body: error.message
|
|
||||||
}, true, skipFeedback);
|
|
||||||
toReturn = Promise.reject(error);
|
toReturn = Promise.reject(error);
|
||||||
} finally {
|
} finally {
|
||||||
if (!skipRequestHooks) {
|
if (!skipRequestHooks) {
|
||||||
|
@ -212,17 +248,10 @@ class AJAXApi {
|
||||||
throw new Error(`Network response was not ok. \`${response.statusText}\``)
|
throw new Error(`Network response was not ok. \`${response.statusText}\``)
|
||||||
}
|
}
|
||||||
const dataJson = await response.json();
|
const dataJson = await response.json();
|
||||||
this.provideFeedback({
|
this.provideSuccessFeedback({message: 'JSON fetched'}, {}, skipFeedback)
|
||||||
variant: 'success',
|
|
||||||
title: 'URL fetched',
|
|
||||||
}, false, skipFeedback);
|
|
||||||
toReturn = dataJson;
|
toReturn = dataJson;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.provideFeedback({
|
this.provideFailureFeedback(error, {}, skipFeedback)
|
||||||
variant: 'danger',
|
|
||||||
title: 'There has been a problem with the operation',
|
|
||||||
body: error.message
|
|
||||||
}, true, skipFeedback);
|
|
||||||
toReturn = Promise.reject(error);
|
toReturn = Promise.reject(error);
|
||||||
} finally {
|
} finally {
|
||||||
if (!skipRequestHooks) {
|
if (!skipRequestHooks) {
|
||||||
|
@ -257,11 +286,7 @@ class AJAXApi {
|
||||||
}
|
}
|
||||||
toReturn = form[0];
|
toReturn = form[0];
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.provideFeedback({
|
this.provideFailureFeedback(error, {}, skipFeedback)
|
||||||
variant: 'danger',
|
|
||||||
title: 'There has been a problem with the operation',
|
|
||||||
body: error.message
|
|
||||||
}, true, skipFeedback);
|
|
||||||
toReturn = Promise.reject(error);
|
toReturn = Promise.reject(error);
|
||||||
} finally {
|
} finally {
|
||||||
if (!skipRequestHooks) {
|
if (!skipRequestHooks) {
|
||||||
|
@ -302,13 +327,10 @@ class AJAXApi {
|
||||||
variant: 'success',
|
variant: 'success',
|
||||||
body: data.message
|
body: data.message
|
||||||
}, false, skipFeedback);
|
}, false, skipFeedback);
|
||||||
|
this.provideSuccessFeedback(data, {}, skipFeedback)
|
||||||
toReturn = data;
|
toReturn = data;
|
||||||
} else {
|
} else {
|
||||||
this.provideFeedback({
|
this.provideFailureFeedback(data, {}, skipFeedback)
|
||||||
variant: 'danger',
|
|
||||||
title: 'There has been a problem with the operation',
|
|
||||||
body: data.message
|
|
||||||
}, true, skipFeedback);
|
|
||||||
toReturn = Promise.reject(data.errors);
|
toReturn = Promise.reject(data.errors);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -327,7 +349,7 @@ class AJAXApi {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {HTMLFormElement} form - The form to be posted
|
* @param {HTMLFormElement} form - The form to be posted
|
||||||
* @param {Object} [dataToMerge={}] - Additional data to be integrated or modified in 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
|
* @param {boolean} [skipRequestHooks=false] - If true, default request hooks will be skipped
|
||||||
* @param {boolean} [skipFeedback=false] - Pass this value to the AJAXApi.provideFeedback function
|
* @param {boolean} [skipFeedback=false] - Pass this value to the AJAXApi.provideFeedback function
|
||||||
|
@ -356,17 +378,10 @@ class AJAXApi {
|
||||||
try {
|
try {
|
||||||
const data = await response.json()
|
const data = await response.json()
|
||||||
if (data.success) {
|
if (data.success) {
|
||||||
this.provideFeedback({
|
this.provideSuccessFeedback(data, {}, skipFeedback)
|
||||||
variant: 'success',
|
|
||||||
body: data.message
|
|
||||||
}, false, skipFeedback);
|
|
||||||
toReturn = data;
|
toReturn = data;
|
||||||
} else {
|
} else {
|
||||||
this.provideFeedback({
|
this.provideFailureFeedback(data, {}, skipFeedback)
|
||||||
variant: 'danger',
|
|
||||||
title: 'There has been a problem with the operation',
|
|
||||||
body: data.message
|
|
||||||
}, true, skipFeedback);
|
|
||||||
feedbackShown = true
|
feedbackShown = true
|
||||||
this.injectFormValidationFeedback(form, data.errors)
|
this.injectFormValidationFeedback(form, data.errors)
|
||||||
toReturn = Promise.reject(data.errors);
|
toReturn = Promise.reject(data.errors);
|
||||||
|
|
Loading…
Reference in New Issue