Refactor of map & other components

This commit is contained in:
ThePetrovich 2025-06-27 18:23:50 +08:00
parent 527d4417ff
commit c7df38e6ce
10 changed files with 532 additions and 466 deletions

View file

@ -1,62 +1,10 @@
import { writable } from "svelte/store"
import { writable } from "svelte/store";
import type { LatLngExpression } from "leaflet";
import L from "leaflet";
import { getCsrfToken } from "./auth";
interface TrajectoryPoint {
altitude: number;
datetime: string;
latitude: number;
longitude: number;
}
interface PredictionStage {
stage: string;
trajectory: TrajectoryPoint[];
}
interface ParsedPrediction {
flight_path: [number, number, number][];
launch: {
latlng: LatLngExpression;
datetime: Date;
};
burst: {
latlng: LatLngExpression;
datetime: Date;
};
landing: {
latlng: LatLngExpression;
datetime: Date;
};
profile: string;
flight_time: number;
}
export const latestPrediction = writable({
metadata: {
complete_datetime: "",
start_datetime: ""
},
prediction: [
{
stage: "",
trajectory: [
{
altitude: 0.0,
datetime: "",
latitude: 0.0,
longitude: 0.0
}
]
}
]
});
export const latestPredictionParsed = writable({} as ParsedPrediction);
import type { PredictionStage, Prediction, ParsedPrediction } from "./types";
import { latestPrediction, latestPredictionParsed } from "./stores";
function getLatestDataset() {
const now = new Date();
@ -72,22 +20,22 @@ function getLatestDataset() {
// Subtract 6 hours to account for the lag
roundedDate.setUTCHours(roundedDate.getUTCHours() - 6);
return roundedDate.toISOString();
return roundedDate.toISOString();
}
function formatLaunchDateTime(dateObj: string | Date, timeStr: string): string {
// Ensure date is a Date object
const date = new Date(dateObj);
// Extract date components
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
// Format time (ensure it has seconds)
let formattedTime = timeStr;
if (timeStr.split(':').length === 2) {
formattedTime += ':00'; // Add seconds if missing
if (timeStr.split(":").length === 2) {
formattedTime += ":00"; // Add seconds if missing
}
// Combine into ISO string
@ -96,12 +44,11 @@ function formatLaunchDateTime(dateObj: string | Date, timeStr: string): string {
return isoString;
}
export const getForecast = async (flightParameters: Record<string, any>, startDate: string, startTime: string): Promise<void> => {
const launch_datetime = formatLaunchDateTime(startDate, startTime);
export const getForecast = async (
flightParameters: Record<string, any>,
): Promise<void> => {
// Create request object
flightParameters.dataset = getLatestDataset();
flightParameters.launch_datetime = launch_datetime;
console.log("Sending request:", flightParameters);
@ -109,17 +56,17 @@ export const getForecast = async (flightParameters: Record<string, any>, startDa
// Example POST request - replace with your actual API endpoint
const csrfToken = await getCsrfToken();
if (!csrfToken) {
throw new Error('CSRF token not found');
throw new Error("CSRF token not found");
}
const response = await fetch('http://localhost:8000/api/predictions', {
method: 'POST',
const response = await fetch("http://localhost:8000/api/predictions", {
method: "POST",
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrfToken
"Content-Type": "application/json",
"X-CSRFToken": csrfToken,
},
body: JSON.stringify(flightParameters),
credentials: 'include'
credentials: "include",
});
if (!response.ok) {
@ -133,7 +80,7 @@ export const getForecast = async (flightParameters: Record<string, any>, startDa
latestPredictionParsed.set(parsePrediction(data.result.prediction));
alert("Forecast request successful!");
// Handle the response data as needed
// Handle the response data as needed
} catch (error) {
console.error("Error sending forecast request:", error);
alert("Error getting forecast: " + error);