fix errors while selecting country, region and city

This commit is contained in:
Nuwan 2024-03-11 22:23:12 +05:30
parent 95008f49e6
commit 17b0ea6111
1 changed files with 39 additions and 9 deletions

View File

@ -198,6 +198,7 @@ function JKEditProfile() {
})
.then(data => {
setRegions(data.regions);
skipRegionChange.current = false;
})
.catch(error => console.log(error));
};
@ -322,17 +323,20 @@ function JKEditProfile() {
regionRef.current.select.clearValue();
setCities([]);
cityRef.current.select.clearValue();
fetchRegions(country.value);
fetchRegions(country);
forceUpdate();
handleChange();
};
const handleRegionChange = selectedOpt => {
console.log("region selectedOpt", selectedOpt)
if (!selectedOpt) return;
if (skipRegionChange.current) {
skipRegionChange.current = false;
return;
}
const state = selectedOpt.value;
const country = getValues('country');
setValue('state', state);
@ -470,13 +474,24 @@ function JKEditProfile() {
control={control}
render={({ field: { onChange, value } }) => {
const country = countries.find(country => country.countrycode === value);
if (!country) {
return (
<Select
data-testid="countrySelect"
onChange={handleCountryChange}
options={countries.map(c => {
return { value: c.countrycode, label: c.countryname };
})}
/>
);
}
return (
<Select
data-testid="countrySelect"
value={{ value: country.countrycode, label: country.countryname }}
onChange={handleCountryChange}
options={countries.map(country => {
return { value: country.countrycode, label: country.countryname };
options={countries.map(c => {
return { value: c.countrycode, label: c.countryname };
})}
/>
);
@ -494,16 +509,30 @@ function JKEditProfile() {
control={control}
render={({ field: { onChange, value } }) => {
const region = regions.find(region => region.region === value);
if (region) {
return (
<Select
value={{ value: region?.region, label: region?.region }}
isDisabled={getValues('country') === null || regions.length === 0}
value={{ value: region.region, label: region.region }}
ref={regionRef}
onChange={handleRegionChange}
options={regions.map(region => {
return { value: region?.region, label: region?.region };
options={regions.map(r => {
return { value: r.region, label: r.region };
})}
/>
);
}else{
return (
<Select
isDisabled={getValues('country') === null || regions.length === 0}
ref={regionRef}
onChange={handleRegionChange}
options={regions.map(r => {
return { value: r.region, label: r.region };
})}
/>
);
}
}}
/>
@ -517,11 +546,12 @@ function JKEditProfile() {
control={control}
render={({ field: { onChange, value } }) => (
<Select
isDisabled={getValues('region') === null || cities.length === 0}
value={{ value: value, label: value }}
ref={cityRef}
onChange={handleCityChange}
options={cities.map(city => {
return { value: city, label: city };
options={cities.map(c => {
return { value: c, label: c };
})}
/>
)}
@ -539,7 +569,7 @@ function JKEditProfile() {
data-testid="biography"
style={{ height: 200 }}
type="textarea"
value={value}
value={value ? value : ''}
onChange={e => {
onChange(e);
handleTextInputChage();