feat(stores): update saved location functions
This commit is contained in:
@ -17,7 +17,7 @@
|
|||||||
selectedVehicle,
|
selectedVehicle,
|
||||||
} from "$lib/vehicles/vehicles.svelte";
|
} from "$lib/vehicles/vehicles.svelte";
|
||||||
import { location } from "../location.svelte";
|
import { location } from "../location.svelte";
|
||||||
import { saved } from "$lib/saved.svelte";
|
import { savedLocations } from "$lib/saved.svelte";
|
||||||
import { m } from "$lang/messages";
|
import { m } from "$lang/messages";
|
||||||
import LocationSelect from "../LocationSelect.svelte";
|
import LocationSelect from "../LocationSelect.svelte";
|
||||||
|
|
||||||
@ -74,8 +74,11 @@
|
|||||||
const FROM: WorldLocation =
|
const FROM: WorldLocation =
|
||||||
fromLocation == "current"
|
fromLocation == "current"
|
||||||
? { lat: location.lat, lon: location.lng }
|
? { lat: location.lat, lon: location.lng }
|
||||||
: saved[fromLocation]
|
: savedLocations.current.find(s => s.name == fromLocation)
|
||||||
? saved[fromLocation]
|
? {
|
||||||
|
lat: savedLocations.current.find(s => s.name == fromLocation)!.data.lat,
|
||||||
|
lon: savedLocations.current.find(s => s.name == fromLocation)!.data.lng,
|
||||||
|
}
|
||||||
: {
|
: {
|
||||||
lat: parseFloat(fromLocation.split(",")[0]),
|
lat: parseFloat(fromLocation.split(",")[0]),
|
||||||
lon: parseFloat(fromLocation.split(",")[1]),
|
lon: parseFloat(fromLocation.split(",")[1]),
|
||||||
@ -83,8 +86,11 @@
|
|||||||
const TO: WorldLocation =
|
const TO: WorldLocation =
|
||||||
toLocation == "current"
|
toLocation == "current"
|
||||||
? { lat: location.lat, lon: location.lng }
|
? { lat: location.lat, lon: location.lng }
|
||||||
: saved[toLocation]
|
: savedLocations.current.find(s => s.name == toLocation)
|
||||||
? saved[toLocation]
|
? {
|
||||||
|
lat: savedLocations.current.find(s => s.name == fromLocation)!.data.lat,
|
||||||
|
lon: savedLocations.current.find(s => s.name == fromLocation)!.data.lng,
|
||||||
|
}
|
||||||
: {
|
: {
|
||||||
lat: parseFloat(toLocation.split(",")[0]),
|
lat: parseFloat(toLocation.split(",")[0]),
|
||||||
lon: parseFloat(toLocation.split(",")[1]),
|
lon: parseFloat(toLocation.split(",")[1]),
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import {
|
|||||||
SchoolIcon,
|
SchoolIcon,
|
||||||
type IconProps,
|
type IconProps,
|
||||||
} from "@lucide/svelte";
|
} from "@lucide/svelte";
|
||||||
|
import { stores, storeSnapshot } from "./services/stores.svelte";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Use stores instead.
|
* @deprecated Use stores instead.
|
||||||
@ -15,6 +16,8 @@ export const saved: Record<string, WorldLocation> = $state(
|
|||||||
JSON.parse(localStorage.getItem("saved") ?? "{}"),
|
JSON.parse(localStorage.getItem("saved") ?? "{}"),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export const savedLocations = stores<Location>("location");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Use stores instead.
|
* @deprecated Use stores instead.
|
||||||
*/
|
*/
|
||||||
@ -22,13 +25,10 @@ export function saveLocations() {
|
|||||||
localStorage.setItem("saved", JSON.stringify(saved));
|
localStorage.setItem("saved", JSON.stringify(saved));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @deprecated Use stores instead.
|
|
||||||
*/
|
|
||||||
export async function geocode(name: string) {
|
export async function geocode(name: string) {
|
||||||
const loc = saved[name];
|
const loc = await storeSnapshot<Location>({ name, type: "location" });
|
||||||
if (!loc) return;
|
if (!loc) return;
|
||||||
const geocode = await reverseGeocode(loc);
|
const geocode = await reverseGeocode({ lat: loc.lat, lon: loc.lng });
|
||||||
if (geocode.length == 0) {
|
if (geocode.length == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -203,6 +203,18 @@ export async function hasStore(info: StoreInfo) {
|
|||||||
return store !== undefined;
|
return store !== undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function storeSnapshot<T extends object>(info: StoreInfo) {
|
||||||
|
const db = await getDB();
|
||||||
|
const store = await db.getFromIndex("stores", "by-name-and-type", [
|
||||||
|
info.name,
|
||||||
|
info.type,
|
||||||
|
]);
|
||||||
|
if (!store) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return JSON.parse(store.data) as T;
|
||||||
|
}
|
||||||
|
|
||||||
// export async function store<T extends object>(info: StoreInfo) {
|
// export async function store<T extends object>(info: StoreInfo) {
|
||||||
// const store = await db.getFromIndex("stores", "by-name-and-type", [info.name, info.type]);
|
// const store = await db.getFromIndex("stores", "by-name-and-type", [info.name, info.type]);
|
||||||
// if (!store) {
|
// if (!store) {
|
||||||
|
|||||||
Reference in New Issue
Block a user