-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
48 lines (42 loc) · 1.64 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//Variables
// API data from Open Weather Map
var APIKey = "4fb00fc4e626764db50e2ea3b5fe5a50";
var queryURL = "https://api.openweathermap.org/data/2.5/forecast";
var city;
// Weather data Document Object Model - DOM
var button = document.querySelector('.search-bar');
var weather = document.querySelector('.weather');
var form = document.querySelector('.input.search-bar');
// Fetch Weather from Requested City
function getWeather(inputValue) {
fetch(https://api.openweathermap.org/data/2.5/forecast?q=${inputValue}&appid=${APIKey})
.then(response => response.json())
.then(data => {
console.log(data);
})
.catch(err => alert("Weather Not Found. Please Search Again."));
}
document.querySelector(".search-bar").addEventListener("click", function () {
var inputValue = document.querySelector(".input").value;
getWeather(inputValue);
});
// Display Weather Results
form.addEventListener('submit', function(e) {
e.preventDefault();
var inputValue = document.querySelector(".input").value;
fetch(https://api.openweathermap.org/data/2.5/forecast?q=${inputValue}&appid=${APIKey}&units=metric)
.then(response => response.json())
.then(data => {
console.log(data);
// call a function to display weather data on the DOM here
})
.catch(err => alert("Weather Not Found. Please Search Again."))
});
// Weather icons from open weather map
var iconWeather = https://openweathermap.org/img/w/${weather.weather[0].icon}.png;
// Store Data to Local Storage
var searchHistory = JSON.parse(localStorage.getItem('search_history')) || [];
if (searchHistory.indexOf(inputValue) === -1) {
searchHistory.push(inputValue);
window.localStorage.setItem("search_history", JSON.stringify(searchHistory));
}