-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
169 lines (130 loc) · 5.17 KB
/
script.js
File metadata and controls
169 lines (130 loc) · 5.17 KB
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
async function getWeather(location, method) {
const API_KEY = 'G294ALKAVC83DBB7KFK99BL9H';
const currentDate = getFormattedDate();
const url = `https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/${location}/${currentDate}?key=${API_KEY}`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error("Location not found");
}
const data = await response.json();
const weather = processWeather(data);
chooseBgImg(weather.conditions);
// toggle
const toggleIcon = document.querySelector('.toggle');
function toggle () {
const isFah = toggleIcon.src.includes('fah.svg');
const method = isFah ? createCelsiusIcon : createFahrenheitIcon;
render(weather, method);
toggleIcon.src = isFah ? "./icons/cel.svg" : "./icons/fah.svg";
}
toggleIcon.addEventListener('click', toggle);
render(weather, method);
} catch (error) {
handleWeatherError(error);
}
}
function handleWeatherError(error) {
const content = document.querySelector('.content');
content.innerHTML = "";
const msg = document.createElement('div');
msg.classList.add('error');
msg.textContent = "❌ Location not found. Please try another city.";
content.appendChild(msg);
}
function getFormattedDate() {
// get current date in the format of yyyy-MM-dd
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1 < 10 ? '0' + (now.getMonth() + 1).toString() : now.getMonth() + 1;
const day = now.getDate() < 10 ? '0' + now.getDate().toString() : now.getDate();
return `${year}-${month}-${day}`;
}
function fahrenheitToCelsius(temp) {
return ((temp - 32) * 5 / 9).toFixed(1);
}
function processWeather(response) {
// return an object of weather information displayed on the page
const today = response.days[0];
const tempF = today.temp;
const tempMaxF = today.tempmax;
const tempMinF = today.tempmin;
const tempC = fahrenheitToCelsius(tempF);
const tempMaxC = fahrenheitToCelsius(tempMaxF);
const tempMinC = fahrenheitToCelsius(tempMinF);
const conditions = today.conditions;
const location = response.address.toUpperCase();
return {tempF, tempMaxF, tempMinF, tempC, tempMaxC, tempMinC, conditions, location};
}
function createFahrenheitIcon() {
const img = document.createElement('img');
img.src = './icons/fah.svg';
img.classList.add('unit-icon', 'fah');
return img;
}
function createCelsiusIcon() {
const img = document.createElement('img');
img.src = './icons/cel.svg';
img.classList.add('unit-icon', 'cel');
return img;
}
function render(weather, method) {
const {tempF, tempMaxF, tempMinF, tempC, tempMaxC, tempMinC, conditions, location} = weather;
const content = document.querySelector('.content');
content.innerHTML = '';
const address = document.createElement('div');
address.classList.add('location');
address.textContent = location
const tempContainer = document.createElement('div');
tempContainer.classList.add('tempContainer');
const temp = document.createElement('div');
temp.classList.add('temp');
temp.textContent = method === createFahrenheitIcon ? tempF : tempC;
tempContainer.append(temp, method());
const condition = document.createElement('div');
condition.classList.add('condition');
condition.textContent = conditions;
const highLowContainer = document.createElement('div');
highLowContainer.classList.add('highLow');
const highContainer = document.createElement('div');
highContainer.classList.add('tempContainer');
const highTemp = document.createElement('div');
highTemp.classList.add('highTemp');
highTemp.textContent = method === createFahrenheitIcon ? tempMaxF : tempMaxC;
highContainer.append(highTemp, method());
const lowContainer = document.createElement('div');
lowContainer.classList.add('tempContainer');
const lowTemp = document.createElement('div');
lowTemp.classList.add('lowTemp');
lowTemp.textContent = method === createFahrenheitIcon ? tempMinF : tempMinC;
lowContainer.append(lowTemp, method());
highLowContainer.append(highContainer, lowContainer);
content.append(address, tempContainer, condition, highLowContainer);
}
const searchBtn = document.querySelector('.search-btn');
searchBtn.addEventListener('click', (e) => {
e.preventDefault();
const form = document.querySelector('form');
const location = document.getElementById('location').value;
form.reset();
getWeather(location, createFahrenheitIcon);
});
async function getBgImg (keyword) {
const url = `https://api.giphy.com/v1/gifs/translate?api_key=PvDMDvo3ZZ5rsBbA6gMAhrM5iq77xZr7&s=${keyword}`;
try {
const response = await fetch(url);
const imgData = await response.json();
document.body.style.backgroundImage = `url(${imgData.data.images.original.url})`;
} catch(err) {
console.error(err);
}
}
function chooseBgImg (conditions) {
if (/rain/i.test(conditions)) {
getBgImg('rainy');
} else if (/cloud/i.test(conditions)) {
getBgImg('cloudy');
} else {
getBgImg('sunny');
}
}