This commit is contained in:
2024-01-23 23:15:29 -05:00
parent eecb79abb8
commit 3a0163fd42
7 changed files with 88 additions and 15 deletions

View File

@ -10,6 +10,24 @@ app.util = (function (app) {
: decodeURIComponent(results[1].replace(/\+/g, " "));
}
function promisify(f) {
return function (...args) { // return a wrapper-function (*)
return new Promise((resolve, reject) => {
function callback(err, result) { // our custom callback for f (**)
if (err) {
reject(err);
} else {
resolve(result);
}
}
args.push(callback); // append our custom callback to the end of f arguments
f.call(this, ...args); // call the original function
});
};
}
function actionMessage(message, $target, type, callback) {
message = message || "";
$target = $target.closest("div.iot-card").find(".actionMessage");
@ -52,12 +70,13 @@ app.util = (function (app) {
};
return {
getUrlParameter: getUrlParameter,
actionMessage: actionMessage,
getUrlParameter,
actionMessage,
promisify
};
})(app);
app.api = (function (app) {
app.apiSync = (function (app) {
var baseURL = "/api/v0/";
function post(url, data, callback) {
@ -141,7 +160,21 @@ app.api = (function (app) {
});
}
return { post: post, get: get, put: put, delete: remove };
return {
post: app.util.promisify(post),
get: app.util.promisify(get),
put: app.util.promisify(put),
delete: app.util.promisify(remove),
};
})(app);
app.api = (function (app) {
return {
post: app.util.promisify(app.apiSync.post),
get: app.util.promisify(app.apiSync.get),
put: app.util.promisify(app.apiSync.put),
delete: app.util.promisify(app.apiSync.remove),
};
})(app);
//socket.io
@ -184,7 +217,7 @@ app.auth = (function (app) {
function isLoggedIn(callback) {
if (getToken()) {
return app.api.get("user/me", function (error, data) {
return app.apiSync.get("user/me", function (error, data) {
if (!error) app.auth.user = data;
//for navbar to show username
if (!location.pathname === "/login")
@ -268,7 +301,7 @@ app.auth = (function (app) {
app.user = (function (app) {
//delete profile
function deleteProfile() {
app.api.delete("user/delete", function (error, data) {
app.apiSync.delete("user/delete", function (error, data) {
if (error) {
app.util.actionMessage(error.message, $("#deleteProfile"), "danger");
} else {
@ -294,7 +327,7 @@ function formAJAX(btn, del) {
//console.log('Data being sent to', $form.attr('action'), formData)
app.api[method]($form.attr("action"), formData, function (error, data) {
app.apiSync[method]($form.attr("action"), formData, function (error, data) {
//console.log('Data back from the server', error, data)
app.util.actionMessage(data.message, $form, error ? "danger" : "success"); //re-populate table
if (!error) {