import React, { useState } from "react";
import "./currentData.css";
const Currentdata = (props) => {
const [query, setQuery] = useState("");
const [data, setData] = useState([]);
const [weatherdata, setweatherData] = useState([]);
const handleInputChange = (e) => {
setQuery(e.target.value);
if (e.target.value.trim() !== "") {
searchAPI(e.target.value.trim());
}
};
const downloadData = () => {
const fileformat = document.getElementById("fileformat").value;
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) => {
var xml = jsonToXml(weatherdata);
xml =
"" +
xml +
"";
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");
};
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 + ">";
}
var xml = xml.replace(/<\/?[0-9]{1,}>/g, "");
return xml;
};
const jsonToCsv = (json) => {
const keys = Object.keys(json);
const values = Object.values(json);
return `${keys.join(",")}\n${values.join(",")}`;
};
return (
Choose your City
Choose your Location
{data.map((item) => (
))}
Choose your custom output Data
Choose a unit
Choose a Fileformat
);
function getData() {
//switch
const units = document.getElementById("units").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
const last_updated_epoch =
document.getElementById("last_updated_epoch").checked;
const last_updated = document.getElementById("last_updated").checked;
const temp = document.getElementById("temp").checked;
const id_day = document.getElementById("id_day").checked;
const condition = document.getElementById("condition_text").checked;
const wind = document.getElementById("wind").checked;
const wind_degree = document.getElementById("wind_degree").checked;
const wind_dir = document.getElementById("wind_dir").checked;
const pressure = document.getElementById("pressure").checked;
const precip = document.getElementById("precip").checked;
const humidity = document.getElementById("humidity").checked;
const cloud = document.getElementById("cloud").checked;
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;
const uv = document.getElementById("uv").checked;
const gust = document.getElementById("gust").checked;
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;
let filterArray = [];
if (name) {
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");
}
if (last_updated_epoch) {
filterArray.push("last_updated_epoch");
}
if (last_updated) {
filterArray.push("last_updated");
}
if (temp) {
if (units == "world") {
filterArray.push("temp_c");
} else {
filterArray.push("temp_f");
}
}
if (id_day) {
filterArray.push("id_day");
}
if (condition) {
filterArray.push("condition");
}
if (wind) {
if (units == "world") {
filterArray.push("wind_kph");
} else {
filterArray.push("wind_mph");
}
}
if (wind_degree) {
filterArray.push("wind_degree");
}
if (wind_dir) {
filterArray.push("wind_dir");
}
if (pressure) {
if (units == "world") {
filterArray.push("temp_mb");
} else {
filterArray.push("pressure_in");
}
}
if (precip) {
if (units == "world") {
filterArray.push("temp_mm");
} else {
filterArray.push("precip_in");
}
}
if (humidity) {
filterArray.push("humidity");
}
if (cloud) {
filterArray.push("cloud");
}
if (feelslike) {
if (units == "world") {
filterArray.push("feelslike_c");
} else {
filterArray.push("feelslike_f");
}
}
if (windchill) {
if (units == "world") {
filterArray.push("windchill_c");
} else {
filterArray.push("windchill_f");
}
}
if (heatindex) {
if (units == "world") {
filterArray.push("heatindex_c");
} else {
filterArray.push("heatindex_f");
}
}
if (dewpoint) {
if (units == "world") {
filterArray.push("dewpoint_c");
} else {
filterArray.push("dewpoint_f");
}
}
if (vis) {
if (units == "world") {
filterArray.push("vis_km");
} else {
filterArray.push("vis_miles");
}
}
if (uv) {
filterArray.push("uv");
}
if (gust) {
if (units == "world") {
filterArray.push("gust_kph");
} else {
filterArray.push("gust_mph");
}
}
if (
air_quality_co ||
air_quality_no2 ||
air_quality_o3 ||
air_quality_so2 ||
air_quality_pm2_5 ||
air_quality_pm10 ||
air_quality_us_epa_index ||
air_quality_gb_defra_index
) {
filterArray.push("air_quality");
}
if (air_quality_co) {
filterArray.push("co");
}
if (air_quality_no2) {
filterArray.push("no2");
}
if (air_quality_o3) {
filterArray.push("o3");
}
if (air_quality_so2) {
filterArray.push("so2");
}
if (air_quality_pm2_5) {
filterArray.push("pm2_5");
}
if (air_quality_pm10) {
filterArray.push("pm10");
}
if (air_quality_us_epa_index) {
filterArray.push("us-epa-index");
}
if (air_quality_gb_defra_index) {
filterArray.push("gb-defra-index");
}
let filterString = filterArray.join(",");
const apiUrl = `http://localhost:8080/currentweather?q=${latitude_text},${longitude_text}&filter=${filterString}`;
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;
setweatherData(data);
})
.catch((error) => {
console.error("There was a problem with the fetch operation:", error);
});
}
function searchAPI() {
const cityInput = document.getElementById("city_text").value;
const apiUrl = `http://localhost:8080/search?city=${cityInput}`;
fetch(apiUrl)
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => {
setData(data);
})
.catch((error) => {
console.error("There was a problem with the fetch operation:", error);
});
}
};
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;
};
return (
{name}
Country: {country}
Region: {region}
Lat: {lat}, Lon: {lon}
);
};
export default Currentdata;