-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
189 lines (150 loc) · 6.22 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
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
//! Selectors
const form = document.querySelector("form");
const input = document.querySelector("form input");
const cardContainer = document.getElementById("card-container");
const alertMessage = document.getElementById("alert");
const searchButton = document.getElementById("search");
//! Variables
const apiKey = "19fadf383f77445c7ead85a8d7ccce88";
let url; //Api isteği için kullanılacak
let cities = []; // Sergilenen şehirlerin isimleri tutulacak
let units = "metric"; // fahrenheit için 'imperial' yazılmalı
let lang = "en"; //Almanca için 'de' yazılacak
//& Location find
const locate = document.getElementById("locate");
const userLocationDiv = document.getElementById("userLocation");
let userLocation = false;
const clockDiv = document.querySelector("#clock");
//& Language
const langButton = document.querySelector(".language");
//! Event listeners
form.addEventListener("submit", (e) => {
e.preventDefault(); // Default özelliği kullanma yani submit etme
// console.log(city)
if (input.value) {
const city = input.value;
url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=${units}&lang=${lang}&appid=${apiKey}`;
// console.log(url)
getWeatherData();
}
form.reset(); // formu sıfırlar
});
locate.addEventListener("click", () => {
navigator.geolocation?.getCurrentPosition(({ coords }) => {
// console.log(coords)
const { latitude, longitude } = coords;
url = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&units=${units}&lang=${lang}&appid=${apiKey}`;
userLocation = true;
clockDiv.classList.replace("d-none", "d-block");
getWeatherData();
});
});
langButton.addEventListener("click", (e) => {
// console.log(e.target.textContent)
if (e.target.textContent == "DE") {
input.setAttribute("placeholder", "Suche nach einer Stadt");
lang = "de";
} else if (e.target.textContent == "EN") {
input.setAttribute("placeholder", "Search for a city");
lang = "en";
}
});
searchButton.addEventListener("click", (e) => {
e.preventDefault(); // Default özelliği kullanma yani submit etme
// console.log(city)
if (input.value) {
const city = input.value;
url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=${units}&lang=${lang}&appid=${apiKey}`;
console.log(userLocationDiv.textContent);
getWeatherData();
}
input.value = ""; // formu sıfırlar
});
//^ Functions
const getWeatherData = async () => {
try {
// const response = await fetch(url).then((response) => response.json()) //& fetch ile
const response = await axios(url); //^ Axios ile istek atma
console.log(response); // Api den gelen veri
//? Data destructure
// const {main, name, weather, sys} = response //& fetch
const { main, name, weather, sys } = response.data; //^ axios
// const iconUrl = `https://openweathermap.org/img/wn/${weather[0].icon}@2x.png` //^ openweathermap.org
const iconUrl = `https://s3-us-west-2.amazonaws.com/s.cdpn.io/162656/${weather[0].icon}.svg`; //^ alternatif
if (cities.indexOf(name) == -1) {
cities.unshift(name);
// console.log(cities)
let card = ` <div class="col" id="${name}">
<div class="card mb-4 rounded-3 shadow-sm">
<ul class="list-unstyled mt-2 mb-4">
<li class="text-end me-2"><i class="bi bi-x-circle"></i></li>
<h4 class="my-0 fw-normal">${name} <span ><sup><img src="https://flagsapi.com/${
sys.country
}/shiny/24.png" class="rounded-circle" alt=${
sys.country
}/> </sup></span></h4>
<h1 class="card-title pricing-card-title"><i class="bi bi-thermometer-half"></i> ${Math.round(
main.temp
)}<sup>°C</sup></h1>
<h6 class="card-title pricing-card-title">Min : ${Math.round(
main.temp_min
)}<sup>°C</sup> - Max : ${Math.round(
main.temp_max
)}<sup>°C</sup> </h6>
<h6 class="card-title pricing-card-title"><img src="./assets/wi-barometer.svg" height="30px"/>${
main.pressure
} <img src="./assets/wi-humidity.svg" height="30px"/>${
main.humidity
} </h6>
<li><img src="${iconUrl}"/></li>
<li>${weather[0].description.toUpperCase()}</li>
</ul>
</div>
</div>`;
if (userLocation) {
userLocationDiv.innerHTML = card;
userLocation = false;
} else {
cardContainer.innerHTML = card + cardContainer.innerHTML;
}
console.log(cities);
//! Remove Cities
const singleClearButton = document.querySelectorAll(".bi-x-circle");
singleClearButton.forEach((button) => {
button.addEventListener("click", (e) => {
// console.log(e.target.closest(".col").id)
// cities.splice(cities.indexOf(e.target.closest(".col").id), 1); //! Development aşamasında
delete cities[cities.indexOf(e.target.closest(".col").id)]; //! Array den siler
if (e.target.closest(".col-md-3")) {
const closestCol3 = e.target.closest(".col-md-3");
const clockElement = closestCol3.querySelector("#clock");
if (clockElement && clockElement.classList.contains("d-block")) {
clockElement.classList.replace("d-block", "d-none");
}
}
e.target.closest(".col").remove(); //! Dom'dan siler
});
});
} else {
if (lang == "de") {
alertMessage.textContent = `Sie kennen das Wetter für die ${name} bereits. Bitte suchen Sie nach einer anderen Stadt 😉`;
} else {
alertMessage.textContent = `You already know the weather for ${name}, Please search for another city 😉`;
}
alertMessage.classList.replace("d-none", "d-block");
setTimeout(() => {
alertMessage.classList.replace("d-block", "d-none");
}, 3000);
}
} catch (error) {
if (lang == "de") {
alertMessage.textContent = `Stadt nicht gefunden`;
} else {
alertMessage.textContent = `City Not Found!`;
}
alertMessage.classList.replace("d-none", "d-block");
setTimeout(() => {
alertMessage.classList.replace("d-block", "d-none");
}, 3000);
}
};