dev #160

Merged
sora merged 97 commits from dev into master 2025-03-02 01:09:30 +01:00
Showing only changes of commit 9f88d80b68 - Show all commits

View File

@ -1,41 +1,59 @@
import axios from "axios"; export const throttle = (func: () => void, wait: number) => {
let lastTime = 0;
let timeoutId: ReturnType<typeof setTimeout> | undefined;
let lastArgs: any[];
return function (...args: any[]) {
const now = Date.now();
lastArgs = args;
if (now - lastTime >= wait) {
lastTime = now;
func.apply(this, lastArgs);
} else {
if (timeoutId) clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
lastTime = Date.now();
func.apply(this, lastArgs);
}, wait - (now - lastTime));
}
};
}
export const load = (id: string) => export const load = (id: string) =>
axios.get("/api/" + id).then((response) => { fetch("/api/" + id).then(res => {
if (response.data == "") throw "Invalid Journey Data Received"; if (!res.ok) throw new Error('Error ' + res.statusText);
let res = response.data; return res.json();
}).then((res) => {
for (let e of res.main) { for (let e of res.main) {
if (e.dateRange) { if (e.date_range) {
e.dateRange[0] = new Date(e.dateRange[0]); e.date_range[0] = new Date(e.date_range[0]);
e.dateRange[1] = new Date(e.dateRange[1]); e.date_range[1] = new Date(e.date_range[1]);
} }
e.step_title = e.step_title || []; e.step_title = e.step_title || [];
} }
return res; return res;
}); })
export const save = (id: string, v: string) => export const save = (id: string, v: journey) =>
axios fetch("/api/" + id, { method: "post", body: JSON.stringify(v) })
.post("/api/" + id, v) .then(res => {
.then((response) => { if (!res.ok) throw new Error('Error ' + res.statusText);
return res.json();
}).then((_res) => {
console.log("Saved..."); console.log("Saved...");
}) })
.catch((error) => {
console.warn("Error! Could not reach the API.");
});
export const query_nominatim = ( export const query_nominatim = (
q: string, q: string,
f: (v: string) => Boolean = () => true, f: (v: string) => Boolean = () => true,
) => ) =>
axios fetch("/api/place/" + q)
.get("/api/place/" + q) .then((res) => (res.status == 200) ? res.json() : [])
.then((res) => res.data)
.then((res) => res.filter(f)); .then((res) => res.filter(f));
export const query_flight = (q: string) => export const query_flight = (q: string) =>
axios.get("/api/flight/" + q).then((res) => res.data); fetch("/api/flight/" + q).then((res) => res.json());
type NominatimResult = { type NominatimResult = {
type: string; type: string;
@ -69,54 +87,25 @@ export const is_attraction_type = (e: NominatimResult): boolean =>
export const icon_type = (item: NominatimResult): string => { export const icon_type = (item: NominatimResult): string => {
let t = item.type; let t = item.type;
let c = item.category; let c = item.category;
const arr = ["restaurant", "cafe", "pub", "bar", "fast_food", "food_court"]; let types = {
if (arr.indexOf(t) != -1) { "utensils": ["restaurant", "cafe", "pub", "bar", "fast_food", "food_court"],
return "utensils"; "bed": ["hotel", "hostel", "guest_house"],
} else if (t == "hotel" || t == "hostel" || t == "guest_house") { "landmark": ["museum", "historic", "place_of_worship", "attraction", "information", "university"],
return "bed"; "mountain": ["peak", "viewpoint"],
} else if (t == "museum" || c == "historic" || t == "place_of_worship") { "parking": ["parking"],
return "landmark"; "water": ["water", "river", "lake", "torrent", "aquarium"],
} else if (t == "peak" || t == "viewpoint") { "building": ["community_center", "locality"],
return "mountain"; "archway": ["bridge"],
} else if (t == "parking") { "tree": ["woodland", "shieling", "national_park", "park", "zoo", "garden"],
return "parking"; "dice-five": ["water_park", "theme_park", "casino"],
} else if ( "": ["?", "neighbourhood", "quarter", "highway"],
t == "water" || }
t == "river" ||
t == "lake" || for (let k in types) {
t == "torrent" || if (types[k].indexOf(t) >= 0 || types[k].indexOf(c) >= 0)
t == "aquarium" return k;
) { }
return "water";
} else if (t == "community_centre" || t == "locality") {
return "building";
} else if (t == "attraction") {
return "landmark";
} else if (t == "information" || t == "university") {
return "landmark";
} else if (t == "bridge") {
return "archway";
} else if (
t == "woodland" ||
t == "shieling" ||
t == "national_park" ||
t == "zoo" ||
t == "park" ||
t == "garden" ||
0
) {
return "tree";
} else if (t == "water_park" || t == "theme_park") {
return "dice-five";
} else if (
t == "?" ||
t == "neighbourhood" ||
t == "quarter" ||
c == "highway"
) {
return "";
} else {
console.log(item.display_name, item.category, item.type); console.log(item.display_name, item.category, item.type);
return "question"; return "question";
}
}; };