Implement JS + add URL field

This commit is contained in:
Elias Schriefer
2024-06-21 11:27:26 +02:00
parent 49f7c92178
commit ee5d9712f4
3 changed files with 130 additions and 25 deletions

View File

@@ -0,0 +1,56 @@
const form = document.forms[0];
const {
title: titleEntry,
author: authorEntry,
["is-dataset"]: isDatasetSwitch,
["short-description"]: shortDescriptionEntry,
url: urlEntry,
["full-description"]: fullDescriptionEntry,
["btn-add"]: addBtn,
["btn-cancel"]: cancelBtn,
} = form.elements;
const validationListener = () => {
addBtn.disabled = !form.checkValidity();
};
// Register validationListener on all required inputs that must be valid
[
titleEntry,
authorEntry,
shortDescriptionEntry,
urlEntry,
fullDescriptionEntry,
].forEach(input => input.addEventListener("input", validationListener));
form.addEventListener("submit", e => {
e.preventDefault();
if (!form.reportValidity()) return;
// Create the request body
const newContent = {
title: titleEntry.value,
author: authorEntry.value,
abst: shortDescriptionEntry.value,
url: urlEntry.value,
description: fullDescriptionEntry.value,
type: isDatasetSwitch.checked ? "API" : "DATASET",
categories: [],
};
console.debug(newContent);
// Don't allow several requests to be sent at the same time
addBtn.disabled = true;
fetch("/api/v1/datasets", {
method: "POST",
body: JSON.stringify(newContent),
}).then(response => {
if (response.status == 200) {
location.assign("/");
} else {
addBtn.disabled = !checkValidity();
}
});
});