All files / util/bookmarks/src sections.ts

100% Statements 33/33
87.5% Branches 7/8
100% Functions 11/11
100% Lines 32/32

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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95                        19x     105x       22x             6x   6x   6x       3x 3x   3x             4x 4x 4x   4x     4x 3x 3x               1x 1x 1x 1x   1x 1x   3x     1x         2x 2x 2x   2x             7x 7x 7x   7x    
import { filter, find, omit } from "lodash";
 
export interface BookmarkSectionProps {
  name: string;
  order: number;
  bookmarkedStops: number[];
}
 
export interface BookmarkSectionsProps {
  [id: number]: BookmarkSectionProps;
}
 
export const BOOKMARK_SECTIONS = "BOOKMARK_SECTIONS";
 
export function fetchStoredBookmarkSections(): BookmarkSectionsProps {
  return JSON.parse(localStorage.getItem(BOOKMARK_SECTIONS) ?? "null") || {};
}
 
function updateStoredBookmarkSections(bookmarkSections: BookmarkSectionsProps) {
  localStorage.setItem(BOOKMARK_SECTIONS, JSON.stringify(bookmarkSections));
}
 
export function storeBookmarkSection(
  nextId: number,
  bookmarkSection: BookmarkSectionProps
) {
  const bookmarkSections = fetchStoredBookmarkSections();
 
  bookmarkSections[nextId] = bookmarkSection;
 
  updateStoredBookmarkSections(bookmarkSections);
}
 
export function removeStoredBookmarkSection(bookmarkSectionId: number) {
  const bookmarkSections = fetchStoredBookmarkSections();
  const updatedBookmarkSections = omit(bookmarkSections, bookmarkSectionId);
 
  updateStoredBookmarkSections(updatedBookmarkSections);
}
 
export function updateStoredBookmarkSection(
  bookmarkSectionId: number,
  stopId: number
) {
  const bookmarkSections = fetchStoredBookmarkSections();
  const bookmarkSection = bookmarkSections[bookmarkSectionId];
  const bookmarkInSection = find(
    bookmarkSection.bookmarkedStops,
    stop => stop === stopId
  );
 
  if (!bookmarkInSection) {
    bookmarkSection.bookmarkedStops.push(stopId);
    updateStoredBookmarkSections(bookmarkSections);
  }
}
 
export function removeStoredBookmarkFromSection(
  bookmarkSectionId: number,
  stopId: number
) {
  const bookmarkSections = fetchStoredBookmarkSections();
  const bookmarkSection = bookmarkSections[bookmarkSectionId];
  const bookmarkSectionStops = bookmarkSection.bookmarkedStops;
  const bookmarkInSection = find(bookmarkSectionStops, stop => stop === stopId);
 
  Eif (bookmarkInSection) {
    bookmarkSections[bookmarkSectionId].bookmarkedStops = filter(
      bookmarkSectionStops,
      id => id !== stopId
    );
 
    updateStoredBookmarkSections(bookmarkSections);
  }
}
 
export function removeAllStoredBookmarksInSection(bookmarkSectionId: number) {
  const bookmarkSections = fetchStoredBookmarkSections();
  const bookmarkSection = bookmarkSections[bookmarkSectionId];
  bookmarkSection.bookmarkedStops = [];
 
  updateStoredBookmarkSections(bookmarkSections);
}
 
export function updateStoredBookmarkSectionName(
  bookmarkSectionId: number,
  bookmarkSectionName: string
) {
  const bookmarkSections = fetchStoredBookmarkSections();
  const bookmarkSection = bookmarkSections[bookmarkSectionId];
  bookmarkSection.name = bookmarkSectionName;
 
  updateStoredBookmarkSections(bookmarkSections);
}