In this task we will be making an AJAX Request to the an endpoint. The endpoint is a JSON file containing a list of phones.
After that we will render the list of phones from the data received from the endpoint. With that we should have other information, not just the name of the phone anymore.
- Create a
fetchPhones()function - Call the
fetch()function with the constantPHONE_ENDPOINTvariable as an argument.
That is the endpoint where the phones data reside.
- Convert the response to JSON using
res.json()e.g
fetch(PHONE_ENDPOINT)
.then(res => res.json())
.then((response) => {
// we'll get the converted response from here!
})Activate your developer tools in the browser enviroment and locate the
Networktab. You should find theendpointamong the logs there. Click on that log to see the response or payload.
- Log the response variable to the console to see the data structure.
You'll observe that it's an
Objectwith 2 keys. We will be needing just thephoneskey in this task. The value of thephoneskey is anArray.
- Store the response using
PhoneHolder.set()function. - Call the
refreshList()function (same function from the previous task)
You'll notice the list renders but instead of the name, we get
[object Object]. So we need to modify the structure of the List next.
- Locate the
createList()function definition. - Change the
phone_nameparameter to tophoneObjectbecause the parameter represents anObject, no longer aString. - Rename all
phone_namevariable in thecreateList()function tophoneObject.name. That way we should get the name.
You should be getting the phone names now.
I think preloaders are cool. It's a way of telling the user that something's coming. We really need it here. So let's add that feature.
- In the
fetchPhones()function- Before
fetch()function, invoke theshowPreloader()function. - Before the
refreshList()function, invoke thehidePreloader()function.
- Before
That's how we control when we want to show or hide the preloader.
- Try adding the
quantityof thephoneto the List element.
😃 🤔 Please if you are having any issue, don't forget to state them in the issues tab Here