Added existing URL check

This commit is contained in:
Raphaël Thériault 2019-10-25 14:09:25 -04:00
parent f9fda22141
commit 8a704eb283
2 changed files with 43 additions and 12 deletions

View File

@ -31,9 +31,33 @@ const inputs = {
],
};
const used = {
files: [],
links: [],
texts: [],
};
let baseUrl = `${location.protocol}//${location.host}${location.pathname}`;
if (!baseUrl.endsWith("/")) {
baseUrl += "/";
}
const fetchUsed = () => {
fetch(`${baseUrl}f`)
.then((response) => response.json())
.then((json) => (used.files = json));
fetch(`${baseUrl}l`)
.then((response) => response.json())
.then((json) => (used.links = json));
fetch(`${baseUrl}t`)
.then((response) => response.json())
.then((json) => (used.texts = json));
};
fetchUsed();
const randomUrl = () => {
return Math.floor(Math.random() * 2147483647).toString(36);
}
};
for (const group in tabs) {
tabs[group][0].onclick = () => {
@ -50,12 +74,6 @@ for (const group in tabs) {
for (const group in inputs) {
const submitButton = inputs[group][inputs[group].length - 1];
const checkValidity = () => {
submitButton.disabled = inputs[group].some(
(input) => input.validity != undefined && !input.validity.valid
);
};
const urlInput = inputs[group][0];
urlInput.addEventListener("input", () => {
urlInput.value = urlInput.value
@ -77,6 +95,17 @@ for (const group in inputs) {
}
});
const checkValidity = () => {
if (used[group].some((x) => x.id === parseInt(urlInput.value, 36))) {
urlInput.classList.add("conflict");
} else {
urlInput.classList.remove("conflict");
}
submitButton.disabled = inputs[group].some(
(input) => input.validity != undefined && !input.validity.valid
);
};
for (const input of inputs[group].filter(
(input) =>
input instanceof HTMLInputElement ||
@ -97,11 +126,6 @@ for (const group in inputs) {
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");
@ -150,6 +174,7 @@ for (const group in inputs) {
window.open(url, "_blank");
clearInputs();
filesValueInput.value = "";
fetchUsed();
}
})
.catch((error) => alert(error));
@ -178,6 +203,7 @@ for (const group in inputs) {
} else {
window.open(url, "_blank");
clearInputs();
fetchUsed();
}
})
.catch((error) => alert(error));
@ -204,6 +230,7 @@ for (const group in inputs) {
} else {
window.open(url, "_blank");
clearInputs();
fetchUsed();
}
})
.catch((error) => alert(error));

View File

@ -108,6 +108,10 @@ main div textarea:invalid {
border: 0.0625rem solid #dc322f;
}
.conflict {
border: 0.0625rem solid #b58900 !important;
}
main div button {
display: block;
padding: 0.5rem 1rem;