-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.js
224 lines (213 loc) · 11.3 KB
/
new.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// bewaar het antwoord in localstorage
// met fetch options objecten maken die gebruiker kan invoeren
//https://publicapis.io/nominatim-api
// https://www.weer.nl/
// https://www.weerplaza.nl/weerwidgets/
// https://appshots.design/apps/nothing-weather-komponent-app-shot-nothing_weather_
// this.childNodes().forEach(node => ... node.remove())
// https://dribbble.com/shots/19207866-Weather-Forecast-Dashboard
import { currentWeather } from './currentWeather.js';
import { weeklyWeather } from './weekInfo.js';
const button = document.querySelector('#button1');
const nav1 = document.querySelector('header nav');
const remove = document.querySelector('#removeSidebar');
const inputButton = document.querySelector('#inputButton');
const currentDataWrapper = document.querySelector('#currentDataWrapper');
const weekWrapper = document.querySelector('#weekWrapper');
const weatherCode = {
0: { code: 'Clear sky', png: 'V2_icons/large/png/10000_clear_large@2x.png' },
1: { code: 'Mainly clear', png: 'V2_icons/large/png/11000_mostly_clear_large@2x.png' },
2: { code: 'Partly cloudy', png: 'V2_icons/large/png/11010_partly_cloudy_large@2x.png' },
3: { code: 'Overcast', png: 'V2_icons/large/png/10010_cloudy_large@2x.png' },
45: { code: 'Fog', png: 'V2_icons/large/png/20000_fog_large@2x.png' },
46: { code: 'Depositing rime fog', png: 'V2_icons/large/png/71030_wintry_mix_large@2x.png' },
51: { code: 'Light drizzle', png: 'V2_icons/large/png/40000_drizzle_large@2x.png' },
53: { code: 'Moderate drizzle', png: 'V2_icons/large/png/40000_drizzle_large@2x.png' },
55: { code: 'Dense drizzle', png: 'V2_icons/large/png/40000_drizzle_large@2x.png' },
56: { code: 'Light freezing drizzle', png: 'V2_icons/large/png/60000_freezing_rain_drizzle_large@2x.png' },
57: { code: 'Dense freezing drizzle', png: 'V2_icons/large/png/60000_freezing_rain_drizzle_large@2x.png' },
61: { code: 'Slight rain', png: 'V2_icons/large/png/42000_rain_light_large@2x.png' },
63: { code: 'Moderate rain', png: 'V2_icons/large/png/40010_rain_large@2x.png' },
65: { code: 'Heavy rain', png: 'V2_icons/large/png/42010_rain_heavy_large@2x.png' },
66: { code: 'Light freezing rain', png: 'V2_icons/large/png/62000_freezing_rain_light_large@2x.png' },
67: { code: 'Heavy freezing rain', png: 'V2_icons/large/png/60010_freezing_rain_large@2x.png' },
71: { code: 'Slight snow fall', png: 'V2_icons/large/png/51000_snow_light_large@2x.png' },
73: { code: 'Moderate snow fall', png: 'V2_icons/large/png/50000_snow_large@2x.png' },
75: { code: 'Heavy snow fall', png: 'V2_icons/large/png/51010_snow_heavy_large@2x.png' },
77: { code: 'Snow grains', png: 'V2_icons/large/png/51220_wintry_mix_large@2x.png' },
80: { code: 'Slight rain showers', png: 'V2_icons/large/png/40010_rain_large@2x.png' },
81: { code: 'Moderate rain showers', png: 'V2_icons/large/png/42010_rain_heavy_large@2x.png' },
82: { code: 'Violent rain showers', png: 'V2_icons/large/png/42010_rain_heavy_large@2x.png' },
85: { code: 'Slight snow showers', png: 'V2_icons/large/png/51000_snow_light_large@2x.png' },
86: { code: 'Heavy snow showers', png: 'V2_icons/large/png/51010_snow_heavy_large@2x.png' },
95: { code: 'Thunderstorm', png: 'V2_icons/large/png/80000_tstorm_large@2x.png' },
96: { code: 'Thunderstorm with slight hail', png: 'V2_icons/large/png/80000_tstorm_large@2x.png' },
99: { code: 'Thunderstorm with heavy hail', png: 'V2_icons/large/png/80000_tstorm_large@2x.png' }
};
const weatherNumSmall = {
0: { code: 'Clear sky', png: 'V2_icons/small/png/10000_clear_small@2x.png' },
1: { code: 'Mainly clear', png: 'V2_icons/small/png/11000_mostly_clear_small@2x.png' },
2: { code: 'Partly cloudy', png: 'V2_icons/small/png/11010_partly_cloudy_small@2x.png' },
3: { code: 'Overcast', png: 'V2_icons/small/png/10010_cloudy_small@2x.png' },
45: { code: 'Fog', png: 'V2_icons/small/png/20000_fog_small@2x.png' },
46: { code: 'Depositing rime fog', png: 'V2_icons/small/png/71030_wintry_mix_small@2x.png' },
51: { code: 'Light drizzle', png: 'V2_icons/small/png/40000_drizzle_small@2x.png' },
53: { code: 'Moderate drizzle', png: 'V2_icons/small/png/40000_drizzle_small@2x.png' },
55: { code: 'Dense drizzle', png: 'V2_icons/small/png/40000_drizzle_small@2x.png' },
56: { code: 'Light freezing drizzle', png: 'V2_icons/small/png/60000_freezing_rain_drizzle_small@2x.png' },
57: { code: 'Dense freezing drizzle', png: 'V2_icons/small/png/60000_freezing_rain_drizzle_small@2x.png' },
61: { code: 'Slight rain', png: 'V2_icons/small/png/42000_rain_light_small@2x.png' },
63: { code: 'Moderate rain', png: 'V2_icons/small/png/40010_rain_small@2x.png' },
65: { code: 'Heavy rain', png: 'V2_icons/small/png/42010_rain_heavy_small@2x.png' },
66: { code: 'Light freezing rain', png: 'V2_icons/small/png/62000_freezing_rain_light_small@2x.png' },
67: { code: 'Heavy freezing rain', png: 'V2_icons/small/png/60010_freezing_rain_small@2x.png' },
71: { code: 'Slight snow fall', png: 'V2_icons/small/png/51000_snow_light_small@2x.png' },
73: { code: 'Moderate snow fall', png: 'V2_icons/small/png/50000_snow_small@2x.png' },
75: { code: 'Heavy snow fall', png: 'V2_icons/small/png/51010_snow_heavy_small@2x.png' },
77: { code: 'Snow grains', png: 'V2_icons/small/png/51220_wintry_mix_small@2x.png' },
80: { code: 'Slight rain showers', png: 'V2_icons/small/png/40010_rain_small@2x.png' },
81: { code: 'Moderate rain showers', png: 'V2_icons/small/png/42010_rain_heavy_small@2x.png' },
82: { code: 'Violent rain showers', png: 'V2_icons/small/png/42010_rain_heavy_small@2x.png' },
85: { code: 'Slight snow showers', png: 'V2_icons/small/png/51000_snow_light_small@2x.png' },
86: { code: 'Heavy snow showers', png: 'V2_icons/small/png/51010_snow_heavy_small@2x.png' },
95: { code: 'Thunderstorm', png: 'V2_icons/small/png/80000_tstorm_small@2x.png' },
96: { code: 'Thunderstorm with slight hail', png: 'V2_icons/small/png/80000_tstorm_small@2x.png' },
99: { code: 'Thunderstorm with heavy hail', png: 'V2_icons/small/png/80000_tstorm_small@2x.png' }
};
window.onload = () => {
const inputCity = document.querySelector('#city').value;
const params = new URLSearchParams({
city: inputCity,
format: 'jsonv2',
});
fetchData(params.toString(), params.get('city'));
};
button.addEventListener('click', () => {
nav1.classList.toggle('visible');
});
remove.addEventListener('click', () => {
nav1.classList.remove('visible');
});
// clears the DOM ... :D
function clearDom() {
const dataTarget = document.querySelector('#data');
const currentTarget = document.querySelector('#currentData');
if (dataTarget !== null && currentTarget !== null) {
dataTarget.remove();
currentTarget.remove();
}
}
function updateData() {
const current = document.querySelector('.currentWeather');
const weekly = document.querySelector('.weeklyWeather');
current.resetData();
weekly.resetData();
}
document.querySelector('#button2').addEventListener('click', () => {
updateData();
});
// calls clearDom, creates div for dailyWeather class
// maps through object, gets type name and changes int to weathercode
// returns new array with today's data
function setData(object, cityName) {
clearDom();
console.log('Setting data...');
const dataWrapper = document.createElement('div');
dataWrapper.id = 'data';
// console.log(object);
const myObject = new weeklyWeather(object, cityName, weatherNumSmall);
dataWrapper.append(myObject);
weekWrapper.append(dataWrapper);
}
// calls the currentWeather class with weather data
function setCurrent(object, city) {
clearDom();
console.log('setting current data...');
const currentData = document.createElement('div');
currentData.id = 'currentData';
const newCurrent = new currentWeather(object, city, weatherCode);
currentData.append(newCurrent);
currentDataWrapper.append(currentData);
}
// calls weather API, takes data and calls setData to set data in DOM
// "https://api.open-meteo.com/v1/forecast?latitude=52.305554&longitude=4.6926644&daily=weather_code,temperature_2m_max,temperature_2m_min,precipitation_sum&timezone=Europe%2FBerlin"
async function weatherData(params, cityName) {
const response =
await fetch(`https://api.open-meteo.com/v1/forecast?${params}&daily=weather_code,temperature_2m_max,temperature_2m_min,apparent_temperature_max,apparent_temperature_min,sunrise,sunset,daylight_duration,sunshine_duration,uv_index_max,uv_index_clear_sky_max,precipitation_sum,rain_sum,showers_sum,snowfall_sum,precipitation_hours,precipitation_probability_max,wind_speed_10m_max,wind_gusts_10m_max,wind_direction_10m_dominant,shortwave_radiation_sum,et0_fao_evapotranspiration&timezone=Europe%2FBerlin`);
const json1 = await response.json();
// console.log(json1);
const daily = Object.entries(json1)[8];
// console.log(daily[1]);
const object = Object.entries(daily[1]);
// console.log(object);
setData(object, cityName);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
}
// fetches weather API and calls setCurrent function for current data
async function currentData(params, cityName) {
const response =
await fetch(`https://api.open-meteo.com/v1/forecast?${params}¤t=temperature_2m,relative_humidity_2m,apparent_temperature,is_day,precipitation,rain,showers,snowfall,weather_code,cloud_cover,pressure_msl,surface_pressure,wind_speed_10m,wind_direction_10m,wind_gusts_10m`);
const json3 = await response.json();
console.log(Object.entries(json3)[8][1]);
const current = Object.entries(json3);
setCurrent(current, cityName);
}
// gets the coordinates of the city with nominatim API, calls weatherData() with coordinates
async function fetchData(params, cityName) {
const response =
await fetch(`https://nominatim.openstreetmap.org/search?${params}`);
let json2 = await response.json();
// console.log(json2);
json2 = json2[0];
console.log(json2.lat, json2.lon);
const params1 = new URLSearchParams({
latitude: json2.lat,
longitude: json2.lon
});
weatherData((params1.toString()), cityName);
currentData((params1.toString()), cityName);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
}
// checks if input is a number or a invalid string and throws an error
function handleData(inputValue) {
const exp = /^[A-Za-z ]+$/;
console.log(exp.test(inputValue));
try {
// if (Number(inputValue)) throw 'a number, please enter a city.';
if (!exp.test(inputValue)) throw 'is invalid, please enter a city.';
}
catch (error) {
alert('Input is ' + error);
}
}
// listens for submit button click, checks for input and calls fetchData()
inputButton.addEventListener('click', () => {
const inputCity = document.querySelector('#city').value;
const current = document.querySelector('.currentWeather');
const weekly = document.querySelector('.weeklyWeather');
current.newData();
if (inputCity !== '') {
handleData(inputCity);
const params = new URLSearchParams({
city: inputCity,
format: 'jsonv2',
});
fetchData(params.toString(), params.get('city'));
}
});
// listens for keypress (enter) in the input field
document.querySelector('#city').addEventListener('keypress', (event) => {
const inputCity = document.querySelector('#city').value;
if (inputCity !== '' && event.key === 'Enter') {
handleData(inputCity);
const params = new URLSearchParams({
city: inputCity,
format: 'jsonv2',
});
fetchData(params.toString(), params.get('city'));
}
});