2023-05-15 20:50:53 +03:00
|
|
|
import moment from "moment";
|
|
|
|
|
import { Accessor, Component, createResource, createSignal } from "solid-js";
|
2023-06-22 19:20:21 +03:00
|
|
|
|
2025-03-14 11:31:47 +02:00
|
|
|
import { FETCH_DELAY_MS, apiHost } from "../../../consts";
|
2023-08-07 23:25:35 +03:00
|
|
|
import { Result } from "./Result";
|
2025-03-14 11:31:47 +02:00
|
|
|
import { LoadingSpinner } from "../../../components/spinner/LoadingSpinner";
|
2023-05-15 20:50:53 +03:00
|
|
|
|
2023-12-24 23:54:16 +02:00
|
|
|
|
2023-08-07 23:25:35 +03:00
|
|
|
export const QueryResult: Component<{
|
2023-05-15 20:50:53 +03:00
|
|
|
getStart: Accessor<Date>,
|
|
|
|
|
getEnd: Accessor<Date>,
|
2023-12-24 23:54:16 +02:00
|
|
|
getFields: Accessor<string[]>,
|
2023-05-15 20:50:53 +03:00
|
|
|
}> = (props) => {
|
|
|
|
|
const queryStart = () => moment(props.getStart()).format("YYYYMMDD_HHmm");
|
|
|
|
|
const queryEnd = () => moment(props.getEnd()).format("YYYYMMDD_HHmm");
|
|
|
|
|
const [getTimestamp, setTimestamp] = createSignal<number>(0);
|
2023-12-24 23:54:16 +02:00
|
|
|
|
2023-05-15 20:50:53 +03:00
|
|
|
const fetchQuery = async (timestamp: number) => {
|
2023-12-24 23:54:16 +02:00
|
|
|
if (props.getFields().length === 0) return new Error("ERROR: Select weather parameters!");
|
2024-01-19 20:32:20 +02:00
|
|
|
await new Promise(resolve => setTimeout(resolve, FETCH_DELAY_MS))
|
2023-12-24 23:54:16 +02:00
|
|
|
const response = await fetch(`${apiHost}/api/query/country/${queryStart()}-${queryEnd()}/${props.getFields().join(",")}`);
|
2023-05-15 20:50:53 +03:00
|
|
|
const json = await response.json();
|
|
|
|
|
return json;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [queryResource] = createResource(getTimestamp, fetchQuery);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
2023-06-22 19:20:21 +03:00
|
|
|
<input
|
|
|
|
|
type="button"
|
|
|
|
|
class="primary"
|
|
|
|
|
value="Query Data"
|
|
|
|
|
onClick={() => setTimestamp(Date.now())}
|
|
|
|
|
/>
|
2024-01-19 20:32:20 +02:00
|
|
|
{ queryResource.loading && <LoadingSpinner text="Loading query" /> }
|
2023-05-23 00:46:07 +03:00
|
|
|
{ queryResource.error && (
|
|
|
|
|
<div>Error while querying: ${queryResource.error}</div>
|
|
|
|
|
)}
|
|
|
|
|
{ queryResource() && queryResource() instanceof Error &&
|
|
|
|
|
<div>{ queryResource().message }</div>
|
|
|
|
|
}
|
2023-12-24 23:54:16 +02:00
|
|
|
{ queryResource() &&
|
2023-08-07 23:25:35 +03:00
|
|
|
<Result result={queryResource} />
|
|
|
|
|
}
|
2023-05-15 20:50:53 +03:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|