|
// retoor <retoor@molodetz.nl>
|
|
|
|
import { Http } from "./Http.js";
|
|
|
|
export class ReportDialog {
|
|
constructor() {
|
|
this.overlay = document.getElementById("report-dialog");
|
|
this.form = document.getElementById("report-form");
|
|
this.target = null;
|
|
document.addEventListener("click", (e) => {
|
|
const btn = e.target.closest("[data-report-type]");
|
|
if (!btn || btn.disabled) return;
|
|
e.preventDefault();
|
|
this.open(btn.dataset.reportType, btn.dataset.reportUid);
|
|
});
|
|
if (this.form) {
|
|
this.form.addEventListener("submit", (e) => {
|
|
e.preventDefault();
|
|
e.stopImmediatePropagation();
|
|
this.submit();
|
|
});
|
|
}
|
|
}
|
|
|
|
open(targetType, targetUid) {
|
|
if (!this.overlay || !this.form) return;
|
|
this.target = { targetType, targetUid };
|
|
this.form.reset();
|
|
this.overlay.classList.add("visible");
|
|
const reason = this.form.querySelector("[name='reason']");
|
|
if (reason) reason.focus();
|
|
}
|
|
|
|
close() {
|
|
if (this.overlay) this.overlay.classList.remove("visible");
|
|
this.target = null;
|
|
}
|
|
|
|
async submit() {
|
|
if (!this.target) return;
|
|
const data = new FormData(this.form);
|
|
const button = this.form.querySelector("button[type='submit']");
|
|
if (button) button.disabled = true;
|
|
try {
|
|
const result = await Http.sendForm(
|
|
`/reports/${this.target.targetType}/${this.target.targetUid}`,
|
|
{ reason: data.get("reason"), detail: data.get("detail") || "" },
|
|
{ silent: true },
|
|
);
|
|
const hours = result && result.data ? result.data.sla_hours : null;
|
|
this.close();
|
|
this.notify(
|
|
hours
|
|
? `Report received. A moderator reviews it within ${hours} hours.`
|
|
: "Report received.",
|
|
"success",
|
|
);
|
|
} catch (err) {
|
|
this.notify(err.message, "error");
|
|
} finally {
|
|
if (button) button.disabled = false;
|
|
}
|
|
}
|
|
|
|
notify(message, type) {
|
|
if (window.app && window.app.toast) {
|
|
window.app.toast.show(message, { type });
|
|
return;
|
|
}
|
|
console.info(message);
|
|
}
|
|
}
|
|
|
|
window.ReportDialog = ReportDialog;
|