-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
145 lines (121 loc) · 4.49 KB
/
app.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
function formatDate(timestamp) {
let date = new Date(timestamp);
let days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
let day = days[date.getDay()];
let americanTime = date.toLocaleString("en-US", {
hour: "numeric",
minute: "numeric",
hour12: true,
});
return `${day} ${americanTime}`;
}
function formatForecastDay(timestamp) {
let date = new Date(timestamp * 1000);
let day = date.getDay();
let days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
return days[day];
}
function getForecast(coordinates) {
console.log(coordinates);
let apiKey = "045ace03oteb7d0da03b1286fde00d59";
let units = "imperial";
let apiUrl = `https://api.shecodes.io/weather/v1/forecast?lon=${coordinates.longitude}&lat=${coordinates.latitude}&key=${apiKey}&units=${units}`;
console.log(apiUrl);
axios.get(apiUrl).then(displayForecast);
}
function displayTemperature(response) {
console.log(response.data);
let cityElement = document.querySelector("#city");
cityElement.innerHTML = response.data.city;
let dateElement = document.querySelector("#date");
dateElement.innerHTML = formatDate(response.data.time * 1000);
let mainTempElement = document.querySelector("#main-temp");
mainTempElement.innerHTML = Math.round(response.data.temperature.current);
let descriptionElement = document.querySelector("#description");
descriptionElement.innerHTML = response.data.condition.description;
let mainIconElement = document.querySelector("#main-icon");
mainIconElement.setAttribute(
"src",
`http://shecodes-assets.s3.amazonaws.com/api/weather/icons/${response.data.condition.icon}.png`
);
mainIconElement.setAttribute("alt", response.data.condition.description);
let humidityElement = document.querySelector("#humidity");
humidityElement.innerHTML = response.data.temperature.humidity;
let windElement = document.querySelector("#wind");
windElement.innerHTML = Math.round(response.data.wind.speed / 1.609);
getForecast(response.data.coordinates);
}
function displayForecast(response) {
console.log(response.data.daily);
let forecast = response.data.daily;
let forecastElement = document.querySelector("#forecast");
let forecastHTML = `<div class="row">`;
forecast.slice(1).forEach(function (forecastDay, index) {
if (index < 6) {
forecastHTML =
forecastHTML +
`<div class="col-2">
<div class="forecast-date">${formatForecastDay(
forecastDay.time
)}</div>
<img
src="http://shecodes-assets.s3.amazonaws.com/api/weather/icons/${
forecastDay.condition.icon
}.png"
alt=""
width="42"
/>
<div class="forecast-temp">
<span class="forecast-temp-max">${Math.round(
forecastDay.temperature.maximum
)}°/</span>
<span class="forecast-temp-min">${Math.round(
forecastDay.temperature.minimum
)}°</span>
</div>
</div>
`;
}
});
forecastHTML = forecastHTML + `</div>`;
forecastElement.innerHTML = forecastHTML;
}
function search(city) {
let apiEndpoint = "https://api.shecodes.io/weather/v1/current?";
let apiKey = "045ace03oteb7d0da03b1286fde00d59";
let units = "imperial";
let apiUrl = `${apiEndpoint}query=${city}&key=${apiKey}&units=${units}`;
axios.get(apiUrl).then(displayTemperature);
}
function handleSubmit(event) {
event.preventDefault();
let cityInputElement = document.querySelector("#city-input");
search(cityInputElement.value);
}
let form = document.querySelector("#search-form");
form.addEventListener("submit", handleSubmit);
function getPositionWeather(position) {
console.log(position);
let lat = position.coords.latitude;
let lon = position.coords.longitude;
let apiEndpoint = "https://api.shecodes.io/weather/v1/current?";
let apiKey = "045ace03oteb7d0da03b1286fde00d59";
let units = "imperial";
let apiUrl = `${apiEndpoint}lon=${lon}&lat=${lat}&key=${apiKey}&units=${units}`;
axios.get(apiUrl).then(displayTemperature, displayForecast);
}
function getPosition(event) {
event.preventDefault();
navigator.geolocation.getCurrentPosition(getPositionWeather);
}
let locationButton = document.querySelector("#location-btn");
locationButton.addEventListener("click", getPosition);
search("Brooklyn");