diff --git a/index.html b/index.html new file mode 100644 index 0000000..e626235 --- /dev/null +++ b/index.html @@ -0,0 +1,114 @@ + + + + + + + + Responsive News Reader + + + + + +
+ +
+ + + +
+ +
+ +
+
+ +
+ +
+ + + +
+ + + +
+ + + + + \ No newline at end of file diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..3e67465 --- /dev/null +++ b/src/index.js @@ -0,0 +1,48 @@ +function submitHandler(event) { + event.preventDefault(); + + // Capture search term + const getTopic = document.querySelector("#topicSearchBar"); + const topicToSearch = getTopic.value; + + const country = document.querySelector("#countries"); + const countryToSearch = country.value; + + // Get news information + + const url = `https://newsapi.org/v2/top-headlines?country=${countryToSearch}&q=${topicToSearch}&apiKey=814db06ca1814aeca4562dbafab25378`; + + fetch(url) + .then(function(response){ + return response.json(); + }).then(function(data){ + + console.log(data); + + const articlesLength = data.articles.length; + + if (articlesLength > 0) { + + const results = data.articles.map(article => + `

${article.title}

${article.source.name}

${article.description}

Author: ${article.author}

+ ` + ).join(''); + getTopic.value = ''; + + const resultsEl = document.querySelector('#articlesBody'); + + resultsEl.innerHTML = results; + } + + else { + alert('Hmm, slow news day. Maybe search for something else.'); + } + + }).catch(function(error){ + alert('Sorry, I couldn\'t find anything.'); + }); +} + +const getSubmit = document.querySelector("#searchTopic"); + +getSubmit.addEventListener("submit", submitHandler); \ No newline at end of file