chg: [js-helper] Added quick methods and more documentation

pull/37/head
mokaddem 2020-12-11 10:38:43 +01:00
parent f4725207fc
commit ae0272a62c
3 changed files with 41 additions and 16 deletions

View File

@ -12,8 +12,8 @@ class AJAXApi {
} }
static defaultOptions = { static defaultOptions = {
showToast: true, provideFeedback: true,
statusNode: false statusNode: false,
} }
options = {} options = {}
loadingOverlay = false loadingOverlay = false
@ -23,11 +23,13 @@ class AJAXApi {
this.mergeOptions(options) this.mergeOptions(options)
} }
provideFeedback(options) { provideFeedback(options, isError=false) {
if (this.options.showToast) { if (this.options.provideFeedback) {
UI.toast(options) UI.toast(options)
} else { } else {
console.error(options.body) if (isError) {
console.error(options.body)
}
} }
} }
@ -42,6 +44,24 @@ class AJAXApi {
return formData return formData
} }
static async quickFetchURL(url, options={}) {
const constAlteredOptions = Object.assign({}, {provideFeedback: false}, options)
const tmpApi = new AJAXApi(constAlteredOptions)
return tmpApi.fetchURL(url, constAlteredOptions.skipRequestHooks)
}
static async quickFetchForm(url, options={}) {
const constAlteredOptions = Object.assign({}, {provideFeedback: false}, options)
const tmpApi = new AJAXApi(constAlteredOptions)
return tmpApi.fetchForm(url, constAlteredOptions.skipRequestHooks)
}
static async quickFetchAndPostForm(url, dataToMerge={}, options={}) {
const constAlteredOptions = Object.assign({}, {}, options)
const tmpApi = new AJAXApi(constAlteredOptions)
return tmpApi.fetchAndPostForm(url, dataToMerge, constAlteredOptions.skipRequestHooks)
}
async fetchURL(url, skipRequestHooks=false) { async fetchURL(url, skipRequestHooks=false) {
if (!skipRequestHooks) { if (!skipRequestHooks) {
this.beforeRequest() this.beforeRequest()
@ -53,13 +73,17 @@ class AJAXApi {
throw new Error('Network response was not ok') throw new Error('Network response was not ok')
} }
const data = await response.text(); const data = await response.text();
this.provideFeedback({
variant: 'success',
title: 'URL fetched',
});
toReturn = data; toReturn = data;
} catch (error) { } catch (error) {
this.provideFeedback({ this.provideFeedback({
variant: 'danger', variant: 'danger',
title: 'There has been a problem with the operation', title: 'There has been a problem with the operation',
body: error body: error
}); }, true);
toReturn = Promise.reject(error); toReturn = Promise.reject(error);
} finally { } finally {
if (!skipRequestHooks) { if (!skipRequestHooks) {
@ -92,7 +116,7 @@ class AJAXApi {
variant: 'danger', variant: 'danger',
title: 'There has been a problem with the operation', title: 'There has been a problem with the operation',
body: error body: error
}); }, true);
toReturn = Promise.reject(error); toReturn = Promise.reject(error);
} finally { } finally {
if (!skipRequestHooks) { if (!skipRequestHooks) {
@ -132,7 +156,7 @@ class AJAXApi {
variant: 'danger', variant: 'danger',
title: 'There has been a problem with the operation', title: 'There has been a problem with the operation',
body: data.errors body: data.errors
}); }, true);
toReturn = Promise.reject(error); toReturn = Promise.reject(error);
} }
} catch (error) { } catch (error) {
@ -140,7 +164,7 @@ class AJAXApi {
variant: 'danger', variant: 'danger',
title: 'There has been a problem with the operation', title: 'There has been a problem with the operation',
body: error body: error
}); }, true);
toReturn = Promise.reject(error); toReturn = Promise.reject(error);
} }
} catch (error) { } catch (error) {

View File

@ -1,5 +1,6 @@
class UIFactory { class UIFactory {
/* Display a toast based on provided options */
toast(options) { toast(options) {
const theToast = new Toaster(options); const theToast = new Toaster(options);
theToast.makeToast() theToast.makeToast()
@ -7,6 +8,7 @@ class UIFactory {
return theToast return theToast
} }
/* Display a modal based on provided options */
modal (options) { modal (options) {
const theModal = new ModalFactory(options); const theModal = new ModalFactory(options);
theModal.makeModal() theModal.makeModal()
@ -14,17 +16,17 @@ class UIFactory {
return theModal return theModal
} }
/* Fetch HTML from the provided URL and override content of $container. $statusNode allows to specify another HTML node to display the loading */
reload (url, $container, $statusNode=null) { reload (url, $container, $statusNode=null) {
$container = $($container) $container = $($container)
$statusNode = $($statusNode) $statusNode = $($statusNode)
if (!$statusNode) { if (!$statusNode) {
$statusNode = $container $statusNode = $container
} }
const tmpApi = new AJAXApi({ AJAXApi.quickFetchURL(url, {
statusNode: $statusNode[0], statusNode: $statusNode[0]
}) }).then((data) => {
tmpApi.fetchURL(url).then((data) => { $container[0].outerHTML = data
$container.html(data)
}) })
} }
} }

View File

@ -38,8 +38,7 @@ function executeStateDependencyChecks(dependenceSourceSelector) {
}); });
} }
var AjaxApi, UI var UI
$(document).ready(() => { $(document).ready(() => {
AjaxApi = new AJAXApi()
UI = new UIFactory() UI = new UIFactory()
}) })