/
script.js
190 lines (135 loc) · 5.24 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
/**
* Internal data structure
* Array stores names of VALID searched cities
*/
var nameHistory = [];
/**
*Global Functions
*/
//isRefresh=true prevents new list item creation
function displayWeatherStats(cityName, isRefresh) {
var generalQueryUrl = "https://api.openweathermap.org/data/2.5/weather?q=" + cityName + "&units=imperial&appid=29bee85b4cd6fced7d450f1d24d41a67";
var fiveDayQueryUrl = "https://api.openweathermap.org/data/2.5/forecast?q=" + cityName + "&units=imperial&appid=29bee85b4cd6fced7d450f1d24d41a67";
$.ajax({
method: "GET",
url: generalQueryUrl,
error: function () {
console.log("404 error do something");
}
}).then(function (response) {
//updates Data only on new query
if (!isRefresh) {
nameHistory.push(cityName);
localStorage.setItem("searched", JSON.stringify(nameHistory));
//call render function based on data
renderSearchList();
}
$("#details-header").text(cityName + " (" + moment().format("MM/D/YYYY") + ")");
var icon = $(`<span><img src=https://openweathermap.org/img/wn/${response.weather[0].icon}.png alt="${response.weather[0].description}" ></img></span>`);
$("#details-header").append(icon);
$("#temp").text("Temperature: " + response.main.temp + " °F");
$("#humidity").text("Humidity: " + response.main.humidity + "%");
$("#wind").text("Wind: " + response.wind.speed + " MPH");
var uvQueryUrl = "https://api.openweathermap.org/data/2.5/uvi?lat=" + response.coord.lat + "&lon=" + response.coord.lon + "&appid=29bee85b4cd6fced7d450f1d24d41a67";
$.ajax({
method: "GET",
url: uvQueryUrl
}).then(function (uvResponse) {
var uvIndex= Number(uvResponse.value);
var span = $(`<span>${uvIndex}</span>`);
if(uvIndex>=8){
span.attr("class", "uv-high uv");
}else if(uvIndex>=3) {
span.attr("class", "uv-med uv");
}else{
span.attr("class", "uv-low uv");
}
$("#uv").text("Uv Index: ");
$("#uv").append(span);
});
});
$.ajax({
method: "GET",
url: fiveDayQueryUrl,
error: function () {
console.log("404 error do something");
}
}).then(function (fiveDayResponse) {
var indexOffset = 0;
var found = false;
//Find Day
for (var j = 0; j < 40 && !found; j++) {
var date = fiveDayResponse.list[j].dt_txt.split(" ")[0];
if (date === moment().add(1, 'days').format("YYYY-MM-D")) {
indexOffset = j;
found = true;
}
}
for (var i = 0; i < 5; i++) {
//each day has 8 datpoints, adding 4 aims for early morning time
var index = (8 * i) + (indexOffset);
var dayDiv = $("#day-" + (i + 1));
if (index >= 40) {
index=39;
}
var date = $("<h5 class='card-title'>" + moment().add(i+1,"day").format("MM/D/YYYY") + "</h5>");
var icon = $(`<img src=" https://openweathermap.org/img/wn/${fiveDayResponse.list[index].weather[0].icon}.png" alt="${fiveDayResponse.list[index].weather[0].description}">`);
var temp = $("<p class='card-text'> Temp: " + fiveDayResponse.list[index].main.temp + "°F</p>");
var humidity = $("<p class='card-text'> Humidity: " + fiveDayResponse.list[index].main.temp.toString().split(".")[0] + "%</p>");
dayDiv.empty();
dayDiv.append(date);
dayDiv.append(icon);
dayDiv.append(temp);
dayDiv.append(humidity);
}
});
};
//renders search list from data
function renderSearchList() {
$("#search-list").empty();
//prevents list from growing infinitly
while (nameHistory.length > 11) {
nameHistory = nameHistory.slice(1);
}
for (var i = 0; i < nameHistory.length; i++) {
var listItem = $("<li>");
var button = $("<button></button>");
button.attr("id", "city-" + i);
button.text(nameHistory[i]);
listItem.append(button);
$("#search-list").prepend(listItem);
}
};
/**
* Event Handlers
*/
$("document").ready(function () {
//select button functionality
$("#search-button").on("click", function (event) {
event.preventDefault();
var cityInput = $("#search-input").val().trim();
$("#search-input").val("");
if (cityInput != "") {
//makes name capital
cityInput = cityInput.charAt(0).toUpperCase() + cityInput.slice(1);
displayWeatherStats(cityInput, false);
}
});
$(".city-button").on("click", function (event) {
var targetIndex = event.target.id.split("-")[1];
displayWeatherStats(nameHistory[targetIndex], true);
});
});
/**
* Main
*/
var tempHistory = JSON.parse(localStorage.getItem("searched"));
if (tempHistory != null) {
nameHistory = tempHistory;
}
renderSearchList();
if (nameHistory.length > 0) {
displayWeatherStats(nameHistory[nameHistory.length - 1], true);
} else {
displayWeatherStats("Cincinnati", true);
}