weatherForecastData.js 27.2 KB
Newer Older
EnesKarakas's avatar
dawd    
EnesKarakas committed
1
import React, { useState } from "react";
Weiser's avatar
asdf  
Weiser committed
2

Weiser's avatar
new    
Weiser committed
3
import "./weatherForecastData.css";
Weiser's avatar
asdf  
Weiser committed
4
5

const WeatherForecastData = (props) => {
EnesKarakas's avatar
dawd    
EnesKarakas committed
6
7
  const [query, setQuery] = useState("");
  const [data, setData] = useState([]);
EnesKarakas's avatar
dawd    
EnesKarakas committed
8
  const [weatherdata, setweatherData] = useState([]);
EnesKarakas's avatar
dawd    
EnesKarakas committed
9

EnesKarakas's avatar
slider    
EnesKarakas committed
10
11
12
13
14
15
16
  const [sliderValue, setSliderValue] = useState(1);

  // Handler-Funktion, um den Wert des Schiebereglers zu aktualisieren
  const handleSliderChange = (event) => {
    setSliderValue(event.target.value);
  };

EnesKarakas's avatar
dawd    
EnesKarakas committed
17
18
19
20
21
22
  const handleInputChange = (e) => {
    setQuery(e.target.value);
    if (e.target.value.trim() !== "") {
      searchAPI(e.target.value.trim());
    }
  };
EnesKarakas's avatar
dawd    
EnesKarakas committed
23
24
  const downloadData = () => {
    const fileformat = document.getElementById("fileformat").value;
EnesKarakas's avatar
fes    
EnesKarakas committed
25

EnesKarakas's avatar
dawd    
EnesKarakas committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    switch (fileformat) {
      case "json":
        downloadJson(weatherdata);
        break;
      case "xml":
        downloadXml(weatherdata);
        break;
      case "csv":
        downloadCsv(weatherdata);
        break;
      default:
        alert("Unsupported file format");
    }
  };

  const downloadJson = (weatherdata) => {
    const json = JSON.stringify(weatherdata, null, 2);
    const blob = new Blob([json], { type: "application/json" });
    saveAs(blob, "weatherdata.json");
  };

  const downloadXml = (weatherdata) => {
EnesKarakas's avatar
EnesKarakas committed
48
49
50
51
52
    var xml = jsonToXml(weatherdata);
    xml =
      "<?xml version='1.0' encoding='UTF-8' ?><weatherdata>" +
      xml +
      "</weatherdata>";
EnesKarakas's avatar
dawd    
EnesKarakas committed
53
54
55
56
57
58
59
60
61
62
    const blob = new Blob([xml], { type: "application/xml" });
    saveAs(blob, "weatherdata.xml");
  };

  const downloadCsv = (weatherdata) => {
    const csv = jsonToCsv(weatherdata);
    const blob = new Blob([csv], { type: "text/csv" });
    saveAs(blob, "weatherdata.csv");
  };

EnesKarakas's avatar
EnesKarakas committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  const jsonToXml = (obj) => {
    var xml = "";
    for (var prop in obj) {
      xml += "<" + prop + ">";
      if (Array.isArray(obj[prop])) {
        for (var array of obj[prop]) {
          // A real botch fix here
          xml += "</" + prop + ">";
          xml += "<" + prop + ">";

          xml += jsonToXml(new Object(array));
        }
      } else if (typeof obj[prop] == "object") {
        xml += jsonToXml(new Object(obj[prop]));
      } else {
        xml += obj[prop];
      }
      xml += "</" + prop + ">";
EnesKarakas's avatar
dawd    
EnesKarakas committed
81
    }
EnesKarakas's avatar
EnesKarakas committed
82
    var xml = xml.replace(/<\/?[0-9]{1,}>/g, "");
EnesKarakas's avatar
dawd    
EnesKarakas committed
83
84
85
86
    return xml;
  };

  const jsonToCsv = (json) => {
EnesKarakas's avatar
EnesKarakas committed
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
    const rows = [];

    // Funktion, um die Header und die Werte zu extrahieren
    function processObject(obj, parentKey = "") {
      const keys = Object.keys(obj);
      keys.forEach((key) => {
        const newKey = parentKey ? `${parentKey}_${key}` : key;
        if (typeof obj[key] === "object" && !Array.isArray(obj[key])) {
          processObject(obj[key], newKey);
        } else {
          rows.push({ key: newKey, value: obj[key] });
        }
      });
    }

    // JSON verarbeiten
    processObject(json);

    // Header und Werte trennen
    const headers = rows.map((row) => row.key).join(";");
    const values = rows.map((row) => row.value).join(";");

    // CSV-Zeilen erstellen
    const csv = `${headers}\n${values}`;

    return csv;
EnesKarakas's avatar
dawd    
EnesKarakas committed
113
  };
EnesKarakas's avatar
EnesKarakas committed
114

EnesKarakas's avatar
csy    
EnesKarakas committed
115
116
  return (
    <div className="home-container">
EnesKarakas's avatar
EnesKarakas committed
117
      <div className="thq-grid-5">
Weiser's avatar
Weiser committed
118
119
120
        <div class="h1">
          <h1>Choose your City</h1>
          <br />
EnesKarakas's avatar
EnesKarakas committed
121
122
123
        </div>
        <div class="field_write">
          <div class="fw">
Weiser's avatar
Weiser committed
124
125
126
            <div class="h3">
              <h3>Choose your Location</h3>
            </div>
EnesKarakas's avatar
EnesKarakas committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
            <label htmlFor="location"></label>
            <input
              type="text"
              id="city_text"
              placeholder="Location"
              value={query}
              onChange={handleInputChange}
            />
            {data.map((item) => (
              <ListItem
                key={item.id}
                name={item.name}
                country={item.country}
                region={item.region}
                lat={item.lat}
                lon={item.lon}
EnesKarakas's avatar
dawd    
EnesKarakas committed
143
              />
EnesKarakas's avatar
EnesKarakas committed
144
145
146
147
            ))}
          </div>
          <div class="fw">
            <div class="h3">
Weiser's avatar
Weiser committed
148
149
              <h3>Choose your Region</h3>
            </div>
EnesKarakas's avatar
EnesKarakas committed
150
151
152
153
154
            <label for="region"></label>
            <input type="text" id="region_text" placeholder="Region" />
          </div>
          <div class="fw">
            <div class="h3">
Weiser's avatar
Weiser committed
155
156
              <h3>Choose your Country</h3>
            </div>
EnesKarakas's avatar
EnesKarakas committed
157
158
159
160
161
            <label></label>
            <input type="text" id="country_text" placeholder="Country" />
          </div>
          <div class="fw">
            <div class="h3">
Weiser's avatar
Weiser committed
162
163
              <h3>Choose your Latitude</h3>
            </div>
EnesKarakas's avatar
EnesKarakas committed
164
165
166
167
168
            <label></label>
            <input type="text" id="latitude_text" placeholder="Latitude" />
          </div>
          <div class="fw">
            <div class="h3">
Weiser's avatar
Weiser committed
169
              <h3>Choose your Longitude</h3>
EnesKarakas's avatar
csy    
EnesKarakas committed
170
            </div>
EnesKarakas's avatar
EnesKarakas committed
171
172
173
174
175
            <label></label>
            <input type="text" id="longitude_text" placeholder="Longitude" />
          </div>
        </div>
        <div class="h1">
Weiser's avatar
Weiser committed
176
177
          <h1>Choose your custom output Data</h1>
          <br />
EnesKarakas's avatar
EnesKarakas committed
178
        </div>
Weiser's avatar
Weiser committed
179
180
181
        <div>
        <button className="thq-button-filled" onClick={toggleCheckboxes}>Toggle Checkboxes</button>
        </div>
EnesKarakas's avatar
EnesKarakas committed
182
183
184
        <div className="dataselect">
          <div class="data">
            <div class="h3">
Weiser's avatar
Weiser committed
185
              <h3>Location</h3>
EnesKarakas's avatar
EnesKarakas committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
            </div>
            <input type="checkbox" id="name" className="checkBoxFilter" />
            <label> City Name</label>
            <br />
            <input type="checkbox" id="region" className="checkBoxFilter" />
            <label> Region</label>
            <br />
            <input type="checkbox" id="country" className="checkBoxFilter" />
            <label> Country</label>
            <br />
            <input type="checkbox" id="lon" className="checkBoxFilter" />
            <label> Longitude</label>
            <br />
            <input type="checkbox" id="lat" className="checkBoxFilter" />
            <label> Latitude</label>
          </div>

Weiser's avatar
Weiser committed
203
          <div class="data">
EnesKarakas's avatar
EnesKarakas committed
204
            <div class="h3">
Weiser's avatar
Weiser committed
205
              <h3>Time</h3>
EnesKarakas's avatar
EnesKarakas committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
            </div>
            <input type="checkbox" id="tz_id" className="checkBoxFilter" />
            <label> Timezone Id</label>
            <br />
            <input
              type="checkbox"
              id="localtime_epoch"
              className="checkBoxFilter"
            />
            <label> Localtime Epoch</label>
            <br />
            <input type="checkbox" id="localtime" className="checkBoxFilter" />
            <label> Localtime</label>
            <br />
            <input
              type="checkbox"
              id="last_updated_epoch"
              className="checkBoxFilter"
            />
            <label> Last Updated Epoch</label>
            <br />
            <input
              type="checkbox"
              id="last_updated"
              className="checkBoxFilter"
            />
            <label> Last Updated</label>
            <br />
            <input type="checkbox" id="date" class="checkBoxFilter" />
            <label for="date">Date</label>
            <br />
            <input type="checkbox" id="date_epoch" class="checkBoxFilter" />
            <label for="date_epoch">Date Epoch</label>
          </div>
Weiser's avatar
Weiser committed
240
          <div class="data">
EnesKarakas's avatar
EnesKarakas committed
241
            <div class="h3">
Weiser's avatar
Weiser committed
242
              <h3>Primery Data</h3>
EnesKarakas's avatar
EnesKarakas committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
            </div>
            <input type="checkbox" id="maxtemp" class="checkBoxFilter" />
            <label for="maxtemp">Max Temperature</label>
            <br />
            <input type="checkbox" id="mintemp" class="checkBoxFilter" />
            <label for="mintemp">Min Temperature</label>
            <br />
            <input type="checkbox" id="avgtemp" class="checkBoxFilter" />
            <label for="avgtemp">Average Temperature</label>
            <br />
            <input type="checkbox" id="maxwind" class="checkBoxFilter" />
            <label for="maxwind">Max Wind Speed</label>
            <br />
            <input type="checkbox" id="totalprecip" class="checkBoxFilter" />
            <label for="totalprecip">Total Precipitation</label>
            <br />
            <input type="checkbox" id="avgvis" class="checkBoxFilter" />
            <label for="avgvis">Average Visibility</label>
            <br />
            <input type="checkbox" id="avghumidity" class="checkBoxFilter" />
            <label for="avghumidity">Average Humidity</label>
            <br />
            <input
              type="checkbox"
              id="daily_will_it_rain"
              class="checkBoxFilter"
            />
            <label for="daily_will_it_rain">Daily Will it Rain</label>
          </div>
          <div class="data">
            <div class="h3">
Weiser's avatar
Weiser committed
274
              <h3>Daily Data</h3>
EnesKarakas's avatar
EnesKarakas committed
275
            </div>
Weiser's avatar
Weiser committed
276

EnesKarakas's avatar
EnesKarakas committed
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
            <input
              type="checkbox"
              id="daily_chance_of_rain"
              class="checkBoxFilter"
            />
            <label for="daily_chance_of_rain">Daily Chance of Rain</label>
            <br />
            <input
              type="checkbox"
              id="daily_will_it_snow"
              class="checkBoxFilter"
            />
            <label for="daily_will_it_snow">Daily Will it Snow</label>
            <br />
            <input
              type="checkbox"
              id="daily_chance_of_snow"
              class="checkBoxFilter"
            />
            <label for="daily_chance_of_snow">Daily Chance of Snow</label>
          </div>
Weiser's avatar
Weiser committed
298
          <div class="data">
EnesKarakas's avatar
EnesKarakas committed
299
            <div class="h3">
Weiser's avatar
Weiser committed
300
              <h3>Time of Day Data</h3>
Weiser's avatar
csssss    
Weiser committed
301
            </div>
EnesKarakas's avatar
EnesKarakas committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
            <input type="checkbox" id="uv" class="checkBoxFilter" />
            <label for="uv">UV Index</label>
            <br />
            <input type="checkbox" id="sunrise" class="checkBoxFilter" />
            <label for="sunrise">Sunrise</label>
            <br />
            <input type="checkbox" id="sunset" class="checkBoxFilter" />
            <label for="sunset">Sunset</label>
            <br />
            <input type="checkbox" id="moonrise" class="checkBoxFilter" />
            <label for="moonrise">Moonrise</label>
            <br />
            <input type="checkbox" id="moonset" class="checkBoxFilter" />
            <label for="moonset">Moonset</label>
            <br />
            <input type="checkbox" id="moon_phase" class="checkBoxFilter" />
            <label for="moon_phase">Moon Phase</label>
            <br />
            <input
              type="checkbox"
              id="moon_illumination"
              class="checkBoxFilter"
            />
            <label for="moon_illumination">Moon Illumination</label>
            <br />
            <input
              type="checkbox"
              id="hour_time_epoch"
              class="checkBoxFilter"
            />
            <label for="hour_time_epoch">Hour Time Epoch</label>
          </div>
          <div class="data">
            <div class="h3">
              <h3>Hourly Data</h3>
Weiser's avatar
csssss    
Weiser committed
337
            </div>
EnesKarakas's avatar
EnesKarakas committed
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
            <input type="checkbox" id="hour_time" class="checkBoxFilter" />
            <label for="hour_time">Hour Time</label>
            <br />
            <input type="checkbox" id="hour_temp" class="checkBoxFilter" />
            <label for="hour_temp">Hour Temperature</label>
            <br />
            <input type="checkbox" id="hour_is_day" class="checkBoxFilter" />
            <label for="hour_is_day">Hour Is Day</label>
            <br />
            <input type="checkbox" id="hour_wind" class="checkBoxFilter" />
            <label for="hour_wind">Hour Wind</label>
            <br />
            <input
              type="checkbox"
              id="hour_wind_degree"
              class="checkBoxFilter"
            />
            <label for="hour_wind_degree">Hour Wind Degree</label>
            <br />
            <input type="checkbox" id="hour_wind_dir" class="checkBoxFilter" />
            <label for="hour_wind_dir">Hour Wind Direction</label>
            <br />
            <input type="checkbox" id="hour_pressure" class="checkBoxFilter" />
            <label for="hour_pressure">Hour Pressure</label>
            <br />
            <input type="checkbox" id="hour_precip" class="checkBoxFilter" />
            <label for="hour_precip">Hour Precipitation</label>
            <br />
            <input type="checkbox" id="hour_humidity" class="checkBoxFilter" />
            <label for="hour_humidity">Hour Humidity</label>
            <br />
            <input type="checkbox" id="hour_cloud" class="checkBoxFilter" />
            <label for="hour_cloud">Hour Cloud</label>
            <br />
            <input type="checkbox" id="hour_feelslike" class="checkBoxFilter" />
            <label for="hour_feelslike">Hour Feels Like</label>
            <br />
            <input type="checkbox" id="hour_windchill" class="checkBoxFilter" />
            <label for="hour_windchill">Hour Wind Chill</label>
            <br />
            <input type="checkbox" id="hour_heatindex" class="checkBoxFilter" />
            <label for="hour_heatindex">Hour Heat Index</label>
            <br />
            <input type="checkbox" id="hour_dewpoint" class="checkBoxFilter" />
            <label for="hour_dewpoint">Hour Dew Point</label>
            <br />
            <input
              type="checkbox"
              id="hourly_will_it_rain"
              class="checkBoxFilter"
            />
            <label for="hourly_will_it_rain">Will it Rain</label>
            <br />
            <input
              type="checkbox"
              id="hourly_chance_of_rain"
              class="checkBoxFilter"
            />
            <label for="hourly_chance_of_rain">Chance of Rain</label>
            <br />
            <input
              type="checkbox"
              id="hourly_will_it_snow"
              class="checkBoxFilter"
            />
            <label for="hourly_will_it_snow">Will it Snow</label>
            <br />
            <input
              type="checkbox"
              id="hourly_chance_of_snow"
              class="checkBoxFilter"
            />
            <label for="hourly_chance_of_snow">Chance of Snow</label>
            <br />
            <input type="checkbox" id="hour_vis" class="checkBoxFilter" />
            <label for="hour_vis">Hour Visibility</label>
            <br />
            <input type="checkbox" id="hour_gust" class="checkBoxFilter" />
            <label for="hour_gust">Hour Gust</label>
            <br />
            <input type="checkbox" id="hour_uv" class="checkBoxFilter" />
            <label for="hour_uv">Hour UV</label>
          </div>
          <div class="data">
            <div class="h3">
              <h3>Weather Alerts</h3>
Weiser's avatar
csssss    
Weiser committed
424
            </div>
EnesKarakas's avatar
EnesKarakas committed
425
426
427
428
429
430
431
432
433
            <input type="checkbox" id="alerts" class="checkBoxFilter" />
            <label for="alerts">Alerts</label>
          </div>
        </div>
        <div class="fw1">
          <div class="h3">
            <h3>How many forecast days do you want?</h3>
          </div>
          <label for="days"></label>
EnesKarakas's avatar
slider    
EnesKarakas committed
434
435
436
437
438
          <input
            type="range"
            id="days"
            name="days"
            min="1"
Weiser's avatar
Weiser committed
439
            max="14"
EnesKarakas's avatar
slider    
EnesKarakas committed
440
441
442
443
            value={sliderValue}
            onChange={handleSliderChange}
          />
          <span>{sliderValue}</span>
EnesKarakas's avatar
EnesKarakas committed
444
445
446
447
448
449
        </div>
        <div class="fw1">
          <div class="h3">
            <h3>Choose a Unit</h3>
          </div>
          <select name="unnits" id="units">
Weiser's avatar
Weiser committed
450
            <option value="world">Metric</option>
EnesKarakas's avatar
EnesKarakas committed
451
            <option value="american">
Weiser's avatar
Weiser committed
452
              Emperial
EnesKarakas's avatar
EnesKarakas committed
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
            </option>
          </select>
        </div>
        <div>
          <button className="thq-button-filled" onClick={getData}>
            Generate your data
          </button>
        </div>
        <br />
        <div class="fw1">
          <div class="h3">
            <h3>Choose a Fileformat</h3>
          </div>
          <select name="fileformat" id="fileformat">
            <option value="json">JSON</option>
            <option value="xml">XML</option>
            <option value="csv">CSV</option>
          </select>
        </div>
        <button className="thq-button-filled" onClick={downloadData}>
          Download your data
        </button>
        <div id="apiUrl">
          <div class="fw1">
            <div class="h3">
Weiser's avatar
csssss    
Weiser committed
478
479
              <h3>Your Generated apiUrl with your custom Data</h3>
            </div>
EnesKarakas's avatar
EnesKarakas committed
480
            <input className="input" type="text" id="apiUrloutput" readOnly />
Weiser's avatar
csssss    
Weiser committed
481
          </div>
Weiser's avatar
asdf  
Weiser committed
482
        </div>
Weiser's avatar
csssss    
Weiser committed
483
        <div class="output">
EnesKarakas's avatar
EnesKarakas committed
484
485
486
487
          <h2>Output:</h2>
          <br />
          <pre id="weatherData"></pre>
        </div>
Weiser's avatar
yippie    
Weiser committed
488
489
      </div>
    </div>
EnesKarakas's avatar
csy    
EnesKarakas committed
490
  );
Weiser's avatar
Weiser committed
491
492
493
494
495
496
497
  function toggleCheckboxes() {
    var checkboxes = document.querySelectorAll('.checkBoxFilter');
    checkboxes.forEach(function(checkbox) {
        checkbox.checked = !checkbox.checked;
    });
}

EnesKarakas's avatar
csy    
EnesKarakas committed
498
499

  function getData() {
Weiser's avatar
Weiser committed
500
    const units = document.getElementById("units").value;
Weiser's avatar
days    
Weiser committed
501
    const days = document.getElementById("days").value;
EnesKarakas's avatar
dfaw    
EnesKarakas committed
502
    const alerts = boolToWord(document.getElementById("alerts").checked);
Weiser's avatar
Weiser committed
503

EnesKarakas's avatar
dwa    
EnesKarakas committed
504
505
    const latitude_text = document.getElementById("latitude_text").value;
    const longitude_text = document.getElementById("longitude_text").value;
EnesKarakas's avatar
csy    
EnesKarakas committed
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560

    const name = document.getElementById("name").checked;
    const regionCheckbox = document.getElementById("region").checked;
    const countryCheckbox = document.getElementById("country").checked;
    const lon = document.getElementById("lon").checked;
    const lat = document.getElementById("lat").checked;
    const tz_id = document.getElementById("tz_id").checked;
    const localtime_epoch = document.getElementById("localtime_epoch").checked;
    const localtime = document.getElementById("localtime").checked;
    const last_updated_epoch =
      document.getElementById("last_updated_epoch").checked;
    const last_updated = document.getElementById("last_updated").checked;
    const date = document.getElementById("date").checked;
    const date_epoch = document.getElementById("date_epoch").checked;
    const maxtemp = document.getElementById("maxtemp").checked;
    const mintemp = document.getElementById("mintemp").checked;
    const avgtemp = document.getElementById("avgtemp").checked;
    const maxwind = document.getElementById("maxwind").checked;
    const totalprecip = document.getElementById("totalprecip").checked;
    const avgvis = document.getElementById("avgvis").checked;
    const avghumidity = document.getElementById("avghumidity").checked;
    const daily_will_it_rain =
      document.getElementById("daily_will_it_rain").checked;
    const daily_chance_of_rain = document.getElementById(
      "daily_chance_of_rain"
    ).checked;
    const daily_will_it_snow =
      document.getElementById("daily_will_it_snow").checked;
    const daily_chance_of_snow = document.getElementById(
      "daily_chance_of_snow"
    ).checked;
    const uv = document.getElementById("uv").checked;
    const sunrise = document.getElementById("sunrise").checked;
    const sunset = document.getElementById("sunset").checked;
    const moonrise = document.getElementById("moonrise").checked;
    const moonset = document.getElementById("moonset").checked;
    const moon_phase = document.getElementById("moon_phase").checked;
    const moon_illumination =
      document.getElementById("moon_illumination").checked;
    const hour_time_epoch = document.getElementById("hour_time_epoch").checked;
    const hour_time = document.getElementById("hour_time").checked;
    const hour_temp = document.getElementById("hour_temp").checked;
    const hour_is_day = document.getElementById("hour_is_day").checked;
    const hour_wind = document.getElementById("hour_wind").checked;
    const hour_wind_degree =
      document.getElementById("hour_wind_degree").checked;
    const hour_wind_dir = document.getElementById("hour_wind_dir").checked;
    const hour_pressure = document.getElementById("hour_pressure").checked;
    const hour_precip = document.getElementById("hour_precip").checked;
    const hour_humidity = document.getElementById("hour_humidity").checked;
    const hour_cloud = document.getElementById("hour_cloud").checked;
    const hour_feelslike = document.getElementById("hour_feelslike").checked;
    const hour_windchill = document.getElementById("hour_windchill").checked;
    const hour_heatindex = document.getElementById("hour_heatindex").checked;
    const hour_dewpoint = document.getElementById("hour_dewpoint").checked;
EnesKarakas's avatar
dwa    
EnesKarakas committed
561
562
563
564
565
566
567
568
569
570
571
572
    const hourly_will_it_rain = document.getElementById(
      "hourly_will_it_rain"
    ).checked;
    const hourly_chance_of_rain = document.getElementById(
      "hourly_chance_of_rain"
    ).checked;
    const hourly_will_it_snow = document.getElementById(
      "hourly_will_it_snow"
    ).checked;
    const hourly_chance_of_snow = document.getElementById(
      "hourly_chance_of_snow"
    ).checked;
EnesKarakas's avatar
csy    
EnesKarakas committed
573
574
575
576
    const hour_vis = document.getElementById("hour_vis").checked;
    const hour_gust = document.getElementById("hour_gust").checked;
    const hour_uv = document.getElementById("hour_uv").checked;

Weiser's avatar
Weiser committed
577

EnesKarakas's avatar
csy    
EnesKarakas committed
578
    let filterArray = [];
Weiser's avatar
Weiser committed
579
    
EnesKarakas's avatar
csy    
EnesKarakas committed
580
581
582
583
584
585
586
587
588
589
590
591
592

    if (name) filterArray.push("name");
    if (regionCheckbox) filterArray.push("region");
    if (countryCheckbox) filterArray.push("country");
    if (lon) filterArray.push("lon");
    if (lat) filterArray.push("lat");
    if (tz_id) filterArray.push("tz_id");
    if (localtime_epoch) filterArray.push("localtime_epoch");
    if (localtime) filterArray.push("localtime");
    if (last_updated_epoch) filterArray.push("last_updated_epoch");
    if (last_updated) filterArray.push("last_updated");
    if (date) filterArray.push("date");
    if (date_epoch) filterArray.push("date_epoch");
EnesKarakas's avatar
dwa    
EnesKarakas committed
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
    if (maxtemp)
      if (units == "world") {
        filterArray.push("maxtemp_c");
      } else {
        filterArray.push("maxtemp_f");
      }
    if (mintemp)
      if (units == "world") {
        filterArray.push("mintemp_c");
      } else {
        filterArray.push("mintemp");
      }
    if (avgtemp)
      if (units == "world") {
        filterArray.push("avgtemp_c");
      } else {
        filterArray.push("avgtemp_f");
      }
    if (maxwind)
      if (units == "world") {
        filterArray.push("maxwind_kph");
      } else {
        filterArray.push("maxwind_mph");
      }
    if (totalprecip)
      if (units == "world") {
        filterArray.push("totalprecip_mm");
      } else {
        filterArray.push("totalprecip_in");
      }
    if (avgvis)
      if (units == "world") {
        filterArray.push("avgis_km");
      } else {
        filterArray.push("avgvis_miles");
      }
EnesKarakas's avatar
csy    
EnesKarakas committed
629
630
631
632
633
    if (avghumidity) filterArray.push("avghumidity");
    if (daily_will_it_rain) filterArray.push("daily_will_it_rain");
    if (daily_chance_of_rain) filterArray.push("daily_chance_of_rain");
    if (daily_will_it_snow) filterArray.push("daily_will_it_snow");
    if (daily_chance_of_snow) filterArray.push("daily_chance_of_snow");
Weiser's avatar
Weiser committed
634
    if (uv) filterArray.push("uv_day");
EnesKarakas's avatar
csy    
EnesKarakas committed
635
636
637
638
639
640
641
642
    if (sunrise) filterArray.push("sunrise");
    if (sunset) filterArray.push("sunset");
    if (moonrise) filterArray.push("moonrise");
    if (moonset) filterArray.push("moonset");
    if (moon_phase) filterArray.push("moon_phase");
    if (moon_illumination) filterArray.push("moon_illumination");
    if (hour_time_epoch) filterArray.push("hourly_time_epoch");
    if (hour_time) filterArray.push("hourly_time");
EnesKarakas's avatar
dwa    
EnesKarakas committed
643
644
645
646
647
648
    if (hour_temp)
      if (units == "world") {
        filterArray.push("hourly_temp_c");
      } else {
        filterArray.push("hourly_temp_f");
      }
EnesKarakas's avatar
csy    
EnesKarakas committed
649
    if (hour_is_day) filterArray.push("hourly_is_day");
EnesKarakas's avatar
dwa    
EnesKarakas committed
650
651
652
653
654
655
    if (hour_wind)
      if (units == "world") {
        filterArray.push("hourly_wind_kph");
      } else {
        filterArray.push("hourly_wind_mph");
      }
EnesKarakas's avatar
csy    
EnesKarakas committed
656
657
    if (hour_wind_degree) filterArray.push("hourly_wind_degree");
    if (hour_wind_dir) filterArray.push("hourly_wind_dir");
EnesKarakas's avatar
dwa    
EnesKarakas committed
658
659
660
661
662
663
664
665
666
667
668
669
    if (hour_pressure)
      if (units == "world") {
        filterArray.push("hourly_pressure_mb");
      } else {
        filterArray.push("hourly_pressure_in");
      }
    if (hour_precip)
      if (units == "world") {
        filterArray.push("hourly_precip_mm");
      } else {
        filterArray.push("hourly_precip_in");
      }
EnesKarakas's avatar
csy    
EnesKarakas committed
670
671
    if (hour_humidity) filterArray.push("hourly_humidity");
    if (hour_cloud) filterArray.push("hourly_cloud");
EnesKarakas's avatar
dwa    
EnesKarakas committed
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
    if (hour_feelslike)
      if (units == "world") {
        filterArray.push("hourly_feelslike_c");
      } else {
        filterArray.push("hourly_feelslike_f");
      }
    if (hour_windchill)
      if (units == "world") {
        filterArray.push("hourly_windchill_c");
      } else {
        filterArray.push("hourly_windchill_f");
      }
    if (hour_heatindex)
      if (units == "world") {
        filterArray.push("hourly_heatindex_c");
      } else {
        filterArray.push("hourly_heatindex_f");
      }
    if (hour_dewpoint)
      if (units == "world") {
        filterArray.push("hourly_dewpoint_c");
      } else {
        filterArray.push("hourly_dewpoint_f");
      }
Weiser's avatar
Weiser committed
696
697
698
699
    if (hourly_will_it_rain) filterArray.push("hourly_will_it_rain");
    if (hourly_chance_of_rain) filterArray.push("hourly_chance_of_rain");
    if (hourly_will_it_snow) filterArray.push("hourly_will_it_snow");
    if (hourly_chance_of_snow) filterArray.push("hourly_chance_of_snow");
EnesKarakas's avatar
dwa    
EnesKarakas committed
700
701
702
703
704
705
706
707
708
709
710
711
    if (hour_vis)
      if (units == "world") {
        filterArray.push("hourly_vis_km");
      } else {
        filterArray.push("hourly_vis_miles");
      }
    if (hour_gust)
      if (units == "world") {
        filterArray.push("hourly_gust_kph");
      } else {
        filterArray.push("hourly_gust_mph");
      }
EnesKarakas's avatar
csy    
EnesKarakas committed
712
713
714
715
    if (hour_uv) filterArray.push("hourly_uv");

    let filterString = filterArray.join(",");

EnesKarakas's avatar
dfaw    
EnesKarakas committed
716
    const apiUrl = `http://localhost:8080/forecastweather?q=${latitude_text},${longitude_text}&days=${days}&filter=${filterString}&alerts=${alerts}`;
Weiser's avatar
asdf  
Weiser committed
717

EnesKarakas's avatar
fes    
EnesKarakas committed
718
    console.log(apiUrl);
Weiser's avatar
asdf  
Weiser committed
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
    fetch(apiUrl)
      .then((response) => {
        if (!response.ok) {
          throw new Error("Network response was not ok");
        }
        return response.json();
      })
      .then((data) => {
        // Wetterdaten anzeigen
        document.getElementById("weatherData").innerText = JSON.stringify(
          data,
          null,
          2
        );
        document.getElementById("apiUrloutput").value = apiUrl;
EnesKarakas's avatar
dawd    
EnesKarakas committed
734
        setweatherData(data);
Weiser's avatar
asdf  
Weiser committed
735
736
737
738
739
740
      })
      .catch((error) => {
        console.error("There was a problem with the fetch operation:", error);
      });
  }
  function searchAPI() {
EnesKarakas's avatar
csy    
EnesKarakas committed
741
    const cityInput = document.getElementById("city_text").value;
Weiser's avatar
asdf  
Weiser committed
742

EnesKarakas's avatar
csy    
EnesKarakas committed
743
    const apiUrl = `http://localhost:8080/search?city=${cityInput}`;
Weiser's avatar
asdf  
Weiser committed
744
745
746
747
748
749
750
751
752
753

    fetch(apiUrl)
      .then((response) => {
        if (!response.ok) {
          throw new Error("Network response was not ok");
        }

        return response.json();
      })
      .then((data) => {
EnesKarakas's avatar
csy    
EnesKarakas committed
754
        setData(data);
Weiser's avatar
asdf  
Weiser committed
755
756
757
758
759
760
761
762
763
764
      })
      .catch((error) => {
        console.error("There was a problem with the fetch operation:", error);
      });
  }

  function boolToWord(bool) {
    return bool ? "yes" : "no";
  }
};
EnesKarakas's avatar
csy    
EnesKarakas committed
765
766
767
768
769
770
771
772
const ListItem = ({ name, country, region, lat, lon }) => {
  const handleClick = () => {
    document.getElementById("city_text").value = name;
    document.getElementById("region_text").value = country;
    document.getElementById("country_text").value = region;
    document.getElementById("latitude_text").value = lat;
    document.getElementById("longitude_text").value = lon;
  };
Weiser's avatar
asdf  
Weiser committed
773

EnesKarakas's avatar
csy    
EnesKarakas committed
774
775
776
777
778
779
780
781
782
783
784
  return (
    <div className="list-item" onClick={handleClick}>
      <div className="title">{name}</div>
      <div className="subtitle">
        Country: {country} <br />
        Region: {region} <br />
        Lat: {lat}, Lon: {lon}
      </div>
    </div>
  );
};
EnesKarakas's avatar
fes    
EnesKarakas committed
785

EnesKarakas's avatar
csy    
EnesKarakas committed
786
export default WeatherForecastData;