-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
207 lines (180 loc) · 6.51 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
'use strict';
$(document).ready(function () {
//Pulls the current date
let NowMoment = moment().format("l");
//adds days to moment for forecast
let day1 = moment().add(1, "days").format("l");
let day2 = moment().add(2, "days").format("l");
let day3 = moment().add(3, "days").format("l");
let day4 = moment().add(4, "days").format("l");
let day5 = moment().add(5, "days").format("l");
//global variables
let city;
let cities;
//function to load most recently searched city from local storage
function loadMostRecent() {
let lastSearch = localStorage.getItem("mostRecent");
if (lastSearch) {
city = lastSearch;
search();
} else {
city = "Seattle";
search();
}
}
loadMostRecent()
//function to load recently searched cities from local storage
function loadRecentCities() {
let recentCities = JSON.parse(localStorage.getItem("cities"));
if (recentCities) {
cities = recentCities;
} else {
cities = [];
}
}
loadRecentCities()
//event handler for search city button
$("#submit").on("click", (e) => {
e.preventDefault();
getCity();
search();
$("#city-input").val("");
listCities();
});
//function to save searched cities to local storage
function saveToLocalStorage() {
localStorage.setItem("mostRecent", city);
cities.push(city);
localStorage.setItem("cities", JSON.stringify(cities));
}
//function to retrieve user inputted city name
function getCity() {
city = $("#city-input").val();
if (city && cities.includes(city) === false) {
saveToLocalStorage();
return city;
} else if (!city) {
alert("Please enter a valid city");
}
}
// searches the API for the chosen city
function search() {
let queryURL = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=imperial&appid=42d98d76405f5b8038f2ad71187af430";
let coords = [];
$.ajax({
url: queryURL,
method: "GET",
}).then(function (response) {
coords.push(response.coord.lat);
coords.push(response.coord.lon);
let cityName = response.name;
let cityCond = response.weather[0].description.toUpperCase();
let cityTemp = response.main.temp;
let cityHum = response.main.humidity;
let cityWind = response.wind.speed;
let icon = response.weather[0].icon;
$("#icon").html(
`<img src="http://openweathermap.org/img/wn/${icon}@2x.png">`
);
$("#city-name").html(cityName + " " + "(" + NowMoment + ")");
$("#city-cond").text("Current Conditions: " + cityCond);
$("#temp").text("Current Temp (F): " + cityTemp.toFixed(1));
$("#humidity").text("Humidity: " + cityHum + "%");
$("#wind-speed").text("Wind Speed: " + cityWind + "mph");
$("#date1").text(day1);
$("#date2").text(day2);
$("#date3").text(day3);
$("#date4").text(day4);
$("#date5").text(day5);
getUV(response.coord.lat, response.coord.lon);
}).fail(function (){
alert("Could not get data")
});
//Function to get 5-day forecast and UV index and put them on page
function getUV(lat, lon) {
$.ajax({
url: "https://api.openweathermap.org/data/2.5/onecall?lat=" + lat + "&lon=" + lon + "&exclude=minutely,hourly" + "&units=imperial&appid=42d98d76405f5b8038f2ad71187af430",
method: "GET",
}).then(function (response) {
//code to determine UV index severity
let uvIndex = response.current.uvi;
$("#uv-index").text("UV Index:" + " " + uvIndex);
if (uvIndex >= 8) {
$("#uv-index").css("color", "red");
} else if (uvIndex > 4 && uvIndex < 8) {
$("#uv-index").css("color", "yellow");
} else {
$("#uv-index").css("color", "green");
}
let cityHigh = response.daily[0].temp.max;
$("#high").text("Expected high (F): " + " " + cityHigh);
//forecast temp variables
let day1temp = response.daily[1].temp.max;
let day2temp = response.daily[2].temp.max;
let day3temp = response.daily[3].temp.max;
let day4temp = response.daily[4].temp.max;
let day5temp = response.daily[5].temp.max;
//forecast humidity variables
let day1hum = response.daily[1].humidity;
let day2hum = response.daily[2].humidity;
let day3hum = response.daily[3].humidity;
let day4hum = response.daily[4].humidity;
let day5hum = response.daily[5].humidity;
//forecast weather icon variables
let icon1 = response.daily[1].weather[0].icon;
let icon2 = response.daily[2].weather[0].icon;
let icon3 = response.daily[3].weather[0].icon;
let icon4 = response.daily[4].weather[0].icon;
let icon5 = response.daily[5].weather[0].icon;
//
$("#temp1").text("Temp(F):" + " " + day1temp.toFixed(1));
$("#temp2").text("Temp(F):" + " " + day2temp.toFixed(1));
$("#temp3").text("Temp(F):" + " " + day3temp.toFixed(1));
$("#temp4").text("Temp(F):" + " " + day4temp.toFixed(1));
$("#temp5").text("Temp(F):" + " " + day5temp.toFixed(1));
$("#hum1").text("Hum:" + " " + day1hum + "%");
$("#hum2").text("Hum:" + " " + day2hum + "%");
$("#hum3").text("Hum:" + " " + day3hum + "%");
$("#hum4").text("Hum:" + " " + day4hum + "%");
$("#hum5").text("Hum:" + " " + day5hum + "%");
$("#icon1").html(
`<img src="http://openweathermap.org/img/wn/${icon1}@2x.png">`
);
$("#icon2").html(
`<img src="http://openweathermap.org/img/wn/${icon2}@2x.png">`
);
$("#icon3").html(
`<img src="http://openweathermap.org/img/wn/${icon3}@2x.png">`
);
$("#icon4").html(
`<img src="http://openweathermap.org/img/wn/${icon4}@2x.png">`
);
$("#icon5").html(
`<img src="http://openweathermap.org/img/wn/${icon5}@2x.png">`
);
});
}
}
//function to render recently searched cities to page
function listCities() {
$("#cityList").text("");
cities.forEach((city) => {
$("#cityList").prepend("<tr><td>" + city + "</td></tr>");
});
}
listCities();
//event handler for recently searched cities in table
$(document).on("click", "td", (e) => {
e.preventDefault();
let listedCity = $(e.target).text();
city = listedCity;
search();
});
//event handler for clear button
$("#clr-btn").click(() => {
localStorage.removeItem("cities");
loadRecentCities();
listCities();
});
});
//End of line