Skip to content

Commit e8ff76f

Browse files
committed
Code review changes
1 parent d244f99 commit e8ff76f

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/components/SubProcessorsForm.astro

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,17 @@
371371
return urlParams.get(name);
372372
}
373373

374+
// Helper function to safely find input by name and value
375+
function findInputByValue(name, value) {
376+
const inputs = form.querySelectorAll(`input[name="${name}"]`);
377+
for (const input of inputs) {
378+
if (input.value === value) {
379+
return input;
380+
}
381+
}
382+
return null;
383+
}
384+
374385
// Function to update URL with current selections
375386
function updateUrl() {
376387
const dataRegionInputs = form.querySelectorAll('input[name="dataRegion"]:checked');
@@ -383,19 +394,29 @@
383394
const emailServices = emailServicesInputs.length > 0 ? emailServicesInputs[0].value : '';
384395
const smsServices = smsServicesInputs.length > 0 ? smsServicesInputs[0].value : '';
385396

386-
const urlParams = new URLSearchParams();
397+
// Start with existing URL parameters to preserve other query params
398+
const urlParams = new URLSearchParams(window.location.search);
387399

400+
// Update only the parameters this form controls
388401
if (dataRegion) {
389402
urlParams.set('data-region', dataRegion.toLowerCase().replace(/\s+/g, '-'));
403+
} else {
404+
urlParams.delete('data-region');
390405
}
391406
if (services.length > 0) {
392407
urlParams.set('services', services.join(','));
408+
} else {
409+
urlParams.delete('services');
393410
}
394411
if (emailServices) {
395412
urlParams.set('email-services', emailServices.toLowerCase().replace(/\s+/g, '-'));
413+
} else {
414+
urlParams.delete('email-services');
396415
}
397416
if (smsServices) {
398417
urlParams.set('sms-services', smsServices.toLowerCase().replace(/\s+/g, '-'));
418+
} else {
419+
urlParams.delete('sms-services');
399420
}
400421

401422
const newUrl = window.location.pathname + (urlParams.toString() ? '?' + urlParams.toString() : '');
@@ -411,7 +432,7 @@
411432

412433
if (dataRegion) {
413434
const dataRegionValue = dataRegion.replace(/-/g, ' ').replace(/\b\w/g, l => l.toUpperCase());
414-
const dataRegionInput = form.querySelector(`input[name="dataRegion"][value="${dataRegionValue}"]`);
435+
const dataRegionInput = findInputByValue('dataRegion', dataRegionValue);
415436
if (dataRegionInput) {
416437
dataRegionInput.checked = true;
417438
}
@@ -421,7 +442,7 @@
421442
const serviceArray = services.toLowerCase().split(',');
422443
serviceArray.forEach(service => {
423444
const serviceValue = service.charAt(0).toUpperCase() + service.slice(1);
424-
const serviceInput = form.querySelector(`input[name="services"][value="${serviceValue}"]`);
445+
const serviceInput = findInputByValue('services', serviceValue);
425446
if (serviceInput) {
426447
serviceInput.checked = true;
427448
}
@@ -437,7 +458,7 @@
437458
} else {
438459
emailServicesValue = emailServices.replace(/-/g, ' ').replace(/\b\w/g, l => l.toUpperCase());
439460
}
440-
const emailServicesInput = form.querySelector(`input[name="emailServices"][value="${emailServicesValue}"]`);
461+
const emailServicesInput = findInputByValue('emailServices', emailServicesValue);
441462
if (emailServicesInput) {
442463
emailServicesInput.checked = true;
443464
}
@@ -452,7 +473,7 @@
452473
} else {
453474
smsServicesValue = smsServices.replace(/-/g, ' ').replace(/\b\w/g, l => l.toUpperCase());
454475
}
455-
const smsServicesInput = form.querySelector(`input[name="smsServices"][value="${smsServicesValue}"]`);
476+
const smsServicesInput = findInputByValue('smsServices', smsServicesValue);
456477
if (smsServicesInput) {
457478
smsServicesInput.checked = true;
458479
}

0 commit comments

Comments
 (0)