Forkify Web app (part 2 – fetching data and presenting it)

forkify-part2 download

The url to fetch data is https://forkify-api.herokuapp.com/api

We get GET data like so:
https://forkify-api.herokuapp.com/api/search?q=pizza
https://forkify-api.herokuapp.com/api//get?rId=47746

src/js/models/Search.js

We use Axios library for data fetching:

1) We await the Promise object to be done
2) then we extract the status to make sure its all OK
3) we finally log the result

The default http grammar is GET. axios(…) automatically use this if we only give it an url.

We will be using this Search’s getResults in the view. However, we need to set up utility functionality first.

src/js/views/base.js

This is just a way to get the DOM element of a certain class or id. Typically, we use document.querySelector, and then we store it in a literal object like so:

Once we get the DOM element, we can use textContent, innerText, innerHTML to get the content.

src/js/views/searchView.js

The whole point to this searchView is to create a renderResults function that our controller can use.
We implement a renderResults function that outputs HTML for rending the content. We render the content. It takes in an recipe object and then uses the object’s properties along with the HTML. We also shorten the incoming titles to the length we want by using reduce.

Other purpose is to clear textfields and such:

– clearResults
– clearInputs

We first import the base. Then we simply access value, or innerHTML, according to what we want. From there we can clear certain HTML controls.