currentData.js 16.3 KB
Newer Older
EnesKarakas's avatar
dw    
EnesKarakas committed
1
import React, { useState } from "react";
Weiser's avatar
Weiser committed
2

Weiser's avatar
fusion    
Weiser committed
3
import "./currentData.css";
Weiser's avatar
Weiser committed
4

Weiser's avatar
fusion    
Weiser committed
5
const Currentdata = (props) => {
EnesKarakas's avatar
dw    
EnesKarakas committed
6
7
8
9
10
11
12
13
14
15
  const [query, setQuery] = useState("");
  const [data, setData] = useState([]);

  const handleInputChange = (e) => {
    setQuery(e.target.value);
    if (e.target.value.trim() !== "") {
      searchAPI(e.target.value.trim());
    }
  };

EnesKarakas's avatar
dw    
EnesKarakas committed
16
17
18
19
20
  return (
    <div className="home-container">
      <div>
        <div className="thq-grid-5">
          <div class="filter">
EnesKarakas's avatar
csy    
EnesKarakas committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
            <label htmlFor="location">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
dw    
EnesKarakas committed
39
40
            <br />
            <label for="region">Region:</label>
EnesKarakas's avatar
dwa    
EnesKarakas committed
41
            <input type="text" id="region_text" placeholder="Region" />
EnesKarakas's avatar
dw    
EnesKarakas committed
42
43
            <br />
            <label>Country:</label>
EnesKarakas's avatar
dwa    
EnesKarakas committed
44
            <input type="text" id="country_text" placeholder="Country" />
EnesKarakas's avatar
dw    
EnesKarakas committed
45
46
            <br />
            <label>Latitude:</label>
EnesKarakas's avatar
dwa    
EnesKarakas committed
47
            <input type="text" id="latitude_text" placeholder="Latitude" />
EnesKarakas's avatar
dw    
EnesKarakas committed
48
            <label>Longitude:</label>
EnesKarakas's avatar
dwa    
EnesKarakas committed
49
            <input type="text" id="longitude_text" placeholder="Longitude" />
EnesKarakas's avatar
dw    
EnesKarakas committed
50
51
            <br />
          </div>
EnesKarakas's avatar
dw    
EnesKarakas committed
52
          <div>
EnesKarakas's avatar
dw    
EnesKarakas committed
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
            <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>
            <br />
            <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 />
Weiser's avatar
asdf    
Weiser committed
95
            <input type="checkbox" id="temp" className="checkBoxFilter" />
Weiser's avatar
asdf    
Weiser committed
96
            <label for="temp">Temp</label>
EnesKarakas's avatar
dw    
EnesKarakas committed
97
98
99
100
101
102
103
104
105
            <br />
            <input type="checkbox" id="id_day" className="checkBoxFilter" />
            <label for="id_day">ID Day</label>
            <br />
            <input
              type="checkbox"
              id="condition_text"
              className="checkBoxFilter"
            />
Weiser's avatar
geht    
Weiser committed
106
            <label for="condition">Condition Text</label>
EnesKarakas's avatar
dw    
EnesKarakas committed
107
108
109
            <br />
            <input
              type="checkbox"
Weiser's avatar
geht    
Weiser committed
110
              id="condition"
EnesKarakas's avatar
dw    
EnesKarakas committed
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
              className="checkBoxFilter"
            />
            <input
              type="checkbox"
              id="wind_degree"
              className="checkBoxFilter"
            />
            <label for="wind_degree">Wind Degree</label>
            <br />
            <input type="checkbox" id="wind_dir" className="checkBoxFilter" />
            <label for="wind_dir">Wind Direction</label>
            <br />
            <input type="checkbox" id="pressure" className="checkBoxFilter" />
            <label for="pressure">Pressure</label>
            <br />
            <input type="checkbox" id="precip" className="checkBoxFilter" />
            <label for="precip">Precip</label>
            <br />
            <input type="checkbox" id="humidity" className="checkBoxFilter" />
            <label for="humidity">Humidity</label>
            <br />
            <input type="checkbox" id="cloud" className="checkBoxFilter" />
            <label for="cloud">Cloud</label>
            <br />
            <input type="checkbox" id="feelslike" className="checkBoxFilter" />
            <label for="feelslike">Feelslike</label>
            <br />
            <input type="checkbox" id="windchill" className="checkBoxFilter" />
            <label for="windchill">Windchill</label>
            <br />
            <input type="checkbox" id="heatindex" className="checkBoxFilter" />
            <label for="heatindex">Heatindex</label>
            <br />
            <input type="checkbox" id="dewpoint" className="checkBoxFilter" />
            <label for="dewpoint">Dewpoint</label>
            <br />
            <input type="checkbox" id="vis" className="checkBoxFilter" />
            <label for="vis">Visibility </label>
            <br />
            <input type="checkbox" id="uv" className="checkBoxFilter" />
            <label for="uv">UV Index</label>
            <br />
            <input type="checkbox" id="gust" className="checkBoxFilter" />
            <label for="gust">Gust </label>
            <br />
            <input
              type="checkbox"
              id="air_quality_co"
              className="checkBoxFilter"
            />
            <label for="air_quality_co">Air Quality CO</label>
            <br />
            <input
              type="checkbox"
              id="air_quality_no2"
              className="checkBoxFilter"
            />
            <label for="air_quality_no2">Air Quality NO2</label>
            <br />
            <input
              type="checkbox"
              id="air_quality_o3"
              className="checkBoxFilter"
            />
            <label for="air_quality_o3">Air Quality O3</label>
            <br />
            <input
              type="checkbox"
              id="air_quality_so2"
              className="checkBoxFilter"
            />
            <label for="air_quality_so2">Air Quality SO2</label>
            <br />
            <input
              type="checkbox"
              id="air_quality_pm2_5"
              className="checkBoxFilter"
            />
            <label for="air_quality_pm2_5">Air Quality PM2.5</label>
            <br />
            <input
              type="checkbox"
              id="air_quality_pm10"
              className="checkBoxFilter"
            />
            <label for="air_quality_pm10">Air Quality PM10</label>
            <br />
            <input
              type="checkbox"
              id="air_quality_us_epa_index"
              className="checkBoxFilter"
            />
            <label for="air_quality_us_epa_index">US EPA Index</label>
            <br />
            <input
              type="checkbox"
              id="air_quality_gb_defra_index"
              className="checkBoxFilter"
            />
            <label for="air_quality_gb_defra_index">GB DEFRA Index</label>
            <br />
Weiser's avatar
yippie    
Weiser committed
212
            <div>
EnesKarakas's avatar
EnesKarakas committed
213
              <label for="units">Choose a unit:</label>
Weiser's avatar
yippie    
Weiser committed
214
215
              <select name="unnits" id="units">
                <option value="world">Rest of The World</option>
EnesKarakas's avatar
EnesKarakas committed
216
217
218
                <option value="american">
                  American units: /eagles per shool shootings
                </option>
Weiser's avatar
yippie    
Weiser committed
219
              </select>
EnesKarakas's avatar
EnesKarakas committed
220
            </div>
EnesKarakas's avatar
dwa    
EnesKarakas committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
            <div>
              <button className="thq-button-filled" onClick={getData}>
                Generate
              </button>
            </div>
            <br />
            <div class="output">
              <h2>Output:</h2>
              <br />
              <pre id="weatherData"></pre>
            </div>
            <div id="apiUrl">
              <label>API URL:</label>
              <br />
              <input className="input" type="text" id="apiUrloutput" readOnly />
            </div>
EnesKarakas's avatar
dw    
EnesKarakas committed
237
238
            <br />
            <br />
Weiser's avatar
Weiser committed
239
240
          </div>
        </div>
EnesKarakas's avatar
dw    
EnesKarakas committed
241
242
243
      </div>
    </div>
  );
Weiser's avatar
Weiser committed
244
245

  function getData() {
EnesKarakas's avatar
adw    
EnesKarakas committed
246
    //switch
Weiser's avatar
if    
Weiser committed
247
248
    const units = document.getElementById("units").value;

EnesKarakas's avatar
dw    
EnesKarakas committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
    const city_text = document.getElementById("city_text").value;
    const region_text = document.getElementById("region_text").value;
    const country_text = document.getElementById("country_text").value;
    const latitude_text = document.getElementById("latitude_text").value;
    const longitude_text = document.getElementById("longitude_text").value;

    //Checkboxes
    //location
    const name = document.getElementById("name").checked;
    const region = document.getElementById("region").checked;
    const country = 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;
    //current
EnesKarakas's avatar
dawd    
EnesKarakas committed
266
267
268
    const last_updated_epoch =
      document.getElementById("last_updated_epoch").checked;
    const last_updated = document.getElementById("last_updated").checked;
Weiser's avatar
XYC    
Weiser committed
269
    const temp = document.getElementById("temp").checked;
EnesKarakas's avatar
dawd    
EnesKarakas committed
270
    const id_day = document.getElementById("id_day").checked;
Weiser's avatar
geht    
Weiser committed
271
    const condition = document.getElementById("condition_text").checked;
EnesKarakas's avatar
dawd    
EnesKarakas committed
272
273
274
    const wind = document.getElementById("wind").checked;
    const wind_degree = document.getElementById("wind_degree").checked;
    const wind_dir = document.getElementById("wind_dir").checked;
Weiser's avatar
Weiser committed
275
    const pressure = document.getElementById("pressure").checked;
EnesKarakas's avatar
dawd    
EnesKarakas committed
276
    const precip = document.getElementById("precip").checked;
Weiser's avatar
Weiser committed
277
278
    const humidity = document.getElementById("humidity").checked;
    const cloud = document.getElementById("cloud").checked;
EnesKarakas's avatar
dawd    
EnesKarakas committed
279
280
281
282
283
    const feelslike = document.getElementById("feelslike").checked;
    const windchill = document.getElementById("windchill").checked;
    const heatindex = document.getElementById("heatindex").checked;
    const dewpoint = document.getElementById("dewpoint").checked;
    const vis = document.getElementById("vis").checked;
Weiser's avatar
Weiser committed
284
285
    const uv = document.getElementById("uv").checked;
    const gust = document.getElementById("gust").checked;
EnesKarakas's avatar
dawd    
EnesKarakas committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
    const air_quality_co = document.getElementById("air_quality_co").checked;
    const air_quality_no2 = document.getElementById("air_quality_no2").checked;
    const air_quality_o3 = document.getElementById("air_quality_o3").checked;
    const air_quality_so2 = document.getElementById("air_quality_so2").checked;
    const air_quality_pm2_5 =
      document.getElementById("air_quality_pm2_5").checked;
    const air_quality_pm10 =
      document.getElementById("air_quality_pm10").checked;
    const air_quality_us_epa_index = document.getElementById(
      "air_quality_us_epa_index"
    ).checked;
    const air_quality_gb_defra_index = document.getElementById(
      "air_quality_gb_defra_index"
    ).checked;
Weiser's avatar
Weiser committed
300

EnesKarakas's avatar
dw    
EnesKarakas committed
301
    let filterArray = [];
EnesKarakas's avatar
adw    
EnesKarakas committed
302

EnesKarakas's avatar
dawd    
EnesKarakas committed
303
    if (name) {
EnesKarakas's avatar
dw    
EnesKarakas committed
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
      filterArray.push("name");
    }
    if (region) {
      filterArray.push("region");
    }
    if (country) {
      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");
    }
EnesKarakas's avatar
dawd    
EnesKarakas committed
327
328
329
330
331
332
    if (last_updated_epoch) {
      filterArray.push("last_updated_epoch");
    }
    if (last_updated) {
      filterArray.push("last_updated");
    }
Weiser's avatar
asdf    
Weiser committed
333
    if (temp) {
Weiser's avatar
asdf    
Weiser committed
334
      if (units == "world") {
EnesKarakas's avatar
adw    
EnesKarakas committed
335
        filterArray.push("temp_c");
EnesKarakas's avatar
dawd    
EnesKarakas committed
336
337
      } else {
        filterArray.push("temp_f");
Weiser's avatar
else    
Weiser committed
338
      }
EnesKarakas's avatar
dawd    
EnesKarakas committed
339
    }
Weiser's avatar
if    
Weiser committed
340

EnesKarakas's avatar
dawd    
EnesKarakas committed
341
342
343
    if (id_day) {
      filterArray.push("id_day");
    }
Weiser's avatar
geht    
Weiser committed
344
345
    if (condition) {
      filterArray.push("condition");
EnesKarakas's avatar
dawd    
EnesKarakas committed
346
347
    }
    if (wind) {
Weiser's avatar
asdf    
Weiser committed
348
      if (units == "world") {
Weiser's avatar
sdfg    
Weiser committed
349
        filterArray.push("wind_kph");
EnesKarakas's avatar
EnesKarakas committed
350
      } else {
Weiser's avatar
if    
Weiser committed
351
352
        filterArray.push("wind_mph");
      }
EnesKarakas's avatar
dawd    
EnesKarakas committed
353
354
355
356
357
358
359
360
    }
    if (wind_degree) {
      filterArray.push("wind_degree");
    }
    if (wind_dir) {
      filterArray.push("wind_dir");
    }
    if (pressure) {
Weiser's avatar
asdf    
Weiser committed
361
      if (units == "world") {
Weiser's avatar
if    
Weiser committed
362
        filterArray.push("temp_mb");
EnesKarakas's avatar
EnesKarakas committed
363
364
      } else {
        filterArray.push("pressure_in");
Weiser's avatar
if    
Weiser committed
365
366
      }
    }
EnesKarakas's avatar
adw    
EnesKarakas committed
367
    if (precip) {
Weiser's avatar
asdf    
Weiser committed
368
      if (units == "world") {
EnesKarakas's avatar
adw    
EnesKarakas committed
369
        filterArray.push("temp_mm");
EnesKarakas's avatar
EnesKarakas committed
370
371
      } else {
        filterArray.push("precip_in");
EnesKarakas's avatar
adw    
EnesKarakas committed
372
      }
EnesKarakas's avatar
dawd    
EnesKarakas committed
373
374
375
376
377
378
379
380
    }
    if (humidity) {
      filterArray.push("humidity");
    }
    if (cloud) {
      filterArray.push("cloud");
    }
    if (feelslike) {
Weiser's avatar
asdf    
Weiser committed
381
      if (units == "world") {
Weiser's avatar
if    
Weiser committed
382
        filterArray.push("feelslike_c");
EnesKarakas's avatar
EnesKarakas committed
383
384
      } else {
        filterArray.push("feelslike_f");
Weiser's avatar
if    
Weiser committed
385
      }
EnesKarakas's avatar
dawd    
EnesKarakas committed
386
387
    }
    if (windchill) {
Weiser's avatar
asdf    
Weiser committed
388
      if (units == "world") {
Weiser's avatar
if    
Weiser committed
389
        filterArray.push("windchill_c");
EnesKarakas's avatar
EnesKarakas committed
390
391
      } else {
        filterArray.push("windchill_f");
Weiser's avatar
if    
Weiser committed
392
      }
EnesKarakas's avatar
dawd    
EnesKarakas committed
393
394
    }
    if (heatindex) {
Weiser's avatar
asdf    
Weiser committed
395
      if (units == "world") {
Weiser's avatar
if    
Weiser committed
396
        filterArray.push("heatindex_c");
EnesKarakas's avatar
EnesKarakas committed
397
398
      } else {
        filterArray.push("heatindex_f");
Weiser's avatar
if    
Weiser committed
399
      }
EnesKarakas's avatar
dawd    
EnesKarakas committed
400
401
    }
    if (dewpoint) {
Weiser's avatar
asdf    
Weiser committed
402
      if (units == "world") {
Weiser's avatar
if    
Weiser committed
403
        filterArray.push("dewpoint_c");
EnesKarakas's avatar
EnesKarakas committed
404
405
      } else {
        filterArray.push("dewpoint_f");
Weiser's avatar
if    
Weiser committed
406
      }
EnesKarakas's avatar
dawd    
EnesKarakas committed
407
408
    }
    if (vis) {
Weiser's avatar
asdf    
Weiser committed
409
      if (units == "world") {
Weiser's avatar
geht    
Weiser committed
410
        filterArray.push("vis_km");
EnesKarakas's avatar
EnesKarakas committed
411
      } else {
Weiser's avatar
if    
Weiser committed
412
413
        filterArray.push("vis_miles");
      }
EnesKarakas's avatar
dawd    
EnesKarakas committed
414
415
416
417
418
    }
    if (uv) {
      filterArray.push("uv");
    }
    if (gust) {
Weiser's avatar
asdf    
Weiser committed
419
      if (units == "world") {
Weiser's avatar
if    
Weiser committed
420
        filterArray.push("gust_kph");
EnesKarakas's avatar
EnesKarakas committed
421
422
      } else {
        filterArray.push("gust_mph");
Weiser's avatar
if    
Weiser committed
423
      }
EnesKarakas's avatar
dawd    
EnesKarakas committed
424
425
    }
    if (air_quality_co) {
Weiser's avatar
geht    
Weiser committed
426
      filterArray.push("co");
EnesKarakas's avatar
dawd    
EnesKarakas committed
427
428
    }
    if (air_quality_no2) {
Weiser's avatar
geht    
Weiser committed
429
      filterArray.push("no2");
EnesKarakas's avatar
dawd    
EnesKarakas committed
430
431
    }
    if (air_quality_o3) {
Weiser's avatar
geht    
Weiser committed
432
      filterArray.push("o3");
EnesKarakas's avatar
dawd    
EnesKarakas committed
433
434
    }
    if (air_quality_so2) {
Weiser's avatar
geht    
Weiser committed
435
      filterArray.push("so2");
EnesKarakas's avatar
dawd    
EnesKarakas committed
436
437
    }
    if (air_quality_pm2_5) {
Weiser's avatar
geht    
Weiser committed
438
      filterArray.push("pm2_5");
EnesKarakas's avatar
dawd    
EnesKarakas committed
439
440
    }
    if (air_quality_pm10) {
Weiser's avatar
geht    
Weiser committed
441
      filterArray.push("pm10");
EnesKarakas's avatar
dawd    
EnesKarakas committed
442
443
    }
    if (air_quality_us_epa_index) {
Weiser's avatar
geht    
Weiser committed
444
      filterArray.push("us-epa-index");
EnesKarakas's avatar
dawd    
EnesKarakas committed
445
446
    }
    if (air_quality_gb_defra_index) {
Weiser's avatar
geht    
Weiser committed
447
      filterArray.push("gb-defra-index");
EnesKarakas's avatar
dawd    
EnesKarakas committed
448
449
    }

EnesKarakas's avatar
dw    
EnesKarakas committed
450
451
    let filterString = filterArray.join(",");

EnesKarakas's avatar
csy    
EnesKarakas committed
452
    const apiUrl = `http://localhost:8080/currentweather?q=${latitude_text},${longitude_text}&filter=${filterString}`;
Weiser's avatar
Weiser committed
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474

    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;
      })
      .catch((error) => {
        console.error("There was a problem with the fetch operation:", error);
      });
  }
  function searchAPI() {
EnesKarakas's avatar
dw    
EnesKarakas committed
475
    const cityInput = document.getElementById("city_text").value;
Weiser's avatar
Weiser committed
476

EnesKarakas's avatar
dw    
EnesKarakas committed
477
    const apiUrl = `http://localhost:8080/search?city=${cityInput}`;
Weiser's avatar
Weiser committed
478
479
480
481
482
483
484
485
486
487

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

        return response.json();
      })
      .then((data) => {
EnesKarakas's avatar
dw    
EnesKarakas committed
488
        setData(data);
Weiser's avatar
Weiser committed
489
490
491
492
493
494
495
496
497
498
499
      })
      .catch((error) => {
        console.error("There was a problem with the fetch operation:", error);
      });
  }

  function boolToWord(bool) {
    return bool ? "yes" : "no";
  }
};

EnesKarakas's avatar
dw    
EnesKarakas committed
500
501
const ListItem = ({ name, country, region, lat, lon }) => {
  const handleClick = () => {
EnesKarakas's avatar
csy    
EnesKarakas committed
502
503
504
505
506
    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;
EnesKarakas's avatar
dw    
EnesKarakas committed
507
508
509
510
511
512
513
514
515
516
517
518
519
520
  };

  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
dw    
EnesKarakas committed
521
export default Currentdata;