Web view still in progress
This commit is contained in:
@@ -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);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user