Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/stores/CountriesProvider/CountriesProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@ export const CountriesProvider: React.FC<{

const searchParams = new URLSearchParams(window.location.search);
const urlCountryIso = searchParams.get("country");
const storedCountryIso = localStorage.getItem("selectedCountry");

const urlCountry = allCountries.find(
(country) => country.iso_alpha2 === urlCountryIso
);
const storedCountry = allCountries.find(
(country) => country.iso_alpha2 === storedCountryIso
);

// Priority: URL param > localStorage > config > default US
const defaultCountry =
urlCountry && buildUrl
(urlCountry && buildUrl)
? urlCountry
: storedCountry
? storedCountry
: configCountry
? allCountries.find((country) => country.iso_alpha2 === configCountry)
: allCountries.find((country) => country.iso_alpha2 === "US");
Expand All @@ -60,6 +67,7 @@ export const CountriesProvider: React.FC<{
const country = availableCountries.find((c) => c.iso_alpha2 === isoCode);
if (country) {
setSelectedCountry(country);
localStorage.setItem("selectedCountry", isoCode);

// Update URL with selected country
if (buildUrl) {
Expand Down