Web view still in progress

This commit is contained in:
Claude Brisson
2023-11-05 13:51:01 +01:00
parent 5fdf3e8944
commit 5f068476dc
111 changed files with 8905 additions and 215 deletions

View File

@@ -23,6 +23,30 @@ let headers = function() {
return ret;
};
function clearFeedback() {
$('#error')[0].innerText = '';
$('#error, #success').addClass('hidden');
}
function success() {
$('#error')[0].innerText = '';
$('#error').addClass('hidden');
}
function error(response) {
const contentType = response.headers.get("content-type");
let promise =
(contentType && contentType.indexOf("application/json") !== -1)
? response.json().then(json => json.error || "unknown error")
: Promise.resolve(response.statusText);
promise.then(message => {
message = message.replaceAll(/([a-z])([A-Z])/g,"$1 $2").toLowerCase()
console.error(message);
$('#error')[0].innerText = message;
$('#error').removeClass('hidden');
});
}
let api = {
get: (path) => fetch(base + path, {
credentials: "same-origin",
@@ -49,22 +73,61 @@ let api = {
/* then, some helpers */
getJson: (path) => api.get(path)
.then(resp => {
if (resp.ok) return resp.json();
else throw resp.statusText;
}),
getJson: (path) => {
clearFeedback();
return api.get(path)
.then(resp => {
if (resp.ok) {
return resp.json();
}
else throw resp;
})
.catch(err => {
error(err);
return 'error'
});
},
postJson: (path, body) => api.post(path, body)
.then(resp => {
if (resp.ok) return resp.json();
else throw resp.statusText;
}),
postJson: (path, body) => {
clearFeedback();
spinner(true);
return api.post(path, body)
.then(resp => {
if (resp.ok) {
success();
return resp.json();
}
else {
throw resp;
}
})
.catch(err => {
error(err);
return 'error';
})
.finally(() => {
spinner(false);
});
},
putJson: (path, body) => api.put(path, body)
.then(resp => {
if (resp.ok) return resp.json();
else throw resp.statusText;
})
putJson: (path, body) => {
clearFeedback();
spinner(true);
return api.put(path, body)
.then(resp => {
if (resp.ok) {
success();
return resp.json();
}
else throw resp;
})
.catch(err => {
error(err);
return 'error';
})
.finally(() => {
spinner(false);
});
}
};