-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
127 lines (116 loc) · 6.46 KB
/
script.js
File metadata and controls
127 lines (116 loc) · 6.46 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
function initPage() {
const inputEl = document.getElementById("city-input");
const searchEl = document.getElementById("search-button");
const clearEl = document.getElementById("clear-history");
const nameEl = document.getElementById("city-name");
const currentPicEl = document.getElementById("current-pic");
const currentTempEl = document.getElementById("temperature");
const currentHumidityEl = document.getElementById("humidity");4
const currentWindEl = document.getElementById("wind-speed");
const currentUVEl = document.getElementById("UV-index");
const historyEl = document.getElementById("history");
let searchHistory = JSON.parse(localStorage.getItem("search")) || [];
console.log(searchHistory);
const APIKey = "e77aba8852ed3846372abd976242255f";
// When search button is clicked, read the city name typed by the user
function getWeather(cityName) {
// Using saved city name, execute a current condition get request from open weather map api
let queryURL = "https://api.openweathermap.org/data/2.5/weather?q=" + cityName + "&appid=" + APIKey;
axios.get(queryURL)
.then(function(response){
console.log(response);
// Parse response to display current conditions
// Method for using "date" objects obtained from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
const currentDate = new Date(response.data.dt*1000);
console.log(currentDate);
const day = currentDate.getDate();
const month = currentDate.getMonth() + 1;
const year = currentDate.getFullYear();
nameEl.innerHTML = response.data.name + " (" + month + "/" + day + "/" + year + ") ";
let weatherPic = response.data.weather[0].icon;
currentPicEl.setAttribute("src","https://openweathermap.org/img/wn/" + weatherPic + "@2x.png");
currentPicEl.setAttribute("alt",response.data.weather[0].description);
currentTempEl.innerHTML = "Temperature: " + k2f(response.data.main.temp) + " °F";
currentHumidityEl.innerHTML = "Humidity: " + response.data.main.humidity + "%";
currentWindEl.innerHTML = "Wind Speed: " + response.data.wind.speed + " MPH";
let lat = response.data.coord.lat;
let lon = response.data.coord.lon;
let UVQueryURL = "https://api.openweathermap.org/data/2.5/uvi/forecast?lat=" + lat + "&lon=" + lon + "&appid=" + APIKey + "&cnt=1";
axios.get(UVQueryURL)
.then(function(response){
let UVIndex = document.createElement("span");
UVIndex.setAttribute("class","badge badge-danger");
UVIndex.innerHTML = response.data[0].value;
currentUVEl.innerHTML = "UV Index: ";
currentUVEl.append(UVIndex);
});
// Using saved city name, execute a 5-day forecast get request from open weather map api
let cityID = response.data.id;
let forecastQueryURL = "https://api.openweathermap.org/data/2.5/forecast?id=" + cityID + "&appid=" + APIKey;
axios.get(forecastQueryURL)
.then(function(response){
// Parse response to display forecast for next 5 days underneath current conditions
console.log(response);
const forecastEls = document.querySelectorAll(".forecast");
for (i=0; i<forecastEls.length; i++) {
forecastEls[i].innerHTML = "";
const forecastIndex = i*8 + 4;
const forecastDate = new Date(response.data.list[forecastIndex].dt * 1000);
const forecastDay = forecastDate.getDate();
const forecastMonth = forecastDate.getMonth() + 1;
const forecastYear = forecastDate.getFullYear();
const forecastDateEl = document.createElement("p");
forecastDateEl.setAttribute("class","mt-3 mb-0 forecast-date");
forecastDateEl.innerHTML = forecastMonth + "/" + forecastDay + "/" + forecastYear;
forecastEls[i].append(forecastDateEl);
const forecastWeatherEl = document.createElement("img");
forecastWeatherEl.setAttribute("src","https://openweathermap.org/img/wn/" + response.data.list[forecastIndex].weather[0].icon + "@2x.png");
forecastWeatherEl.setAttribute("alt",response.data.list[forecastIndex].weather[0].description);
forecastEls[i].append(forecastWeatherEl);
const forecastTempEl = document.createElement("p");
forecastTempEl.innerHTML = "Temp: " + k2f(response.data.list[forecastIndex].main.temp) + " °F";
forecastEls[i].append(forecastTempEl);
const forecastHumidityEl = document.createElement("p");
forecastHumidityEl.innerHTML = "Humidity: " + response.data.list[forecastIndex].main.humidity + "%";
forecastEls[i].append(forecastHumidityEl);
}
})
});
}
searchEl.addEventListener("click",function() {
const searchTerm = inputEl.value;
getWeather(searchTerm);
searchHistory.push(searchTerm);
localStorage.setItem("search",JSON.stringify(searchHistory));
renderSearchHistory();
})
clearEl.addEventListener("click",function() {
searchHistory = [];
renderSearchHistory();
})
function k2f(K) {
return Math.floor((K - 273.15) *1.8 +32);
}
function renderSearchHistory() {
historyEl.innerHTML = "";
for (let i=0; i<searchHistory.length; i++) {
const historyItem = document.createElement("input");
// <input type="text" readonly class="form-control-plaintext" id="staticEmail" value="email@example.com"></input>
historyItem.setAttribute("type","text");
historyItem.setAttribute("readonly",true);
historyItem.setAttribute("class", "form-control d-block bg-white");
historyItem.setAttribute("value", searchHistory[i]);
historyItem.addEventListener("click",function() {
getWeather(historyItem.value);
})
historyEl.append(historyItem);
}
}
renderSearchHistory();
if (searchHistory.length > 0) {
getWeather(searchHistory[searchHistory.length - 1]);
}
// Save user's search requests and display them underneath search form
// When page loads, automatically generate current conditions and 5-day forecast for the last city the user searched for
}
initPage();