Replaced the custom controller for fragment replacements with symfony streams

This does not require a complete new endpoint
This commit is contained in:
Jan Böhmer 2026-02-22 19:14:35 +01:00
parent 05ee3157fb
commit 910ad939df
5 changed files with 65 additions and 198 deletions

View file

@ -30,9 +30,9 @@ export default class extends Controller {
_submitting = false;
_lastDecodedText = "";
_onInfoChange = null;
_onFormSubmit = null;
connect() {
// Prevent double init if connect fires twice
if (this._scanner) return;
@ -45,22 +45,6 @@ export default class extends Controller {
info.addEventListener("change", this._onInfoChange);
}
// Stop camera cleanly before manual form submit (prevents broken camera after reload)
const form = document.getElementById("scan_dialog_form");
if (form) {
this._onFormSubmit = () => {
try {
const p = this._scanner?.clear?.();
if (p && typeof p.then === "function") p.catch(() => {});
} catch (_) {
// ignore
}
};
// capture=true so we run before other handlers / navigation
form.addEventListener("submit", this._onFormSubmit, { capture: true });
}
const isMobile = window.matchMedia("(max-width: 768px)").matches;
//This function ensures, that the qrbox is 70% of the total viewport
@ -94,10 +78,10 @@ export default class extends Controller {
}
disconnect() {
// If we already stopped/cleared before submit, nothing to do.
const scanner = this._scanner;
this._scanner = null;
this._submitting = false;
this._lastDecodedText = "";
// Unbind info-mode change handler (always do this, even if scanner is null)
@ -107,13 +91,6 @@ export default class extends Controller {
}
this._onInfoChange = null;
// remove the onForm submit handler
const form = document.getElementById("scan_dialog_form");
if (form && this._onFormSubmit) {
form.removeEventListener("submit", this._onFormSubmit, { capture: true });
}
this._onFormSubmit = null;
if (!scanner) return;
try {
@ -125,7 +102,7 @@ export default class extends Controller {
}
async onScanSuccess(decodedText) {
onScanSuccess(decodedText) {
if (!decodedText) return;
const normalized = String(decodedText).trim();
@ -134,94 +111,11 @@ export default class extends Controller {
// scan once per barcode
if (normalized === this._lastDecodedText) return;
// If a request/submit is in-flight, ignore scans.
if (this._submitting) return;
// Mark as handled immediately (prevents spam even if callback fires repeatedly)
this._lastDecodedText = normalized;
this._submitting = true;
// Clear previous augmented result immediately to avoid stale info
// lingering when the next scan is not augmented (or is transient/junk).
const el = document.getElementById("scan-augmented-result");
if (el) el.innerHTML = "";
//Put our decoded Text into the input box
const input = document.getElementById("scan_dialog_input");
if (input) input.value = decodedText;
const infoMode = !!document.getElementById("scan_dialog_info_mode")?.checked;
try {
const data = await this.lookup(normalized, infoMode);
// ok:false = transient junk decode; ignore without wiping UI
if (!data || data.ok !== true) {
this._lastDecodedText = ""; // allow retry
return;
}
// If info mode is OFF and part was found -> redirect
if (!infoMode && data.found && data.redirectUrl) {
window.location.assign(data.redirectUrl);
return;
}
// If info mode is OFF and part was NOT found, redirect to create part URL
if (!infoMode && !data.found && data.createUrl) {
window.location.assign(data.createUrl);
return;
}
// Otherwise render returned fragment HTML
if (typeof data.html === "string" && data.html !== "") {
const el = document.getElementById("scan-augmented-result");
if (el) el.innerHTML = data.html;
}
} catch (e) {
console.warn("[barcode_scan] lookup failed", e);
// allow retry on failure
this._lastDecodedText = "";
} finally {
this._submitting = false;
}
}
async lookup(decodedText, infoMode) {
const form = document.getElementById("scan_dialog_form");
if (!form) return { ok: false };
generateCsrfToken(form);
const mode =
document.querySelector('input[name="scan_dialog[mode]"]:checked')?.value ?? "";
const body = new URLSearchParams();
body.set("input", decodedText);
if (mode !== "") body.set("mode", mode);
body.set("info_mode", infoMode ? "1" : "0");
const headers = {
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
...generateCsrfHeaders(form),
};
const url = this.element.dataset.lookupUrl;
if (!url) throw new Error("Missing data-lookup-url on #reader-box");
const resp = await fetch(url, {
method: "POST",
headers,
body: body.toString(),
credentials: "same-origin",
});
if (!resp.ok) {
throw new Error(`lookup failed: HTTP ${resp.status}`);
}
return await resp.json();
document.getElementById('scan_dialog_input').value = decodedText;
//Submit form
document.getElementById('scan_dialog_form').requestSubmit();
}
}