Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alexookah committed Aug 6, 2024
1 parent c44a447 commit 388b136
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions woonuxt_base/app/composables/useCountry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import type { CountriesEnum } from '#gql/default';
import { countries } from '#constants';

export const useCountry = () => {
// State to store allowed countries
const allowedCountries = useState<CountriesEnum[] | null>('allowedCountries', () => null);
const isLoadingAllowedCountries = useState<boolean>('isLoadingAllowedCountries', () => false);


// State to store the countries to be shown - init with static countries
const countriesToShow = useState<GeoLocation[]>('countriesToShow', () => countries);

// State to store states for each country state
const countryStatesDict = useState<{ [code: string]: GeoLocation[] }>('countryStatesDict', () => ({}));
const isLoadingCountryStates = useState<{ [code: string]: boolean }>('isLoadingCountryStates', () => ({}));

// Function to get allowed countries from API
async function getAllowedCountries() {
if (allowedCountries.value || isLoadingAllowedCountries.value) {
return;
Expand All @@ -20,8 +24,15 @@ export const useCountry = () => {
try {
const response = await GqlGetAllowedCountries();
if (response.allowedCountries) {
allowedCountries.value = (response.allowedCountries).filter((country): country is CountriesEnum => country !== null);
countriesToShow.value = countries.filter((country) => allowedCountries.value?.includes(country.code as CountriesEnum))
// Filter out null values and store the result
allowedCountries.value = response.allowedCountries.filter(
(country): country is CountriesEnum => country !== null
);

// Filter countries to show based on allowed countries
countriesToShow.value = countries.filter(
(country) => allowedCountries.value?.includes(country.code as CountriesEnum)
);
}
} catch (error) {
console.error('Failed to retrieve allowed countries', error);
Expand All @@ -30,12 +41,13 @@ export const useCountry = () => {
}
}

// Function to get states for a specific country from API - once
async function getStatesForCountry(countryCode: CountriesEnum) {
if (countryStatesDict.value[countryCode] || isLoadingCountryStates.value[countryCode]) {
return;
}

isLoadingCountryStates.value[countryCode.toString()] = true;
isLoadingCountryStates.value[countryCode] = true;

try {
const { countryStates } = await GqlGetStates({ country: countryCode });
Expand Down

0 comments on commit 388b136

Please sign in to comment.