Skip to content

Commit

Permalink
add loading indicator in countries list
Browse files Browse the repository at this point in the history
  • Loading branch information
eloicasamayor committed Dec 24, 2024
1 parent a8ea83d commit bde92c4
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/pages/PointCountryInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export function PointCountryInfo() {
};
const [countryInfo, setCountryInfo] = useState<countryInfoT | undefined>();
const [countryList, setCountryList] = useState<Country[] | undefined>();
const [loadingList, setLoadingList] = useState<boolean>();

useEffect(() => {
if (!country) {
Expand All @@ -81,12 +82,14 @@ export function PointCountryInfo() {
}, [country]);

useEffect(() => {
setLoadingList(true);
fetch(`https://restcountries.com/v3.1/all`)
.then((response) => response.json())
// 4. Setting *dogImage* to the image url that we received from the response above
.then((data) => {
setCountryList(data);
});
})
.then(() => setLoadingList(false));
}, []);

function onClickPais(e: MouseEvent) {
Expand Down Expand Up @@ -124,7 +127,9 @@ export function PointCountryInfo() {
"no info :/"
)}
</div>
{countryList && (
{loadingList ? (
<p>{"loading list..."}</p>
) : countryList ? (
<div id="country-list">
<ul>
{countryList.map((country) => (
Expand All @@ -134,6 +139,8 @@ export function PointCountryInfo() {
))}
</ul>
</div>
) : (
<p>{"Couldn't load the countries list :/"}</p>
)}
</div>
);
Expand Down

0 comments on commit bde92c4

Please sign in to comment.