2024-01-12 17:13:39 +02:00
|
|
|
import { Accessor, Component, createResource } from "solid-js";
|
|
|
|
|
import { apiHost } from "../consts";
|
|
|
|
|
import moment from "moment";
|
2024-01-15 23:22:45 +02:00
|
|
|
import { CityChart } from "../components/chart/CityChart";
|
2024-01-12 17:13:39 +02:00
|
|
|
|
|
|
|
|
export const Result: Component<{
|
2024-01-15 23:22:45 +02:00
|
|
|
getCity: Accessor<string | undefined>,
|
2024-01-12 17:13:39 +02:00
|
|
|
getField: Accessor<string>,
|
|
|
|
|
getStart: Accessor<Date>,
|
|
|
|
|
getEnd: Accessor<Date>,
|
|
|
|
|
}> = (props) => {
|
|
|
|
|
const queryStart = () => moment(props.getStart()).format("YYYYMMDD_HHmm");
|
|
|
|
|
const queryEnd = () => moment(props.getEnd()).format("YYYYMMDD_HHmm");
|
|
|
|
|
|
2024-01-16 16:18:52 +02:00
|
|
|
const fetchList = async ([city, field, getStart, getEnd]: [string | undefined, string, Date, Date]) => {
|
2024-01-12 17:13:39 +02:00
|
|
|
if (city === undefined) return undefined;
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 500));
|
2024-01-15 23:22:45 +02:00
|
|
|
const response = await fetch(`${apiHost}/api/query/city/${city}/${queryStart()}-${queryEnd()}/hour/${field}/list`);
|
2024-01-12 17:13:39 +02:00
|
|
|
const json = await response.json();
|
|
|
|
|
return json;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-16 16:18:52 +02:00
|
|
|
const fetchMeteo = async ([city, getStart, getEnd]: [string | undefined, Date, Date]) => {
|
2024-01-12 17:13:39 +02:00
|
|
|
if (city === undefined) return undefined;
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 500));
|
2024-01-16 16:18:52 +02:00
|
|
|
const response = await fetch(`${apiHost}/api/query/city/${city}/${queryStart()}-${queryEnd()}/allFields`);
|
2024-01-12 17:13:39 +02:00
|
|
|
const json = await response.json();
|
|
|
|
|
return json;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-16 16:18:52 +02:00
|
|
|
const cityFieldDate = (): [string | undefined, string, Date, Date] => [
|
|
|
|
|
props.getCity(),
|
|
|
|
|
props.getField(),
|
|
|
|
|
props.getStart(),
|
|
|
|
|
props.getEnd(),
|
|
|
|
|
];
|
|
|
|
|
const cityDate = (): [string | undefined, Date, Date] => [
|
|
|
|
|
props.getCity(),
|
|
|
|
|
props.getStart(),
|
|
|
|
|
props.getEnd(),
|
|
|
|
|
]
|
|
|
|
|
const [listResource] = createResource(cityFieldDate, fetchList);
|
|
|
|
|
const [meteoResource] = createResource(cityDate, fetchMeteo);
|
2024-01-06 23:48:23 +02:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
2024-01-12 17:13:39 +02:00
|
|
|
{
|
|
|
|
|
props.getCity() === undefined && <div>Select city!</div>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{ /* City weather field as chart */}
|
|
|
|
|
{ listResource.loading && (
|
|
|
|
|
<div>
|
|
|
|
|
<span class="spinner"></span>
|
|
|
|
|
<span style={{ "padding-left": "16px" }}>Loading chart</span>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{ listResource.error && (
|
|
|
|
|
<div>Error while querying: ${listResource.error}</div>
|
|
|
|
|
)}
|
|
|
|
|
{ listResource() && listResource() instanceof Error &&
|
|
|
|
|
<div>{ listResource().message }</div>
|
|
|
|
|
}
|
2024-01-15 23:22:45 +02:00
|
|
|
{ listResource() && props.getCity() && !listResource.loading &&
|
2024-01-12 17:13:39 +02:00
|
|
|
<div>
|
|
|
|
|
<h3>{ props.getCity() }</h3>
|
|
|
|
|
<CityChart
|
|
|
|
|
city={() => listResource().query.cities[0]}
|
|
|
|
|
data={() => listResource().result[listResource().query.cities[0]]}
|
|
|
|
|
query={() => listResource().query}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{ /* City all weather fields with double values */}
|
|
|
|
|
{ meteoResource.loading && (
|
|
|
|
|
<div>
|
|
|
|
|
<span class="spinner"></span>
|
|
|
|
|
<span style={{ "padding-left": "16px" }}>Loading meteo data</span>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{ meteoResource.error && (
|
|
|
|
|
<div>Error while querying: ${meteoResource.error}</div>
|
|
|
|
|
)}
|
|
|
|
|
{ meteoResource() && meteoResource() instanceof Error &&
|
|
|
|
|
<div>{ meteoResource().message }</div>
|
|
|
|
|
}
|
2024-01-15 23:22:45 +02:00
|
|
|
{ meteoResource() && props.getCity() && !meteoResource.loading &&
|
2024-01-12 17:13:39 +02:00
|
|
|
<ul>
|
|
|
|
|
{ Object.entries(meteoResource())
|
|
|
|
|
.sort((a, b) => a[0] > b[0] ? 1 : -1)
|
|
|
|
|
.map(([key, value]: any) =>
|
2024-01-15 23:22:45 +02:00
|
|
|
<li>{key}: {toStringFloat(value)}</li>
|
2024-01-12 17:13:39 +02:00
|
|
|
)}
|
|
|
|
|
</ul>
|
|
|
|
|
}
|
2024-01-06 23:48:23 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
2024-01-15 23:22:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function toStringFloat(val: any): string {
|
|
|
|
|
const numVal = parseFloat(val);
|
|
|
|
|
if (isNaN(numVal)) return "";
|
|
|
|
|
|
|
|
|
|
return (Math.round(numVal * 10) / 10) + "";
|
2024-01-06 23:48:23 +02:00
|
|
|
}
|