Skip to content

Commit

Permalink
fix dropdown logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Cordiviola committed Feb 26, 2025
1 parent 5d95f27 commit f9921de
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
8 changes: 6 additions & 2 deletions www/assets/js/country-list/country-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

((window) => {
class CountrySelector {
constructor(selector, subdivisionSelector, isoCompliantJsonBlob) {
constructor(selector, subdivisionSelector, isoCompliantJsonBlob, initialCountry, initialState) {
this.countryList = isoCompliantJsonBlob;
this.countries = Object.keys(this.countryList).map((code) => {
return {
Expand All @@ -22,9 +22,12 @@

this.countrySelector = selector;
this.subdivisionSelector = subdivisionSelector;
this.countrySelector.value = initialCountry;

// Fill in subdivisions
this.fillSubdivision(this.countries[0].code);
this.fillSubdivision(initialCountry ?? this.countries[0].code);

this.subdivisionSelector.value = initialState;

// Attach listener
this.countrySelector.addEventListener("change", (e) => {
Expand All @@ -47,6 +50,7 @@
}

fillSubdivision(countryCode) {
this.subdivisionSelector.value = undefined;
const divisions = this.getSubdivisions(countryCode);
const opts = Object.keys(divisions).map((key) => {
return new Option(divisions[key], key);
Expand Down
31 changes: 8 additions & 23 deletions www/templates/account/billing/update-payment-confirm-address.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@
<label for="state">State</label>
<div>
<select autocomplete="off" name="state" data-country-selector="state-selector" required>

<?php foreach ($state_list as $state) : ?>
<option value="<?= $state['code'] ?>" <?php if ($state['code'] == $state_code) {
echo 'selected';
} ?>>
<?php foreach ($state_list as $state): ?>
<option value="<?= $state['code'] ?>">
<?= $state['name']; ?>
</option>
<?php endforeach; ?>
Expand All @@ -45,21 +42,8 @@
<div class="info-container country">
<label for="country">Country</label>
<select autocomplete="off" name="country" data-country-selector="selector" required>
<?php foreach ($country_list as $country) : ?>
<?php
$current_code = $country["code"];
$selected_country = false;
if (isset($country_code) && !is_null($country_code)) {
if ($current_code == $country_code) {
$selected_country = true;
}
} else {
if ($current_code == 'US') {
$selected_country = true;
}
}
?>
<option value="<?= $country["code"] ?>" <?= $selected_country ? 'selected' : '' ?>>
<?php foreach ($country_list as $country): ?>
<option value="<?= $country["code"] ?>">
<?= $country["name"]; ?>
</option>
<?php endforeach; ?>
Expand All @@ -83,18 +67,19 @@
<script>
(() => {
const countryList = <?= $country_list_json_blob ?>;
const initialCountry = "<?= $country_code ?>";
const initialState = "<?= $state_code ?>";
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", () => {
const countrySelectorEl = document.querySelector("[data-country-selector=selector]");
const divisionSelectorEl = document.querySelector("[data-country-selector=state-selector]");

new CountrySelector(countrySelectorEl, divisionSelectorEl, countryList);
new CountrySelector(countrySelectorEl, divisionSelectorEl, countryList, initialCountry, initialState);
});
} else {
const countrySelectorEl = document.querySelector("[data-country-selector=selector]");
const divisionSelectorEl = document.querySelector("[data-country-selector=state-selector]");

new CountrySelector(countrySelectorEl, divisionSelectorEl, countryList);
new CountrySelector(countrySelectorEl, divisionSelectorEl, countryList, initialCountry, initialState);
}
})();
</script>
Expand Down

0 comments on commit f9921de

Please sign in to comment.