All files / util/bookmarks/src bookmarks.localstorage.ts

100% Statements 11/11
100% Branches 6/6
100% Functions 4/4
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35        19x             78x       9x       6x 6x   6x   6x       3x 3x   3x 3x    
import { omit } from "lodash";
import type { StopLocation } from "@overworld/trimet-client";
import { removeShortcutsForBookmark } from "./bookmarkShortcuts";
 
export const BOOKMARKS = "BOOKMARKS";
 
export interface StoredBookmarks {
  [locationId: number]: StopLocation;
}
 
export function fetchStoredBookmarks(): StoredBookmarks {
  return JSON.parse(localStorage.getItem(BOOKMARKS) ?? "null") || {};
}
 
function updateBookmarks(existingBookmarks: StoredBookmarks) {
  localStorage.setItem(BOOKMARKS, JSON.stringify(existingBookmarks));
}
 
export function storeLocationBookmark(stopLocation: StopLocation) {
  const existingBookmarks = fetchStoredBookmarks();
  const id = stopLocation.locid ? stopLocation.locid : stopLocation.id;
 
  existingBookmarks[id] = stopLocation;
 
  updateBookmarks(existingBookmarks);
}
 
export function removeStoredBookmark(locationId: number) {
  const bookmarks = fetchStoredBookmarks();
  const updatedBookmarks = omit(bookmarks, locationId);
 
  updateBookmarks(updatedBookmarks);
  removeShortcutsForBookmark("stop", locationId);
}