-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
202 lines (178 loc) · 7.14 KB
/
index.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
function getDayName(dateString) {
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var date = new Date(dateString);
return days[date.getDay()];
}
let weather = {
apiKey: "670a5a75c62f443e711f0d34f2e18bf8",
fetchWeather: function (city) {
fetch(
"https://api.openweathermap.org/data/2.5/weather?q=" +
city +
"&units=metric&appid=" +
this.apiKey
)
.then((response) => {
if (!response.ok) {
alert("No weather found.");
throw new Error("No weather found.");
}
return response.json();
})
.then((data) => {
const { lon, lat } = data.coord;
this.displayCurrentWeather(data);
this.fetchUVIndex(lon, lat);
this.fetchForecastWeather(city);
});
},
//API cari UVI
fetchUVIndex: function (lon, lat) {
fetch(
`https://api.openweathermap.org/data/2.5/uvi?lat=${lat}&lon=${lon}&appid=${this.apiKey}`
)
.then((response) => {
if (!response.ok) {
alert("Failed to fetch UV index.");
throw new Error("Failed to fetch UV index.");
}
return response.json();
})
.then((data) => {
this.displayUVIndex(data);
});
},
//API mencari data 5 hari kedepan output dalam 3 jam
fetchForecast: function (lon, lat) {
fetch(
`https://api.openweathermap.org/data/2.5/forecast?lat=${lat}&lon=${lon}&appid=${this.apiKey}`
)
.then((response) => {
if (!response.ok) {
alert("Failed to fetch forecast data.");
throw new Error("Failed to fetch forecast data.");
}
return response.json();
})
.then((data) => {
this.showEventDayWeather(data);
});
},
displayCurrentWeather: function (data) {
const { name } = data;
const { icon, description } = data.weather[0];
const { temp, humidity, pressure } = data.main;
const { speed } = data.wind;
document.getElementById("city").innerText = name;
document.getElementById("temp").innerText = temp + "°C";
document.getElementById("humidity").innerText = humidity + "%";
document.getElementById("wind").innerText = speed + " km/h";
document.getElementById("description").innerText = description;
document.getElementById("pressure").innerText = pressure + "mb";
if (temp > 32) {
document.getElementById("weather-condition").innerText = "Cuaca sangat panas";
} else if (temp < 20) {
document.getElementById("weather-condition").innerText = "Cuaca dingin";
} else {
document.getElementById("weather-condition").innerText = "Cuaca normal";
}
//recomendations umbrella
if(description.includes("rain") || description.includes("rainfall")){
document.getElementById("umbrella").innerText = "Need";
}else{
document.getElementById("umbrella").innerText = "No need"
}
//rekomendasi outdoor
if(description.includes("rain") || description.includes("rainfall")){
document.getElementById("outdoor").innerText = "Not enough";
}else if(description.includes("clear")){
document.getElementById("outdoor").innerText = "More";
}else if(description.includes("clouds")){
document.getElementById("outdoor").innerText = "Enough";
}else{
ocument.getElementById("outdoor").innerText = "Not enough";
}
//rekomendasi clothes
if(description.includes("rain") || description.includes("rainfall")){
document.getElementById("clothes").innerText = "waterproof";
}else if(description.includes("clear")){
document.getElementById("clothes").innerText = "Short";
}else if(description.includes("snow")){
document.getElementById("clothes").innerText = "Coat & waterproof";
}else{
document.getElementById("clothes").innerText = "Long";
}
},
//uv kondisi
displayUVIndex: function (data) {
const uvIndex = data.value;
//document.getElementById("uvdata").innerText = uvIndex;
//document.getElementById("uvdata").textContent = uvIndex;
//document.getElementById("uvvalue").innerText = uvIndex;
//let uvCategory;
if (uvIndex >= 0 && uvIndex <= 2) {
document.getElementById("uvindex").innerText = "Low";
} else if (uvIndex >= 3 && uvIndex <= 5) {
document.getElementById("uvindex").innerText = "Medium";
} else if (uvIndex >= 6 && uvIndex <= 7) {
document.getElementById("uvindex").innerText = "Hight";
} else if (uvIndex >= 8 && uvIndex <= 10) {
document.getElementById("uvindex").innerText = "Very high";
} else if (uvIndex >= 11) {
document.getElementById("uvindex").innerText = "Extreme";
}
},
fetchForecastWeather: function (city) {
fetch(
"https://api.openweathermap.org/data/2.5/forecast?q=" +
city +
"&units=metric&appid=" +
this.apiKey
)
.then((response) => {
if (!response.ok) {
alert("Tidak ada informasi cuaca.");
throw new Error("Tidak ada informasi cuaca.");
}
return response.json();
})
.then((data) => this.displayForecastWeather(data));
},
/*data cuaca 5 hari kedepan*/
displayForecastWeather: function (data) {
const forecastContainer = document.getElementById("forecast-container");
forecastContainer.innerHTML = "";
const options = { weekday: "short", day: "numeric", month: "long", year: "numeric" };
for (let i = 0; i < data.list.length; i += 8) {
const { dt_txt, main, weather } = data.list[i];
const date = new Date(dt_txt);
const forecastCard = document.createElement("div");
forecastCard.className = "flex flex-row w-[280px] h-[80px] bg-[#ffffff] rounded-lg gap-x-[10px] items-center p-[16px] mt-[16px]";
const weatherIcon = document.createElement("img");
weatherIcon.className = "w-[52px]";
weatherIcon.src = `http://openweathermap.org/img/wn/${weather[0].icon}.png`;
forecastCard.appendChild(weatherIcon);
const weatherInfo = document.createElement("div");
weatherInfo.className = "weather-info";
const dateElement = document.createElement("h2");
dateElement.innerText = date.toLocaleDateString("en-US", options);
weatherInfo.appendChild(dateElement);
const weatherDescription = document.createElement("p");
weatherDescription.className = "weather-description";
weatherDescription.innerText = weather[0].description;
weatherInfo.appendChild(weatherDescription);
forecastCard.appendChild(weatherInfo);
forecastContainer.appendChild(forecastCard);
}
},
search: function () {
const city = document.getElementById("InputSearchCountry").value;
this.fetchWeather(city);
sessionStorage.setItem('city', city);
},
};
document.getElementById("InputSearchCountry").addEventListener("keyup", function (event) {
if (event.key == "Enter") {
weather.search();
}
});