Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

REST Countries API with vanilla JS

@lukassiarny

Desktop design screenshot for the REST Countries API with color theme switcher coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
  • API
4advanced
View challenge

Design comparison


SolutionDesign

Solution retrospective


Hi,

I'm currently learning JS and wanted to practice fetch calls, so I decided to try this challenge. It would be better to do it with some kind of framework, since there should be a route for every single country, but I don't know any yet. So It's just a single page app with pure JS re-rendering the content again and again.

I wanted to make countries lazy loading with Intersection Observer, but couldn't figure out how to fetch just certain amount of countries from API. So they show up all at once on one looong page.

Any feedback will be appreciated. Thanks.

Community feedback

Devdgehog 80

@devdgehog

Posted

Hello, I haven't tried this challenge yet but I checked the API. Indeed, it's imposible to fetch countries by pages.

I recommend you to store your countries fetched by the all query in an array. By doing that you will be able to use that array instead of making queries for regions and search, using filter for example. The only queries left will be all and the detail ones.

You can still benefit using Intersection Observer to load images only when the country is visible instead of loading all of them.

I feel like you should at least bind your search function to the enter key on your search input (or onsubmit if you wrap it into a form). You can even bind the search function to onchange for a highly reactive search (if you do this you definitively need the stored array)

You can format numbers using toLocaleString() or toLocaleString("en-US") if you want to force commas.

Your listener to change z-indexes is useless you can directly set the z-index to 2 as you correctly handle the transition with opacity.

Have a nice day. 🦔

1

@lukassiarny

Posted

Thank you for your time and comments!

I actually started with workflow you suggest at first - I stored all countries in array and then worked with that data. But since I wanted to practice fetch calls and work with asynchronous JS a bit I decided to change that.

So is it better to make just one call and then work with stored data? It would definitely help with search and filtering as you said. Because the way how it works now is that when you search something and there is more than one result and you want to filter it by region, it actually shows all countries in that region instead of filtering the result.. so I'll try to look at this.

Enter key is a great idea, I completely forgot about that!

I didn't know how to handle z-index problem just with CSS, because my transition lasts 250ms.. and when I set z-index in CSS to non-checked state, it sets it immediately, so you can see how menu is closing, but behind the country cards, not in front of them. That's why I used JS and setTimeout function, to set z-index after the transition ends. But if you can suggest me a way how to do it just with css, I'll be glad.

0
Devdgehog 80

@devdgehog

Posted

@lukassiarny Yes, in this use case, it will be better. You can set your array in the resolution of your query for all countries.

For your dropdown, I have the same results using .countries { /*...*/ z-index: 2 } and removing JS code that modifies zIndexes (in the selectByRegion function and the whole selectByRegionZindex function).

I also noticed your "All regions" dropdown option is broken, you call it with an undefined object, it shows an error and your result is empty.

1
P
ApplePieGiraffe 30,545

@ApplePieGiraffe

Posted

Hey, lukassiarny! 👋

Good work! 👍

It looks like devdgehog already gave you some feedback on your JS and how to work with APIs, so I'll just say overall your page still looks good and works well! 🙌

I only suggest,

  • Perhaps adding a hover state to the options of the drop-down menu on the right of the page for a better user experience.

Keep coding (and happy coding, too)! 😁

0

@lukassiarny

Posted

@ApplePieGiraffe Thanks for your reply! I'll definitely add some hover states to right menu... and also on buttons. Good point.

0

Please log in to post a comment

Log in with GitHub
Discord logo

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord