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