feat(houseofrc): create a race from a House of RC event and its entries (1.80.0)
This commit is contained in:
1 parent
42df6a786f
commit
9c1f5713e6
18 files changed
+444
-21
No files matched your search
+115
-1
@@ -1,4 +1,4 @@
|
||||
import { planHouseOfRcDriverUpdates, applyHouseOfRcDriverUpdates } from "./houseofrc_logic.js";
|
||||
import { planHouseOfRcDriverUpdates, applyHouseOfRcDriverUpdates, planHouseOfRcEntries, applyHouseOfRcEntries } from "./houseofrc_logic.js";
|
||||
|
||||
// Settings → System → House of RC: save the club's API key on the server and test it.
|
||||
// The key is never read back; the server only tells whether one is set (and its last 4 chars).
|
||||
@@ -150,3 +150,117 @@ export async function openHouseOfRcDriverImport({ state, t, escapeHtml, saveStat
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function openHorModal({ id, title, escapeHtml, wide = true }) {
|
||||
document.getElementById(id)?.remove();
|
||||
const overlay = document.createElement("div");
|
||||
overlay.className = "modal-overlay";
|
||||
overlay.id = id;
|
||||
overlay.innerHTML = `<div class="modal-card ${wide ? "hor-import" : ""}"><div class="panel-header"><h3>🏠 ${escapeHtml(title)}</h3><button class="btn" type="button" data-hor-close>✕</button></div><div class="panel-body" data-hor-body></div></div>`;
|
||||
document.body.append(overlay);
|
||||
const onKey = (event) => { if (event.key === "Escape") close(); };
|
||||
const close = () => { overlay.remove(); document.removeEventListener("keydown", onKey); };
|
||||
document.addEventListener("keydown", onKey);
|
||||
overlay.addEventListener("click", (event) => { if (event.target === overlay || event.target.closest("[data-hor-close]")) close(); });
|
||||
return { overlay, body: overlay.querySelector("[data-hor-body]"), close };
|
||||
}
|
||||
|
||||
// Race wizard → "Fetch from House of RC": pick an event, then one of our classes and the
|
||||
// entered drivers; fills the wizard (name, date, class, participants) and links the event.
|
||||
export function openHouseOfRcEventImport({ state, t, escapeHtml, saveState, makeDriver, onImported }) {
|
||||
const { overlay, body, close } = openHorModal({ id: "houseOfRcEventOverlay", title: t("houseofrc.event_title"), escapeHtml });
|
||||
const day = (offset) => new Date(Date.now() + offset * 864e5).toISOString().slice(0, 10);
|
||||
const fmtDate = (iso) => (iso ? new Date(iso).toLocaleDateString(undefined, { weekday: "short", day: "numeric", month: "short" }) : "");
|
||||
const error = (code, data) => `<p class="error">${escapeHtml(describeHouseOfRcError(t, code, data))}</p>`;
|
||||
|
||||
const showEvents = async (filters = { from: day(0), to: day(60), country: "SE" }) => {
|
||||
body.innerHTML = `
|
||||
<form class="form-grid cols-4" data-hor-filter>
|
||||
<label>${escapeHtml(t("houseofrc.event_from"))}<input type="date" name="from" value="${escapeHtml(filters.from)}" /></label>
|
||||
<label>${escapeHtml(t("houseofrc.event_to"))}<input type="date" name="to" value="${escapeHtml(filters.to)}" /></label>
|
||||
<label>${escapeHtml(t("houseofrc.event_country"))}<input name="country" maxlength="2" value="${escapeHtml(filters.country)}" placeholder="SE" /></label>
|
||||
<div class="actions-inline align-end"><button class="btn" type="submit">${escapeHtml(t("houseofrc.event_search"))}</button></div>
|
||||
</form>
|
||||
<form class="hor-link-row mt-16" data-hor-link>
|
||||
<input name="link" placeholder="${escapeHtml(t("houseofrc.event_link_placeholder"))}" />
|
||||
<button class="btn btn-primary" type="submit">${escapeHtml(t("houseofrc.event_open"))}</button>
|
||||
</form>
|
||||
<div class="mt-16" data-hor-list><p class="hint">${escapeHtml(t("houseofrc.event_loading"))}</p></div>`;
|
||||
body.querySelector("[data-hor-filter]").addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
const form = new FormData(event.currentTarget);
|
||||
showEvents({ from: String(form.get("from") || day(0)), to: String(form.get("to") || day(60)), country: String(form.get("country") || "").trim().toUpperCase() });
|
||||
});
|
||||
body.querySelector("[data-hor-link]").addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
const link = String(new FormData(event.currentTarget).get("link") || "").trim();
|
||||
if (link) showEntries(link);
|
||||
});
|
||||
const list = body.querySelector("[data-hor-list]");
|
||||
const { status: code, body: data } = await postJson("/api/houseofrc/events", { from: `${filters.from}T00:00:00Z`, to: `${filters.to}T23:59:59Z`, country: filters.country });
|
||||
if (!overlay.isConnected || !list.isConnected) return;
|
||||
if (code !== 200 || !data.ok) { list.innerHTML = error(code, data); return; }
|
||||
const events = (data.events || []).filter((event) => event.status !== "cancelled");
|
||||
list.innerHTML = events.length
|
||||
? `<div class="hor-event-list">${events.map((event) => `
|
||||
<button type="button" class="hor-event" data-hor-event="${escapeHtml(String(event.id))}">
|
||||
<strong>${escapeHtml(event.name)}</strong>
|
||||
<small>${escapeHtml([fmtDate(event.start), event.city].filter(Boolean).join(" · "))}</small>
|
||||
</button>`).join("")}</div>`
|
||||
: `<p class="hint">${escapeHtml(t("houseofrc.event_none"))}</p>`;
|
||||
list.querySelectorAll("[data-hor-event]").forEach((button) => button.addEventListener("click", () => showEntries(button.dataset.horEvent)));
|
||||
};
|
||||
|
||||
const showEntries = async (eventRef) => {
|
||||
body.innerHTML = `<p class="hint">${escapeHtml(t("houseofrc.entries_loading"))}</p>`;
|
||||
const { status: code, body: data } = await postJson("/api/houseofrc/entries", { eventId: eventRef });
|
||||
if (!overlay.isConnected) return;
|
||||
const back = `<button class="btn" type="button" data-hor-back>← ${escapeHtml(t("houseofrc.event_back"))}</button>`;
|
||||
const bindBack = () => body.querySelector("[data-hor-back]")?.addEventListener("click", () => showEvents());
|
||||
if (code !== 200 || !data.ok) {
|
||||
body.innerHTML = (code === 502 && data.status === 404 ? `<p class="error">${escapeHtml(t("houseofrc.entries_not_found"))}</p>` : error(code, data)) + back;
|
||||
bindBack();
|
||||
return;
|
||||
}
|
||||
const { event, entries } = data;
|
||||
let classId = state.classes[0]?.id || "";
|
||||
const render = () => {
|
||||
const plan = planHouseOfRcEntries(entries, state.drivers, classId);
|
||||
const statusText = (row) => t(`houseofrc.entry_${row.status}`);
|
||||
body.innerHTML = `
|
||||
<p><strong>${escapeHtml(event.name)}</strong> · ${escapeHtml([fmtDate(event.start), event.city].filter(Boolean).join(" · "))}${event.url ? ` · <a href="${escapeHtml(event.url)}" target="_blank" rel="noopener">House of RC ↗</a>` : ""}</p>
|
||||
${entries.length ? `
|
||||
<label>${escapeHtml(t("houseofrc.entries_class"))}
|
||||
<select data-hor-class>${state.classes.map((item) => `<option value="${escapeHtml(item.id)}" ${item.id === classId ? "selected" : ""}>${escapeHtml(item.name)}</option>`).join("")}</select>
|
||||
<small>${escapeHtml(t("houseofrc.entries_class_hint"))}</small>
|
||||
</label>
|
||||
<div class="table-wrap mt-16"><table class="hor-import-table">
|
||||
<thead><tr><th><input type="checkbox" data-hor-all checked aria-label="${escapeHtml(t("houseofrc.entries_all"))}" /></th><th>${escapeHtml(t("table.name"))}</th><th>${escapeHtml(t("drivers.club"))}</th><th>${escapeHtml(t("houseofrc.import_model"))}</th><th>${escapeHtml(t("table.transponder"))}</th><th></th></tr></thead>
|
||||
<tbody>${plan.map((row, index) => `<tr>
|
||||
<td><input type="checkbox" data-hor-entry="${index}" checked /></td>
|
||||
<td>${escapeHtml(row.entry.name)}${row.entry.number != null ? ` <small>#${escapeHtml(String(row.entry.number))}</small>` : ""}</td>
|
||||
<td>${escapeHtml(row.entry.club || "–")}</td>
|
||||
<td>${escapeHtml(row.entry.car || "–")}</td>
|
||||
<td>${escapeHtml(row.transponder || "–")}</td>
|
||||
<td><span class="pill ${row.status === "new" ? "pill-warn" : "pill-green"}">${escapeHtml(statusText(row))}</span></td>
|
||||
</tr>`).join("")}</tbody>
|
||||
</table></div>
|
||||
<p class="hint">${escapeHtml(t("houseofrc.entries_hint"))}</p>
|
||||
<div class="actions mt-16">${back}<button class="btn btn-primary" type="button" data-hor-apply>${escapeHtml(t("houseofrc.entries_apply"))}</button></div>`
|
||||
: `<p>${escapeHtml(t("houseofrc.entries_empty"))}</p>${back}`}`;
|
||||
bindBack();
|
||||
body.querySelector("[data-hor-class]")?.addEventListener("change", (change) => { classId = change.target.value; render(); });
|
||||
body.querySelector("[data-hor-all]")?.addEventListener("change", (change) => body.querySelectorAll("[data-hor-entry]").forEach((box) => { box.checked = change.target.checked; }));
|
||||
body.querySelector("[data-hor-apply]")?.addEventListener("click", () => {
|
||||
const selected = new Set([...body.querySelectorAll("[data-hor-entry]:checked")].map((box) => Number(box.dataset.horEntry)));
|
||||
const driverIds = applyHouseOfRcEntries(state.drivers, plan, { classId, selected, makeDriver });
|
||||
saveState();
|
||||
close();
|
||||
onImported({ event, classId, driverIds });
|
||||
});
|
||||
};
|
||||
render();
|
||||
};
|
||||
|
||||
showEvents();
|
||||
}
|
||||
Reference in new issue
Block a user