Files
timetracker/ts/add_purchase.ts
T
lukas 5bb8b92c05 Fix add-form JS: name→sort-name sync and related-game disable
Two bugs in the add forms, both root-caused via the e2e harness:

1. add_game Name → Sort name never synced. syncSelectInputUntilChanged was
   scoped to "form", but the first <form> on every page is the navbar logout
   form — the add-form fields live in a later form, so the delegated listener
   never heard their events. Scope to "#add-form" (the add-form wrapper). Also
   switch the sync from the "change" event to "input" so Sort name mirrors Name
   live as you type, not only on blur.

2. add_purchase Related game not disabled when Type == Game.
   disableElementsWhenTrue set `.disabled` on #id_related_game, which is the
   SearchSelect wrapper <div> (a <div> ignores `disabled`). Target the inner
   [data-search-select-search] input instead, so the widget is actually disabled.

Adds two e2e regression tests (live sync; type-game disables the related-game
search input and re-enables it for other types).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-19 18:11:49 +02:00

70 lines
2.8 KiB
TypeScript

import { disableElementsWhenTrue, onSwap } from "./utils.js";
import type { SearchSelectChangeDetail } from "./search_select.js";
// Switch between a single bundle price and one price per game. The per-game
// inputs are the selection-fields element; this only sets the policy: the
// hidden pricing_mode the view reads, the element's "active" flag, and whether
// the bundle Price field is shown.
function applyPricingMode(separate: boolean): void {
const pricingMode = document.querySelector<HTMLInputElement>("#id_pricing_mode");
if (pricingMode) pricingMode.value = separate ? "per_game" : "combined";
const selectionFields = document.querySelector("selection-fields");
if (selectionFields)
selectionFields.setAttribute("active", separate ? "true" : "false");
const priceInput = document.querySelector<HTMLInputElement>("#id_price");
if (priceInput) {
const wrapper = priceInput.closest("div");
if (wrapper) wrapper.classList.toggle("hidden", separate);
}
}
// The games field is a SearchSelect widget (a <div>, not a <select>), so we
// react to its custom "search-select:change" event instead of syncing a select.
document.addEventListener("search-select:change", (event) => {
const detail = (event as CustomEvent<SearchSelectChangeDetail>).detail;
if (detail.name !== "games") return;
// Auto-fill platform from the clicked option's data-platform.
const last = detail.last;
const platformId = last && last.data ? last.data.platform : "";
if (platformId) {
const platformElement = document.querySelector<HTMLInputElement>("#id_platform");
if (platformElement) platformElement.value = platformId;
}
// The combined/per-game choice is only meaningful with 2+ games. Reveal the
// checkbox there; below the threshold, fall back to a single bundle price.
const separateRow = document.querySelector<HTMLElement>("#separate-prices-row");
const multipleGames = detail.values.length >= 2;
if (separateRow) separateRow.classList.toggle("hidden", !multipleGames);
if (!multipleGames) {
const checkbox = document.querySelector<HTMLInputElement>("#id_separate_prices");
if (checkbox) checkbox.checked = false;
applyPricingMode(false);
}
});
onSwap("#id_separate_prices", (checkbox) => {
checkbox.addEventListener("change", () =>
applyPricingMode((checkbox as HTMLInputElement).checked)
);
});
function setupElementHandlers(): void {
// related_game is a SearchSelect: its #id_related_game wrapper is a <div>
// (ignores `disabled`), so target the inner search <input> instead.
disableElementsWhenTrue("#id_type", "game", [
"#id_name",
"#id_related_game [data-search-select-search]",
]);
}
onSwap("#id_type", (typeSelect) => {
setupElementHandlers();
typeSelect.addEventListener("change", () => {
setupElementHandlers();
});
});