feat(filters): panel removal + All-Books field-values fix (faceted plan Phase 8)
- Remove the Phase 7 facet-panel UI: SSR route, facet_panel.templ, FacetCountsForView, drawer panel block, panel TS + .facet-* CSS. The ENGINE stays (FacetCountsUnderFilters, all-facets endpoint, drift guard) — it becomes the Phase 9 builder's machinery. - Fix the All-Books bug: field-values autocomplete (author/genre/ tags/narrator/series/language) treats library_id as OPTIONAL — absent aggregates every VISIBLE library (family lens in-query); explicit out-of-set library_id is now 403, never a silent pass. The four scalar Search*Values queries get the nullable shape. - toast.ts: suggestion fetches are exempt from error toasts (console-only) — a failed background hint never interrupts. - TestFacetPanelPartial deleted with the behavior; TestFieldValuesAll- Libraries pins the fix (200 + cross-library aggregation, family lens, 403). Contract-pins changelog entry added.
This commit is contained in:
1 parent
f52bb6f073
commit
ebb7f4a5b0
14 files changed
+192
-643
No files matched your search
@@ -94,7 +94,6 @@ Alpine.data("bookshelf", () => ({
|
||||
// Standalone function references (don't access component state)
|
||||
clearFilters,
|
||||
initBookshelf,
|
||||
loadFacetPanel,
|
||||
|
||||
// Inline method - opens the modal
|
||||
showSaveFilterModal() {
|
||||
@@ -659,74 +658,6 @@ function refreshFilterChips(): void {
|
||||
);
|
||||
}
|
||||
|
||||
// splitFacetValues is the client twin of services.SplitFacetValues: a
|
||||
// comma-joined filter value is a multi-select (OR within the facet,
|
||||
// AND across facets).
|
||||
function splitFacetValues(wireValue: string): string[] {
|
||||
return wireValue
|
||||
.split(",")
|
||||
.map((v) => v.trim())
|
||||
.filter((v) => v !== "");
|
||||
}
|
||||
|
||||
// loadFacetPanel fetches the drawer's facet-value partial with the
|
||||
// CURRENT filter query string (faceted plan Phase 7): the values are
|
||||
// server-rendered by templ (templates/facet_panel.templ), and their
|
||||
// counts are computed under the active filters by the all-facets query
|
||||
// — one round trip for every facet at once.
|
||||
async function loadFacetPanel(): Promise<void> {
|
||||
const target = document.getElementById("facet-panel");
|
||||
const filterForm = document.getElementById(
|
||||
"filter-form",
|
||||
) as HTMLFormElement | null;
|
||||
if (!target || !filterForm) return;
|
||||
// Serialize the form's named fields (all inputs/selects — no
|
||||
// multi-selects or file inputs here) — typed directly, since the
|
||||
// project's tsconfig lib lacks DOM.Iterable (FormData.entries()).
|
||||
const pairs: string[][] = [];
|
||||
filterForm
|
||||
.querySelectorAll<HTMLInputElement | HTMLSelectElement>("input, select")
|
||||
.forEach((el) => {
|
||||
if (el.name) pairs.push([el.name, el.value]);
|
||||
});
|
||||
const qs = new URLSearchParams(pairs).toString();
|
||||
await window.htmx.ajax("GET", `/media-items/facet-panel?${qs}`, {
|
||||
target: "#facet-panel",
|
||||
swap: "innerHTML",
|
||||
});
|
||||
}
|
||||
|
||||
// toggleFacetValue applies one panel click: the value joins/leaves its
|
||||
// facet's comma-joined filter. The facet's input may be a typed drawer
|
||||
// field (set its value) or absent entirely — deep-link-only facets get
|
||||
// a hidden input, exactly like applyURLFilters does, so chips and
|
||||
// clearing keep working.
|
||||
function toggleFacetValue(param: string, value: string): void {
|
||||
const filterForm = document.getElementById(
|
||||
"filter-form",
|
||||
) as HTMLFormElement | null;
|
||||
if (!filterForm) return;
|
||||
let input = filterForm.querySelector(
|
||||
`[name="${param}"]`,
|
||||
) as HTMLInputElement | null;
|
||||
if (!input) {
|
||||
const hidden = document.createElement("input");
|
||||
hidden.type = "hidden";
|
||||
hidden.name = param;
|
||||
filterForm.appendChild(hidden);
|
||||
input = hidden;
|
||||
}
|
||||
const values = splitFacetValues(input.value);
|
||||
const idx = values.indexOf(value);
|
||||
if (idx >= 0) {
|
||||
values.splice(idx, 1);
|
||||
} else {
|
||||
values.push(value);
|
||||
}
|
||||
input.value = values.join(",");
|
||||
window.htmx.trigger(filterForm, "submit");
|
||||
}
|
||||
|
||||
function initBookshelf(): void {
|
||||
const filterForm = document.getElementById(
|
||||
"filter-form",
|
||||
@@ -737,31 +668,8 @@ function initBookshelf(): void {
|
||||
// filters, so one bubbled htmx:afterRequest hook is the sync point.
|
||||
filterForm.addEventListener("htmx:afterRequest", () => {
|
||||
refreshFilterChips();
|
||||
// Facet counts must track the applied filters: refetch the panel
|
||||
// while the drawer is open (the panel's own htmx.ajax requests
|
||||
// dispatch on body, so this form-scoped hook never loops).
|
||||
const bookshelfElem = document.querySelector(
|
||||
'[x-data*="bookshelf"]',
|
||||
) as any;
|
||||
const data = bookshelfElem?._x_dataStack?.[0];
|
||||
if (data?.filtersOpen) {
|
||||
void loadFacetPanel();
|
||||
}
|
||||
});
|
||||
|
||||
// Panel value clicks: one delegated listener for every section's
|
||||
// buttons (the partial re-renders on each refetch).
|
||||
const facetPanel = document.getElementById("facet-panel");
|
||||
if (facetPanel) {
|
||||
facetPanel.addEventListener("click", (ev) => {
|
||||
const btn = (ev.target as HTMLElement).closest<HTMLElement>(
|
||||
".facet-value-btn",
|
||||
);
|
||||
if (!btn || !btn.dataset.param || !btn.dataset.value) return;
|
||||
toggleFacetValue(btn.dataset.param, btn.dataset.value);
|
||||
});
|
||||
}
|
||||
|
||||
// A2c §5.3: deep-linkable filters — /bookshelf?author_filter=Sapkowski
|
||||
// pre-fills the form and applies it to the first grid load. Author
|
||||
// links on the detail page (and anything else) can share filtered
|
||||
|
||||
+26
-3
@@ -176,6 +176,18 @@ const setupHTMXListeners = (): void => {
|
||||
});
|
||||
};
|
||||
|
||||
// Suggestion fetches — field-value autocomplete (author=, genre=, …)
|
||||
// on the search endpoint — are background hints for typed inputs. A
|
||||
// failed hint must never interrupt the user with a toast: it logs to
|
||||
// the console only. The grid's real search (q=/sort= params) is NOT a
|
||||
// suggestion fetch and keeps its toasts.
|
||||
const suggestionFieldRe = /[?&](author|genre|tags|narrator|series|language)=/;
|
||||
function isSuggestionFetch(input: Parameters<typeof fetch>[0]): boolean {
|
||||
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
|
||||
if (!url.includes("/api/media-items/search?")) return false;
|
||||
return suggestionFieldRe.test(url) && !/[?&](q|sort)=/.test(url);
|
||||
}
|
||||
|
||||
// Setup fetch interceptor
|
||||
const setupFetchInterceptor = (): void => {
|
||||
const originalFetch = window.fetch;
|
||||
@@ -209,17 +221,28 @@ const setupFetchInterceptor = (): void => {
|
||||
return response;
|
||||
}
|
||||
|
||||
// Handle other errors
|
||||
// Handle other errors. Suggestion fetches are exempt (console
|
||||
// only) — a failed background hint never interrupts.
|
||||
if (!response.ok) {
|
||||
const errorMessage = await parseFetchError(response);
|
||||
showToast(errorMessage, "error");
|
||||
if (isSuggestionFetch(args[0])) {
|
||||
console.warn(
|
||||
`Suggestion fetch failed (${response.status}): ${errorMessage}`,
|
||||
);
|
||||
} else {
|
||||
showToast(errorMessage, "error");
|
||||
}
|
||||
}
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
// Don't show toast for redirect errors
|
||||
if ((error as Error).message !== "Session expired") {
|
||||
showToast("Network error: Unable to connect to server", "error");
|
||||
if (isSuggestionFetch(args[0])) {
|
||||
console.warn("Suggestion fetch failed: network error", error);
|
||||
} else {
|
||||
showToast("Network error: Unable to connect to server", "error");
|
||||
}
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user