69 lines
1.8 KiB
JavaScript
69 lines
1.8 KiB
JavaScript
import Dataset from "./dataset.js";
|
|
|
|
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));
|
|
|
|
cancelBtn.addEventListener("click", () => {
|
|
window.location.href = location.origin;
|
|
})
|
|
|
|
form.addEventListener("submit", async 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;
|
|
|
|
let response = await fetch("/api/v1/datasets", {
|
|
method: "POST",
|
|
body: JSON.stringify(newContent),
|
|
headers: {
|
|
"Content-Type": "application/json;charset=utf-8"
|
|
}
|
|
|
|
});
|
|
let data = await response.json();
|
|
let dataset = new Dataset(data);
|
|
dataset.storageSetKey("created-locally", true);
|
|
if (response.ok) {
|
|
location.assign("/");
|
|
} else {
|
|
addBtn.disabled = !form.checkValidity();
|
|
}
|
|
});
|