web: fix user detail form not working

This commit is contained in:
Jens Langhammer 2020-12-13 00:03:37 +01:00
parent 1524061480
commit 7e8702a71e

View file

@ -56,6 +56,10 @@ export class SiteShell extends LitElement {
} }
loadContent(): void { loadContent(): void {
const bodySlot = this.querySelector("[slot=body]");
if (!bodySlot) {
return;
}
if (!this._url) { if (!this._url) {
return; return;
} }
@ -74,14 +78,18 @@ export class SiteShell extends LitElement {
throw new SentryIgnoredError("Request failed"); throw new SentryIgnoredError("Request failed");
}) })
.then((r) => r.text()) .then((r) => r.text())
.then((t) => { .then((text) => {
const bodySlot = this.querySelector("[slot=body]"); bodySlot.innerHTML = text;
if (!bodySlot) { this.updateHandlers();
return;
}
bodySlot.innerHTML = t;
}) })
.then(() => { .then(() => {
setTimeout(() => {
this.loading = false;
}, 100);
});
}
updateHandlers(): void {
// Ensure anchors only change the hash // Ensure anchors only change the hash
this.querySelectorAll<HTMLAnchorElement>("a:not(.ak-root-link)").forEach((a) => { this.querySelectorAll<HTMLAnchorElement>("a:not(.ak-root-link)").forEach((a) => {
if (a.href === "") { if (a.href === "") {
@ -103,17 +111,42 @@ export class SiteShell extends LitElement {
}); });
}); });
// Make get forms (search bar) notify us on submit so we can change the hash // Make get forms (search bar) notify us on submit so we can change the hash
this.querySelectorAll("form").forEach((f) => { this.querySelectorAll<HTMLFormElement>("form[method=get]").forEach((form) => {
f.addEventListener("submit", (e) => { form.addEventListener("submit", (e) => {
e.preventDefault(); e.preventDefault();
const formData = new FormData(f); const formData = new FormData(form);
const qs = new URLSearchParams((<any>formData)).toString(); // eslint-disable-line const qs = new URLSearchParams((<any>formData)).toString(); // eslint-disable-line
window.location.hash = `#${this._url}?${qs}`; window.location.hash = `#${this._url}?${qs}`;
}); });
}); });
setTimeout(() => { // Make forms with POST Method have a correct action set
this.loading = false; this.querySelectorAll<HTMLFormElement>("form[method=post]").forEach((form) => {
}, 100); form.addEventListener("submit", (e) => {
e.preventDefault();
const formData = new FormData(form);
fetch(this._url ? this._url : form.action, {
method: form.method,
body: formData,
})
.then((response) => {
return response.text();
})
.then((data) => {
const bodySlot = this.querySelector("[slot=body]");
if (!bodySlot) {
return;
}
bodySlot.innerHTML = data;
this.updateHandlers();
})
.catch((e) => {
showMessage({
level_tag: "error",
message: "Unexpected error"
});
console.log(e);
});
});
}); });
} }