Skip to content

Commit 1253153

Browse files
committed
Add URL param preselect support
- Merge rebuild and URL param flow into initPreSelect - Use URLSearchParams consistently - Rename rebuildConfig to preSelectConfig - Unify vehicle/version/board error handling - Remove out-of-scope changes
1 parent 3d4a2ff commit 1253153

File tree

1 file changed

+94
-65
lines changed

1 file changed

+94
-65
lines changed

web/static/js/add_build.js

Lines changed: 94 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ const Features = (() => {
297297

298298
var init_categories_expanded = false;
299299

300-
var rebuildConfig = {
300+
var preSelectConfig = {
301301
vehicleId: null,
302302
versionId: null,
303303
boardId: null,
@@ -306,46 +306,71 @@ var rebuildConfig = {
306306
};
307307

308308
async function init() {
309-
if (typeof rebuildFromBuildId !== 'undefined') {
310-
await initRebuild(rebuildFromBuildId);
311-
}
312-
313-
fetchVehicles();
309+
await initPreSelect();
310+
fetchVehicles();
314311
}
315312

316-
async function initRebuild(buildId) {
313+
async function initPreSelect() {
314+
const params = new URLSearchParams(window.location.search);
315+
316+
if (params.has("rebuild_from")) {
317+
const rebuildId = params.get("rebuild_from");
318+
317319
try {
318-
const buildResponse = await fetch(`/api/v1/builds/${buildId}`);
319-
if (!buildResponse.ok) {
320-
throw new Error('Failed to fetch build details');
321-
}
322-
const buildData = await buildResponse.json();
323-
324-
if (!buildData.vehicle || !buildData.vehicle.id) {
325-
throw new Error('Vehicle information is missing from the build');
326-
}
327-
if (!buildData.version || !buildData.version.id) {
328-
throw new Error('Version information is missing from the build');
329-
}
330-
if (!buildData.board || !buildData.board.id) {
331-
throw new Error('Board information is missing from the build');
332-
}
333-
334-
rebuildConfig.vehicleId = buildData.vehicle.id;
335-
rebuildConfig.versionId = buildData.version.id;
336-
rebuildConfig.boardId = buildData.board.id;
337-
rebuildConfig.selectedFeatures = buildData.selected_features || [];
338-
rebuildConfig.isRebuildMode = true;
339-
340-
} catch (error) {
341-
console.error('Error loading rebuild configuration:', error);
342-
alert('Failed to load build configuration: ' + error.message + '\n\nRedirecting to new build page...');
343-
window.location.href = '/add_build';
344-
throw error;
320+
const res = await fetch(`/api/v1/builds/${rebuildId}`);
321+
if (!res.ok) throw new Error("Failed to fetch build");
322+
323+
const data = await res.json();
324+
325+
if (!data.vehicle || !data.vehicle.id) {
326+
throw new Error("Vehicle information is missing from the build");
327+
}
328+
329+
if (!data.version || !data.version.id) {
330+
throw new Error("Version information is missing from the build");
331+
}
332+
333+
if (!data.board || !data.board.id) {
334+
throw new Error("Board information is missing from the build");
335+
}
336+
337+
preSelectConfig.vehicleId = data.vehicle.id;
338+
preSelectConfig.versionId = data.version.id;
339+
preSelectConfig.boardId = data.board.id;
340+
preSelectConfig.selectedFeatures = data.selected_features || [];
341+
preSelectConfig.isRebuildMode = true;
342+
343+
} catch (err) {
344+
console.error("Error loading rebuild config:", err);
345+
alert(
346+
"Failed to load build configuration: " +
347+
err.message +
348+
"\n\nRedirecting to new build page..."
349+
);
350+
window.location.href = "/add_build";
351+
throw err;
345352
}
353+
354+
return;
355+
}
356+
357+
358+
if (params.has("vehicle_id")) {
359+
preSelectConfig.vehicleId = params.get("vehicle_id");
360+
}
361+
362+
if (params.has("version_id")) {
363+
preSelectConfig.versionId = params.get("version_id");
364+
}
365+
366+
if (params.has("board_id")) {
367+
preSelectConfig.boardId = params.get("board_id");
368+
}
346369
}
347370

348-
function applyRebuildFeatures(featuresList) {
371+
372+
373+
function applyPreSelectedFeatures(featuresList) {
349374
Features.checkUncheckAll(false);
350375

351376
if (featuresList && featuresList.length > 0) {
@@ -355,12 +380,12 @@ function applyRebuildFeatures(featuresList) {
355380
}
356381
}
357382

358-
function clearRebuildConfig() {
359-
rebuildConfig.vehicleId = null;
360-
rebuildConfig.versionId = null;
361-
rebuildConfig.boardId = null;
362-
rebuildConfig.selectedFeatures = [];
363-
rebuildConfig.isRebuildMode = false;
383+
function clearPreSelectConfig() {
384+
preSelectConfig.vehicleId = null;
385+
preSelectConfig.versionId = null;
386+
preSelectConfig.boardId = null;
387+
preSelectConfig.selectedFeatures = [];
388+
preSelectConfig.isRebuildMode = false;
364389
}
365390

366391
// enables or disables the elements with ids passed as an array
@@ -397,17 +422,18 @@ function fetchVehicles() {
397422
.then((json_response) => {
398423
let all_vehicles = json_response;
399424

400-
if (rebuildConfig.vehicleId) {
401-
const vehicleExists = all_vehicles.some(v => v.id === rebuildConfig.vehicleId);
425+
if (preSelectConfig.vehicleId) {
426+
const vehicleExists = all_vehicles.some(v => v.id === preSelectConfig.vehicleId);
402427
if (!vehicleExists) {
403-
console.warn(`Rebuild vehicle '${rebuildConfig.vehicleId}' not found in available vehicles`);
404-
alert(`Warning: The vehicle from the original build is no longer available.\n\nRedirecting to new build page...`);
428+
const msg = `Vehicle '${preSelectConfig.vehicleId}' is no longer listed for building. Redirecting to new build page...`;
429+
console.warn(msg);
430+
alert(msg);
405431
window.location.href = '/add_build';
406432
return;
407433
}
408434
}
409435

410-
let new_vehicle = rebuildConfig.vehicleId ||
436+
let new_vehicle = preSelectConfig.vehicleId ||
411437
(all_vehicles.find(vehicle => vehicle.name === "Copter") ? "copter" : all_vehicles[0].id);
412438
updateVehicles(all_vehicles, new_vehicle);
413439
})
@@ -439,17 +465,19 @@ function onVehicleChange(new_vehicle_id) {
439465
let all_versions = json_response;
440466
all_versions = sortVersions(all_versions);
441467

442-
if (rebuildConfig.versionId) {
443-
const versionExists = all_versions.some(v => v.id === rebuildConfig.versionId);
444-
if (!versionExists) {
445-
console.warn(`Rebuild version '${rebuildConfig.versionId}' not found for vehicle '${new_vehicle_id}'`);
446-
alert(`Warning: The version from the original build is no longer available.\n\nRedirecting to new build page...`);
447-
window.location.href = '/add_build';
448-
return;
449-
}
468+
if (preSelectConfig.versionId) {
469+
const versionExists = all_versions.some(v => v.id === preSelectConfig.versionId);
470+
if (!versionExists) {
471+
const msg = `Version '${preSelectConfig.versionId}' is no longer listed for building. Redirecting to new build page...`;
472+
console.warn(msg);
473+
alert(msg);
474+
window.location.href = '/add_build';
475+
return;
476+
}
477+
450478
}
451479

452-
const new_version = rebuildConfig.versionId || all_versions[0].id;
480+
const new_version = preSelectConfig.versionId || all_versions[0].id;
453481
updateVersions(all_versions, new_version);
454482
})
455483
.catch((message) => {
@@ -495,17 +523,18 @@ function onVersionChange(new_version) {
495523
// Keep full board objects with id and name
496524
let boards = boards_response;
497525

498-
if (rebuildConfig.boardId) {
499-
const boardExists = boards.some(b => b.id === rebuildConfig.boardId);
526+
if (preSelectConfig.boardId) {
527+
const boardExists = boards.some(b => b.id === preSelectConfig.boardId);
500528
if (!boardExists) {
501-
console.warn(`Rebuild board '${rebuildConfig.boardId}' not found for version '${version_id}'`);
502-
alert(`Warning: The board from the original build is no longer available.\n\nRedirecting to new build page...`);
529+
const msg = `Board '${preSelectConfig.boardId}' is no longer listed for building. Redirecting to new build page...`;
530+
console.warn(msg);
531+
alert(msg);
503532
window.location.href = '/add_build';
504533
return;
505-
}
534+
}
535+
506536
}
507-
508-
let new_board = rebuildConfig.boardId || (boards.length > 0 ? boards[0].id : null);
537+
let new_board = preSelectConfig.boardId || (boards.length > 0 ? boards[0].id : null);
509538
updateBoards(boards, new_board);
510539
})
511540
.catch((message) => {
@@ -545,9 +574,9 @@ function onBoardChange(new_board) {
545574
fillBuildOptions(features_response);
546575

547576
// TODO: Refactor to use a single method to apply both rebuild and default features
548-
if (rebuildConfig.isRebuildMode) {
549-
applyRebuildFeatures(rebuildConfig.selectedFeatures);
550-
clearRebuildConfig();
577+
if (preSelectConfig.isRebuildMode) {
578+
applyPreSelectedFeatures(preSelectConfig.selectedFeatures);
579+
clearPreSelectConfig();
551580
} else {
552581
Features.applyDefaults();
553582
}

0 commit comments

Comments
 (0)