fix(settings): make data imports real buttons and confirm the full import

The JSON imports were bare file inputs next to the export buttons and
didn't read as imports. They are now buttons like the CSV imports; the
full import asks before it replaces all race data.
This commit is contained in:
larssand committed 2026-10-23 08:05:57 +02:00
1 parent f4dc3bb5bd
commit f51aa0ba37
4 files changed
+50 -5

No files matched your search

+3 -1
View File
@@ -6,7 +6,9 @@ The application version is maintained in `package.json` and displayed in the adm
## Unreleased
No changes recorded yet.
### Changed
- **Import is a button next to each export** under Settings → System & backup: **Import all race data** and **Import drivers/classes/cars**, like the CSV imports. They used to be bare "Choose file" fields that didn't read as an import. The full import now asks before it replaces all race data (the backup is still taken first), and the same file can be picked again.
## 1.42.0 - 2026-10-22
+2
View File
@@ -2276,6 +2276,7 @@ const TRANSLATIONS = {
"settings.clear_training_confirm": "Rensa all träningsstatistik, mål, belöningar och publika profillänkar? Detta kan inte ångras.",
"settings.clear_all_confirm": "Rensa alla klasser, förare, bilar, race, sessioner, resultat och all träningsstatistik? Systeminställningar behålls. Detta kan inte ångras.",
"settings.clear_success": "Valda data har rensats.",
"settings.import_full_confirm": "Importera \"{name}\"?\n\nAll nuvarande race-data (klasser, förare, bilar, race, sessioner och resultat) ersätts med filens innehåll. En backup tas först och finns under SQLite-backuper på servern.",
"settings.import_full_success": "Race-data importerad och ersatte nuvarande tävlingsdata.",
"settings.import_directory_success": "Klasser, förare och bilar importerades in i befintlig data.",
"settings.import_failed": "Import misslyckades: {msg}",
@@ -4114,6 +4115,7 @@ const TRANSLATIONS = {
"settings.clear_training_confirm": "Clear all training statistics, goals, rewards, and public profile links? This cannot be undone.",
"settings.clear_all_confirm": "Clear all classes, drivers, cars, races, sessions, results, and training statistics? System settings are retained. This cannot be undone.",
"settings.clear_success": "The selected data has been cleared.",
"settings.import_full_confirm": "Import \"{name}\"?\n\nAll current race data (classes, drivers, cars, races, sessions and results) is replaced with the file's contents. A backup is taken first and is listed under Server SQLite backups.",
"settings.import_full_success": "Race data imported and replaced the current competition data.",
"settings.import_directory_success": "Classes, drivers and cars were imported into the existing data.",
"settings.import_failed": "Import failed: {msg}",
+17 -4
View File
@@ -525,14 +525,16 @@ export function renderSettings(deps) {
<p class="hint">${t("settings.storage_note_full")}</p>
<div class="actions">
<button id="exportData" class="btn" type="button">${t("settings.export_all_data")}</button>
<input id="importData" type="file" accept="application/json" />
<button id="selectImportData" class="btn" type="button">${t("settings.import_all_data")}</button>
<input id="importData" type="file" accept="application/json,.json" hidden />
</div>
</div>
<div class="mt-16">
<p class="hint">${t("settings.storage_note_directory")}</p>
<div class="actions">
<button id="exportDirectoryData" class="btn" type="button">${t("settings.export_directory")}</button>
<input id="importDirectoryData" type="file" accept="application/json" />
<button id="selectImportDirectoryData" class="btn" type="button">${t("settings.import_directory")}</button>
<input id="importDirectoryData" type="file" accept="application/json,.json" hidden />
</div>
</div>
<div class="mt-16">
@@ -918,18 +920,28 @@ Erik Eriksson;654321;4WD;Associated;;</code></pre>
downloadJsonFile("live_rc_full_export.json", payload);
});
// The file inputs are hidden behind real buttons: a bare "Choose file" didn't read as an import.
document.getElementById("selectImportData")?.addEventListener("click", () => document.getElementById("importData")?.click());
document.getElementById("selectImportDirectoryData")?.addEventListener("click", () => document.getElementById("importDirectoryData")?.click());
document.getElementById("importData")?.addEventListener("change", (event) => {
const input = event.currentTarget;
const file = input instanceof HTMLInputElement ? input.files?.[0] : null;
if (!file) {
return;
}
// A full import replaces every race, session and result - ask first (a safety backup is
// still taken). Clearing the input lets the same file be picked again.
if (!window.confirm(t("settings.import_full_confirm", { name: file.name }))) {
input.value = "";
return;
}
const reader = new FileReader();
reader.onload = async () => {
try {
const parsed = JSON.parse(String(reader.result || "{}"));
const completed = await runWithSafetyBackup(() => importDataPayload(parsed, "full"));
if (!completed && input instanceof HTMLInputElement) input.value = "";
await runWithSafetyBackup(() => importDataPayload(parsed, "full"));
if (input instanceof HTMLInputElement) input.value = "";
} catch (error) {
settingsStorageNotice = t("settings.import_failed", { msg: error instanceof Error ? error.message : String(error) });
settingsStorageNoticeIsError = true;
@@ -995,6 +1007,7 @@ Erik Eriksson;654321;4WD;Associated;;</code></pre>
try {
const parsed = JSON.parse(String(reader.result || "{}"));
importDataPayload(parsed, "directory");
if (input instanceof HTMLInputElement) input.value = "";
} catch (error) {
settingsStorageNotice = t("settings.import_failed", { msg: error instanceof Error ? error.message : String(error) });
settingsStorageNoticeIsError = true;
+28
View File
@@ -66,6 +66,34 @@ export async function runSettingsTabsSmoke({ evaluate, navigate, waitFor, savedS
await evaluate(`(() => { document.querySelector('[name="raceAudioPbWarmup"]').value = "10"; document.querySelector('[name="raceAudioPbCooldown"]').value = "60"; document.querySelector('[name="raceAudioPbMinGain"]').value = "0.1"; document.getElementById("settingsForm").requestSubmit(); })()`);
console.log("PASS the practice personal best call rules default to 10 laps / 60 s / 0.1 s and are saved");
// Import is a real button next to each export (a bare "Choose file" didn't read as an import).
await evaluate(`document.querySelector('[data-settings-tab="system"]').click()`);
await waitFor(async () => evaluate(`Boolean(document.getElementById("selectImportData"))`), "System & backup shown");
assert.equal(await evaluate(`document.getElementById("importData").hidden && document.getElementById("importDirectoryData").hidden`), true, "the file inputs are hidden");
assert.equal(await evaluate(`document.getElementById("selectImportData").textContent.trim()`), "Import all race data");
assert.equal(await evaluate(`document.getElementById("selectImportDirectoryData").textContent.trim()`), "Import drivers/classes/cars");
const pickFile = (inputId, name, payload) => evaluate(`(() => {
const input = document.getElementById(${JSON.stringify(inputId)});
const transfer = new DataTransfer();
transfer.items.add(new File([${JSON.stringify(JSON.stringify(payload))}], ${JSON.stringify(name)}, { type: "application/json" }));
input.files = transfer.files;
input.dispatchEvent(new Event("change", { bubbles: true }));
})()`);
assert.equal(await evaluate(`(() => { let opened = false; const input = document.getElementById("importDirectoryData"); input.click = () => { opened = true; }; document.getElementById("selectImportDirectoryData").click(); delete input.click; return opened; })()`), true, "the button opens the file picker");
await pickFile("importDirectoryData", "directory.json", { classes: [{ id: "imp_class", name: "IMP Class" }], drivers: [{ id: "imp_driver", name: "IMP Driver", classId: "imp_class" }], cars: [] });
await waitFor(async () => (await savedState()).drivers.some((driver) => driver.id === "imp_driver"), "drivers/classes/cars imported");
assert.ok((await savedState()).sessions.length > 0, "the directory import merges - races and sessions stay");
// The full import replaces everything, so it asks first; declining changes nothing.
const sessionsBefore = (await savedState()).sessions.length;
await evaluate(`window.__confirms = []; window.__realConfirm = window.confirm; window.confirm = (message) => { window.__confirms.push(message); return false; }`);
await pickFile("importData", "full_export.json", { classes: [], drivers: [], cars: [], events: [], sessions: [] });
assert.match((await evaluate(`window.__confirms`))[0] || "", /full_export\.json[\s\S]*replaced/, "the confirm names the file and says it replaces");
assert.equal(await evaluate(`document.getElementById("importData").value`), "", "the picker is cleared so the same file can be picked again");
await new Promise((resolve) => setTimeout(resolve, 600));
assert.equal((await savedState()).sessions.length, sessionsBefore, "declining keeps everything");
await evaluate(`window.confirm = window.__realConfirm`);
console.log("PASS import is a button next to each export, and the full import asks before it replaces anything");
// The sidebar "update available" link opens Settings on System & backup at Updates, even
// when another tab was the last one used.
await evaluate(`document.querySelector('[data-settings-tab="connection"]').click()`);