Restructure javascript

This commit is contained in:
Patrick Jentsch
2023-03-08 11:42:53 +01:00
parent 0d7fca9b0b
commit 6bb4594937
8 changed files with 131 additions and 22 deletions

View File

@@ -0,0 +1,37 @@
Requests = {};
Requests.JSONfetch = (input, init={}) => {
return new Promise((resolve, reject) => {
let fixedInit = {};
fixedInit.headers = {};
fixedInit.headers['Accept'] = 'application/json';
if (init.hasOwnProperty('body')) {
fixedInit.headers['Content-Type'] = 'application/json';
}
fetch(input, Utils.mergeObjectsDeep(init, fixedInit))
.then(
(response) => {
response.json()
.then(
(json) => {
let message = json.message || json;
let category = json.category || 'message';
app.flash(message, category);
},
(error) => {
app.flash(`[${response.status}]: ${response.statusText}`, 'error');
}
);
if (response.ok) {
resolve(response);
} else {
reject(response);
}
},
(response) => {
app.flash('Something went wrong', 'error');
reject(response);
}
);
});
};