fix the countrystatecity api issue. react-jsx-dev-runtime.development.js:87 Warning: Each child in a list should have a unique "key" prop.
Check the render method of OrganizationSignup. See https://reactjs.org/link/warning-keys for more information.
at option
at OrganizationSignup (http://localhost:3000/static/js/bundle.js:1129:84)
at RenderedRoute (http://localhost:3000/static/js/bundle.js:44200:5)
at Routes (http://localhost:3000/static/js/bundle.js:44891:5)
at App
at Router (http://localhost:3000/static/js/bundle.js:44825:15)
at BrowserRouter (http://localhost:3000/static/js/bundle.js:428: /* eslint-disable no-undef /
/ eslint-disable no-unused-vars */
import React, { useState, useEffect } from 'react';
import { useNavigate, Outlet } from 'react-router-dom';
import { toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import './OrganizationSignup.css';
import AdminInfo from '../admin-info/AdminInfo';
const config = {
cUrl: 'https://api.countrystatecity.in/v1/countries',
ckey: 'dDIycENUZ1NGWEJDaFBVQU4weGg1UUxzVlF2bWJ4dlk5WE5uVjRhTQ==',
};
const OrganizationSignup = () => {
const [countries, setCountries] = useState([]);
const [states, setStates] = useState([]);
const [cities, setCities] = useState([]);
const [selectedCountry, setSelectedCountry] = useState('');
const [selectedState, setSelectedState] = useState('');
const [selectedCity, setSelectedCity] = useState('');
const [organizationName, setOrganizationName] = useState('');
const [orgEmailAddress, setOrganizationEmail] = useState('');
const [regNo, setRegNo] = useState('');
const [address, setAddress] = useState('');
useEffect(() => {
fetchCountries();
}, []);
const handleValidation = () => {
if (!organizationName || !orgEmailAddress || !regNo || !address) {
toast.error('Please fill in all required fields.', {
position: toast.POSITION.TOP_RIGHT,
});
}
};
const handleSubmit = (e) => {
e.preventDefault();
handleValidation();
const organisation_signup = {
name: organizationName,
orgEmailAddress,
regNo,
address,
country: selectedCountry,
state: selectedState,
city: selectedCity,
};
fetch('http://159.89.10.175:8083/api/v1/organizations/create', {
method: 'POST',
headers: { 'Content-type': 'application/json' },
body: JSON.stringify(organisation_signup),
})
.then((res) => res.json())
.then((data) => {
if (data.success) {
toast.success('Registered successfully');
} else {
toast.error('Failed: ' + data.message);
}
})
.catch((err) => {
toast.error('Failed: ' + err.message);
});
};
const fetchCountries = async () => {
try {
const response = await fetch(config.cUrl, {
headers: {
'X-CSCAPI-KEY': config.ckey,
},
});
const data = await response.json();
const dataArray = Object.values(data);
setCountries(dataArray);
setStates([]);
setCities([]);
setSelectedState('');
setSelectedCity('');
} catch (err) {
console.error('Error fetching countries', err);
}
};
const changeCountry = async (e) => {
setSelectedCountry(e.target.value);
setSelectedState('');
setSelectedCity('');
loadStates();
try {
const response = await fetch(${config.cUrl}/${e.target.value}/states, {
headers: {
'X-CSCAPI-KEY': config.ckey,
},
});
const data = await response.json();
setStates(Object.values(data));
setCities([]);
setSelectedCity('');
} catch (error) {
console.error('Error loading states:', error);
}
};
const changeState = async (e) => {
setSelectedState(e.target.value);
setSelectedCity('');
loadCities();
try {
const response = await fetch(
${config.cUrl}/${selectedCountry}/states/${e.target.value}/cities,
{
headers: {
'X-CSCAPI-KEY': config.ckey,
},
}
);
const data = await response.json();
setCities(Object.values(data));
} catch (error) {
console.error('Error loading states:', error);
}
};
const changeCity = async (e) => {
setSelectedCity(e.target.value);
};
const loadStates = async () => {
try {
const response = await fetch(${config.cUrl}/${selectedCountry}/states, {
headers: {
'X-CSCAPI-KEY': config.ckey,
},
});
const data = await response.json();
const statesArray = Array.isArray(data) ? data : Object.values(data);
setStates(statesArray);
setCities([]);
setSelectedCity('');
} catch (error) {
console.error('Error loading states:', error);
}
};
const loadCities = async () => {
try {
const response = await fetch(
${config.cUrl}/${selectedCountry}/states/${selectedCity}/cities,
{
headers: {
'X-CSCAPI-KEY': config.ckey,
},
}
);
const data = await response.json();
const citiesArray = Array.isArray(data) ? data : Object.values(data);
setCities(citiesArray);
} catch (error) {
console.error('Error loading cities:', error);
}
};
return (
Sign-up an Organization
Enter details to create your organization
<div className="input-group">
<label htmlFor="name-org" />
<input
type="text"
id="name-org"
name="name-org"
placeholder="Name of organization"
onChange={(e) => setOrganizationName(e.target.value)}
/>
</div>
<div className="input-group">
<label htmlFor="email-org" />
<input
type="text"
id="email-org"
name="email-org"
placeholder="Organization email"
onChange={(e) => setOrganizationEmail(e.target.value)}
/>
</div>
<div className="input-group">
<label htmlFor="reg-no" />
<input
type="number"
id="reg-no"
name="reg-no"
placeholder="Reg. No"
onChange={(e) => setRegNo(e.target.value)}
/>
</div>
<div className="location-container">
<div className="location">
<select
className="form-select country"
value={selectedCountry}
onChange={changeCountry}
>
<option value="">Select Country</option>
{countries.map((country) => (
<option key={country.iso2} value={country.iso2}>
{country.name}
</option>
))}
</select>
</div>
<div className="location">
<select
value={selectedState}
onChange={changeState}
disabled={!selectedCountry}
>
<option value="">Select State</option>
{states.map((state) => (
<option key={state.iso2} value={state.iso2}>
{state.name}
</option>
))}
</select>
</div>
<div className="location">
<select
value={selectedCity}
onChange={changeCity}
disabled={!selectedState}
>
<option value="">Select City</option>
{cities.map((city) => (
<option key={city.iso2} value={city.iso2}>
{city.name}
</option>
))}
</select>
</div>
</div>
<div className="input-group">
<label htmlFor="address" />
<input
type="text"
id="address"
name="address"
placeholder="Address"
onChange={(e) => setAddress(e.target.value)}
/>
</div>
<nav>
<button className="submit-button" type="submit" onClick={handleSubmit}>
Submit
</button>
</nav>
</div>
<div className="split-right"></div>
</div>
);
};
export default OrganizationSignup;