-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
109 lines (94 loc) · 3.49 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
function refreshWeather(response) {
let temperatureElement = document.querySelector("#weather-app-temperature");
let temperature = response.data.temperature.current;
let cityElement = document.querySelector("#city");
let descriptionElement = document.querySelector("#description");
let humidityElement = document.querySelector("#humidity");
let windElement = document.querySelector("#wind-speed");
let timeElement = document.querySelector("#time");
let date = new Date(response.data.time * 1000);
let iconElement = document.querySelector("#icon");
iconElement.innerHTML = `<img src="${response.data.condition.icon_url}"class=class="weather-app-icon"/>`;
date.innerHTML = timeElement.innerHTML = formatDate(date);
windElement.innerHTML = `${response.data.wind.speed}km/h`;
humidityElement.innerHTML = `${response.data.temperature.humidity}%`;
descriptionElement.innerHTML = response.data.condition.description;
cityElement.innerHTML = response.data.city;
temperatureElement.innerHTML = Math.round(temperature);
getForecast(response.data.city);
}
function formatDate(date) {
let minutes = date.getMinutes();
let hours = date.getHours();
let days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
let day = days[date.getDay()];
if (minutes < 10) {
minutes = `0${minutes}`;
}
return `${day} ${hours}:${minutes}`;
}
function searchCity(city) {
let apiKey = "91t68d1a23a3153b0a155oba25d504df";
let apiUrl = `https://api.shecodes.io/weather/v1/current?query=${city}&key=${apiKey}&units=metric`;
axios.get(apiUrl).then(refreshWeather);
}
//create a search engine
function handleSearchSubmit(event) {
event.preventDefault();
let searchInput = document.querySelector("#search-form-input");
searchCity(searchInput.value);
}
function getForecast(city) {
let apiKey = "91t68d1a23a3153b0a155oba25d504df";
let apiUrl = `https://api.shecodes.io/weather/v1/forecast?query=${city}&key=${apiKey}&units=metric`;
axios(apiUrl).then(displayForcast);
}
function displayForcast(response) {
let forecastHtml = "";
console.log(response.data);
let days = ["Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
function formatDay(timestamp) {
let date = new Date(timestamp * 1000);
let days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
return days[date.getDay()];
}
response.data.daily.forEach(function (day, index) {
if (index < 5) {
forecastHtml =
forecastHtml +
`
<div class="weather-forecast-item">
<div class="weather-forecast-date">${formatDay(day.time)}</div>
<div > <img class="weather-forecast-icon"
src="${day.condition.icon_url}"
alt=""
width="36"
/> </div>
<div class="weather-forecast-temperatures">
<span class="weather-forecast-temperature-max">${Math.round(
day.temperature.maximum
)}°</span>
<span class="weather-forecast-temperature-min">${Math.round(
day.temperature.minimum
)}°</span>
</div>
</div>
</div>`;
}
});
let forecastElement = document.querySelector("#forecast");
forecastElement.innerHTML = forecastHtml;
}
//replace the city element with search
let searchFormElement = document.querySelector("#search-form");
searchFormElement.addEventListener("submit", handleSearchSubmit);
searchCity("Cape Town");
displayForcast();