-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
131 lines (112 loc) · 5.86 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
function attachEvents() {
let baseUrl = 'http://localhost:3030/jsonstore/forecaster';
let location = document.getElementById('location');
let codeLocation = '';
let getWeatherButton = document.getElementById('submit');
let forecastDiv = document.getElementById('forecast');
let currentDiv = document.getElementById('current');
let upcomingDiv = document.getElementById('upcoming');
let errorP = document.createElement('p');
getWeatherButton.addEventListener('click', solve);
function solve() {
codeLocation = '';
fetch(baseUrl + '/locations')
.then((res) => res.json())
.then((data) => {
for (let each of data) {
if (each.name == location.value) {
codeLocation = each.code;
break;
}
}
})
.catch((error) => console.log('Error'));
fetch(baseUrl + `/today/${codeLocation}`)
.then((res) => res.json())
.then((data) => {
forecastDiv.style.display = 'block';
let todayName = data[codeLocation].name;
let todayDegreesLow = data[codeLocation].forecast.low;
let todayDegreesHigh = data[codeLocation].forecast.high;
let todayWeather = data[codeLocation].forecast.condition;
let divForecasts = document.createElement('div');
divForecasts.setAttribute('class', 'forecasts');
let conditionSymbolSpan = document.createElement('span');
let conditionSpan = document.createElement('span');
let forecastCity = document.createElement('span');
let forecastDegrees = document.createElement('span');
let forecastWeather = document.createElement('span');
forecastDegrees.setAttribute('class', 'forecast-data');
forecastWeather.setAttribute('class', 'forecast-data');
conditionSymbolSpan.setAttribute('class', 'condition symbol');
conditionSpan.setAttribute('class', 'condition');
forecastCity.setAttribute('class', 'forecast-data');
if (todayWeather == 'Sunny') {
conditionSymbolSpan.innerHTML = '☀';
} else if (todayWeather == 'Partly sunny') {
conditionSymbolSpan.innerHTML = '⛅';
} else if (todayWeather == 'Overcast') {
conditionSymbolSpan.innerHTML = '☁';
} else if (todayWeather == 'Rain') {
conditionSymbolSpan.innerHTML = '☔';
}
forecastCity.textContent = todayName;
forecastDegrees.innerHTML = `${todayDegreesLow}${"°"}/${todayDegreesHigh}${"°"}`;
forecastWeather.textContent = todayWeather;
currentDiv.appendChild(divForecasts);
divForecasts.appendChild(conditionSymbolSpan);
divForecasts.appendChild(conditionSpan);
conditionSpan.appendChild(forecastCity);
conditionSpan.appendChild(forecastDegrees);
conditionSpan.appendChild(forecastWeather);
})
.catch((error) => {
let errorP = document.createElement('p');
errorP.textContent = 'Error';
forecastDiv.appendChild(errorP);
forecastDiv.style.display = 'block';
currentDiv.style.display = 'none';
upcomingDiv.style.display = 'none';
});
fetch(baseUrl + `/upcoming/${codeLocation}`)
.then((res) => res.json())
.then((data) => {
let divForecasts3Days = document.createElement('div');
divForecasts3Days.setAttribute('class', 'forecast-info');
upcomingDiv.appendChild(divForecasts3Days);
for (each of data[codeLocation].forecast) {
let upcomingSpan = document.createElement('span');
let upcomingSymbol = document.createElement('span');
let upcomingDegrees = document.createElement('span');
let upcomingWeather = document.createElement('span');
upcomingSpan.setAttribute('class', 'upcoming');
upcomingSymbol.setAttribute('class', 'symbol');
upcomingDegrees.setAttribute('class', 'forecast-data');
upcomingWeather.setAttribute('class', 'forecast-data');
upcomingWeather.textContent = each.condition;
upcomingDegrees.innerHTML = `${each.low}${"°"}/${each.high}${"°"}`;
if (each.condition == 'Sunny') {
upcomingSymbol.innerHTML = '☀';
} else if (each.condition == 'Partly sunny') {
upcomingSymbol.innerHTML = '⛅';
} else if (each.condition == 'Overcast') {
upcomingSymbol.innerHTML = '☁';
} else if (each.condition == 'Rain') {
upcomingSymbol.innerHTML = '☔';
}
divForecasts3Days.appendChild(upcomingSpan);
upcomingSpan.appendChild(upcomingSymbol);
upcomingSpan.appendChild(upcomingDegrees);
upcomingSpan.appendChild(upcomingWeather);
};
});
// .catch((error) => {
// errorP.textContent = 'Error';
// forecastDiv.appendChild(errorP);
// forecastDiv.style.display = 'block';
// currentDiv.style.display = 'none';
// upcomingDiv.style.display = 'none';
// });
}
}
attachEvents();