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