-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
196 lines (163 loc) · 7.76 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
189
190
191
192
193
194
195
window.addEventListener('load', async () => {
const placeEl = document.querySelector('.place');
const celciusEl = document.querySelector('.celcius');
const fahrenheitEl = document.querySelector('.fahrenheit');
const inputEl = document.querySelector('input');
const btnEl = document.querySelector('.btn');
const todayH2El = document.querySelector('.today h2');
const tomorrowH2El = document.querySelector('.tomorrow h2');
const meterEl = document.querySelector('.meter');
const teleEl = document.querySelector('.tele');
const loaderEl = document.querySelector('[class$="-loader"]');
const todayCondition = document.querySelector('.today .condition');
const todayHigh = document.querySelector('.today .high');
const todayLow = document.querySelector('.today .low');
const todayHumidity = document.querySelector('.today .humidity');
const todayWind = document.querySelector('.today .wind');
const todayRain = document.querySelector('.today .rain');
const tomorrowCondition = document.querySelector('.tomorrow .condition');
const tomorrowHigh = document.querySelector('.tomorrow .high');
const tomorrowLow = document.querySelector('.tomorrow .low');
const tomorrowHumidity = document.querySelector('.tomorrow .humidity');
const tomorrowWind = document.querySelector('.tomorrow .wind');
const tomorrowRain = document.querySelector('.tomorrow .rain');
const weatherApiKey = 'ead34245ca2ab83931b4369bb4a80be2';
const convertToCelcius = fahr => (fahr - 32) * (5/9)
const convertNameToCoords = async (cityName="london") => {
const apikey = '81059bbe9281463a9034986438b03482';
const base_url = 'https://api.opencagedata.com/geocode/v1/json'
const request_url = base_url + '?' + 'key=' + apikey + '&q=' + cityName
return fetch(request_url)
.then(res => res.json())
.then(data => data)
}
const getWeatherData = async (coords) => {
const proxy = 'https://cors-anywhere.herokuapp.com/'
const weatherApiUrl = `${proxy}https://api.darksky.net/forecast/${weatherApiKey}/${coords.lat},${coords.lng}`
return fetch(weatherApiUrl)
.then(res => res.json())
.then(data => data)
}
const toTitleCase = (phrase) => {
return phrase
.toLowerCase()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
};
const setIcon = (id, iconName) => {
let skycons = new Skycons({ "color": "#f5a623" });
skycons.set(id, iconName);
skycons.play();
}
const getGeoLoc = async () => {
const geoApiKey = 'f73e3d2d85744784a8c22f907bf4cd99';
const res = await fetch(`https://api.ipgeolocation.io/ipgeo?apiKey=${geoApiKey}`);
const data = await res.json()
// console.log(data);
loaderEl.style.display = 'none';
var weatherData = await getWeatherData({lng: data.longitude, lat: data.latitude});
const todayTemp = weatherData.currently.temperature;
const firstDay = weatherData.daily.data[0];
const secondDay = weatherData.daily.data[1];
placeEl.textContent = toTitleCase(data.city) + ', ' + data.country_name;
todayH2El.style.opacity = '1';
tomorrowH2El.style.opacity = '1';
meterEl.style.opacity = '1';
teleEl.style.opacity = '1';
fahrenheitEl.innerHTML = `
${Math.floor(todayTemp)}
<span class="unit">
<div class="box">
<sup>°</sup>
<sub>Fahrenheit</sub>
</div>
</span>
`
celciusEl.innerHTML = `
${Math.floor(convertToCelcius(todayTemp))}
<div class="unit">
<div class="box">
<sup>°</sup>
<sub>Celcius</sub>
</div>
</div>
`
// FIRST DAY
todayCondition.textContent = firstDay.summary.replace('.', '');
todayHigh.innerHTML = '<span class="hell">High:</span> ' + Math.floor(firstDay.temperatureHigh) + '°F' + ' / ' + Math.floor(convertToCelcius(firstDay.temperatureHigh)) + '°C'
todayLow.textContent = 'Low: ' + Math.floor(firstDay.temperatureLow) + '°F' + ' / ' + Math.floor(convertToCelcius(firstDay.temperatureLow)) + '°C'
todayHumidity.textContent = 'Humidity: ' + Math.floor(firstDay.humidity * 100) + '%'
todayWind.textContent = 'Wind: ' + firstDay.windSpeed + "mph"
todayRain.textContent = 'Rain: ' + Math.floor(firstDay.precipProbability * 100) + '% chance'
setIcon(icon1, firstDay.icon)
// SECOND DAY
tomorrowCondition.textContent = secondDay.summary.replace('.', '');
tomorrowHigh.textContent = 'High: ' + Math.floor(secondDay.temperatureHigh) + '°F' + ' / ' + Math.floor(convertToCelcius(secondDay.temperatureHigh)) + '°C'
tomorrowLow.textContent = 'Low: ' + Math.floor(secondDay.temperatureLow) + '°F' + ' / ' + Math.floor(convertToCelcius(secondDay.temperatureLow)) + '°C'
tomorrowHumidity.textContent = 'Humidity: ' + Math.floor(secondDay.humidity * 100) + '%'
tomorrowWind.textContent = 'Wind: ' + secondDay.windSpeed + "mph"
tomorrowRain.textContent = 'Rain: ' + Math.floor(secondDay.precipProbability * 100) + '% chance'
setIcon(icon2, secondDay.icon)
// console.log(weatherData)
}
getGeoLoc()
btnEl.addEventListener('click', async (e) => {
if (inputEl.value.trim() !== ''){
loaderEl.style.display = 'inline-block';
}
const coordsData = await convertNameToCoords(inputEl.value);
let cityName = inputEl.value
inputEl.value = ''
const locationInfo = coordsData.results[0];
if (!locationInfo){
loaderEl.style.display = 'none';
}
const { city, continent, country, country_code } = locationInfo.components
console.log(locationInfo)
var weatherData = await getWeatherData(locationInfo.geometry);
const todayTemp = weatherData.currently.temperature;
const firstDay = weatherData.daily.data[0];
const secondDay = weatherData.daily.data[1];
placeEl.textContent = toTitleCase(cityName) + ', ' + country;
loaderEl.style.display = 'none';
todayH2El.style.opacity = '1';
tomorrowH2El.style.opacity = '1';
teleEl.style.opacity = '1';
fahrenheitEl.innerHTML = `
${Math.floor(todayTemp)}
<span class="unit">
<div class="box">
<sup>°</sup>
<sub>Fahrenheit</sub>
</div>
</span>
`
celciusEl.innerHTML = `
${Math.floor(convertToCelcius(todayTemp))}
<div class="unit">
<div class="box">
<sup>°</sup>
<sub>Celcius</sub>
</div>
</div>
`
// FIRST DAY
todayCondition.textContent = firstDay.summary.replace('.', '');
todayHigh.textContent = 'High: ' + Math.floor(firstDay.temperatureHigh) + '°F' + ' / ' + Math.floor(convertToCelcius(firstDay.temperatureHigh)) + '°C'
todayLow.textContent = 'Low: ' + Math.floor(firstDay.temperatureLow) + '°F' + ' / ' + Math.floor(convertToCelcius(firstDay.temperatureLow)) + '°C'
todayHumidity.textContent = 'Humidity: ' + Math.floor(firstDay.humidity * 100) + '%'
todayWind.textContent = 'Wind: ' + firstDay.windSpeed + "mph"
todayRain.textContent = 'Rain: ' + Math.floor(firstDay.precipProbability * 100) + '% chance'
setIcon(icon1, firstDay.icon)
// SECOND DAY
tomorrowCondition.textContent = secondDay.summary.replace('.', '');
tomorrowHigh.textContent = 'High: ' + Math.floor(secondDay.temperatureHigh) + '°F' + ' / ' + Math.floor(convertToCelcius(secondDay.temperatureHigh)) + '°C'
tomorrowLow.textContent = 'Low: ' + Math.floor(secondDay.temperatureLow) + '°F' + ' / ' + Math.floor(convertToCelcius(secondDay.temperatureLow)) + '°C'
tomorrowHumidity.textContent = 'Humidity: ' + Math.floor(secondDay.humidity * 100) + '%'
tomorrowWind.textContent = 'Wind: ' + secondDay.windSpeed + "mph"
tomorrowRain.textContent = 'Rain: ' + Math.floor(secondDay.precipProbability * 100) + '% chance'
setIcon(icon2, secondDay.icon)
console.log(weatherData)
});
})