Finalized web UI

This commit is contained in:
Raphaël Thériault 2019-10-25 01:07:56 -04:00
parent 464a5f12f8
commit f234c12fc8
1 changed files with 107 additions and 1 deletions

View File

@ -44,8 +44,9 @@ for (const group in tabs) {
}
for (const group in inputs) {
const submitButton = inputs[group][inputs[group].length - 1];
const checkValidity = () => {
const submitButton = inputs[group][inputs[group].length - 1];
submitButton.disabled = inputs[group].some(
(input) => input.validity != undefined && !input.validity.valid
);
@ -74,6 +75,22 @@ for (const group in inputs) {
input.addEventListener("change", () => checkValidity());
}
const clearInputs = () => {
for (const input of inputs[group].filter(
(input) =>
input instanceof HTMLInputElement ||
input instanceof HTMLTextAreaElement
)) {
input.value = "";
}
submitButton.disabled = true;
};
let baseUrl = `${location.protocol}//${location.host}${location.pathname}`;
if (!baseUrl.endsWith("/")) {
baseUrl += "/";
}
if (group === "files") {
const filesFileInput = inputs.files[1];
const filesBrowseButton = document.querySelector("#files-browse");
@ -89,7 +106,96 @@ for (const group in inputs) {
filesValueInput.blur();
return false;
};
submitButton.addEventListener("click", () => {
const file = filesFileInput.files[0];
if (!file) {
alert(new Error("No file selected"));
return;
}
let fileReader = new FileReader();
fileReader.onload = () => {
const id = urlInput.value;
const url = `${baseUrl}f/${id}`;
const base64 = btoa(fileReader.result);
const filename = file.name;
let status;
fetch(url, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ base64, filename }),
})
.then((response) => {
status = response.status;
return response.text();
})
.then((text) => {
if (status !== 201) {
throw new Error(text);
} else {
window.open(url, "_blank");
clearInputs();
filesValueInput.value = "";
}
})
.catch((error) => alert(error));
};
fileReader.readAsBinaryString(file);
});
} else if (group === "links") {
submitButton.addEventListener("click", () => {
const id = urlInput.value;
const forward = inputs.links[1].value;
const url = `${baseUrl}l/${id}`;
let status;
fetch(url, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ forward }),
})
.then((response) => {
status = response.status;
return response.text();
})
.then((text) => {
if (status !== 201) {
throw new Error(text);
} else {
window.open(url, "_blank");
clearInputs();
}
})
.catch((error) => alert(error));
});
} else if (group === "texts") {
submitButton.addEventListener("click", () => {
const id = urlInput.value;
const contents = inputs.texts[1].value;
const url = `${baseUrl}t/${id}`;
let status;
fetch(url, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ contents }),
})
.then((response) => {
status = response.status;
return response.text();
})
.then((text) => {
if (status !== 201) {
throw new Error(text);
} else {
window.open(url, "_blank");
clearInputs();
}
})
.catch((error) => alert(error));
});
}
}