Improve domain fetch api

This commit is contained in:
2021-05-09 18:38:50 +02:00
parent 7bb9c67f52
commit 0dd6fcbf5e
2 changed files with 67 additions and 15 deletions

View File

@ -28,18 +28,49 @@ function createLoginWindow() {
<button type="submit">Login</button> <button type="submit">Login</button>
</form>`; </form>`;
win.getElementsByTagName('form')[0].addEventListener("submit", function (evt) { win.getElementsByTagName("form")[0].addEventListener("submit", (evt) => {
evt.preventDefault(); evt.preventDefault();
let form = evt.target; let form = evt.target;
for (let e of form) e.disabled = true;
for (let d of form.getElementsByTagName("div")) {
form.removeChild(d);
}
usimp.lookup(form.domain.value) usimp.lookup(form.domain.value)
.then(res => res.json() .then(res => {
.then(data => console.log(data)) if (res.ok) {
.catch(reason => console.error(reason))) res.json()
.catch(reason => console.error(reason)); .then(data => console.log(data))
.catch(reason => console.error(reason))
} else {
document.getElementsByName("domain")[0].setAttribute("invalid", "invalid");
let div = document.createElement("div");
div.classList.add("error");
div.innerText = "Invalid USIMP domain";
form.appendChild(div);
}
})
.catch(reason => {
document.getElementsByName("domain")[0].setAttribute("invalid", "invalid");
let div = document.createElement("div");
div.classList.add("error");
div.innerText = "Invalid USIMP domain";
form.appendChild(div);
})
.finally(() => {
for (let e of form) e.disabled = false;
});
}); });
while (windows.lastChild) windows.removeChild(windows.lastChild); while (windows.lastChild) windows.removeChild(windows.lastChild);
windows.appendChild(win); windows.appendChild(win);
document.getElementsByName("domain")[0].addEventListener("input", (evt) => {
evt.target.removeAttribute("invalid");
});
document.getElementsByName("account")[0].focus();
} }
function handleHash(hash) { function handleHash(hash) {
@ -64,7 +95,7 @@ function handleUrl(url) {
handleHash(new URL(url).hash); handleHash(new URL(url).hash);
} }
window.addEventListener("DOMContentLoaded", function () { window.addEventListener("DOMContentLoaded", (evt) => {
main = document.getElementsByTagName("main")[0]; main = document.getElementsByTagName("main")[0];
windows = document.getElementById("windows"); windows = document.getElementById("windows");
@ -74,6 +105,6 @@ window.addEventListener("DOMContentLoaded", function () {
handleUrl(document.URL) handleUrl(document.URL)
}); });
window.addEventListener("hashchange", function (evt) { window.addEventListener("hashchange", (evt) => {
handleUrl(evt.newURL); handleUrl(evt.newURL);
}); });

View File

@ -116,8 +116,8 @@ form {
form input, form input,
form button, form button,
a.button { a.button,
background-color: var(--bg); form div {
border: 1px solid var(--bg); border: 1px solid var(--bg);
border-radius: 4px; border-radius: 4px;
outline: none; outline: none;
@ -130,6 +130,19 @@ a.button {
transition: border-color 0.125s, background-color 0.125s; transition: border-color 0.125s, background-color 0.125s;
} }
form input,
form button,
a.button {
background-color: var(--bg);
}
form div.error {
border-color: rgba(192, 0, 0, 0.75);
text-align: center;
color: #C00000;
padding: 1em;
}
a.button { a.button {
text-decoration: none; text-decoration: none;
text-align: center; text-align: center;
@ -152,15 +165,23 @@ form input::placeholder {
opacity: 1; opacity: 1;
} }
form input:focus, form input[invalid] {
form input:hover, border-color: rgba(192, 0, 0, 0.75);
form button:focus, }
form button:hover,
a.button:focus, form input:not(:disabled):focus,
a.button:hover { form input:not(:disabled):hover,
form button:not(:disabled):focus,
form button:not(:disabled):hover,
a.button:not(:disabled):focus,
a.button:not(:disabled):hover {
border-color: rgba(255, 255, 255, 0.75); border-color: rgba(255, 255, 255, 0.75);
} }
form *:disabled {
cursor: not-allowed;
}
form button:active, form button:active,
a.button:active { a.button:active {
background-color: rgba(224, 224, 224, 0.75); background-color: rgba(224, 224, 224, 0.75);