All files / applications/go-by-transit/src/view/home BookmarksCard.tsx

0% Statements 0/13
0% Branches 0/2
0% Functions 0/10
0% Lines 0/13

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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119                                                                                                                                                                                                                                             
import {
  Card,
  CardBody,
  CardHeader,
  ListGroup,
  ListItem
} from "@wintermuted/ui-react";
import { isEmpty, map } from "lodash";
import React, { useEffect, useState } from "react";
import FontAwesome from "react-fontawesome";
import { Link } from "react-router-dom";
import { StoredBookmarks } from "../../api/localstorage/bookmarks.localstorage";
import {
  StopData,
  StopLocation,
  TrimetRoute
} from "../../api/trimet/interfaces/types";
import { getNearbyStops } from "../../api/trimet/stops";
import RouteIndicator from "../../component/route/RouteIndicator";
 
interface BookmarkProps {
  index: string;
  bookmark: StopLocation;
}
 
interface BookmarkItemProps {
  index: string;
  bookmark: StopLocation;
  stopData: StopData;
}
 
function BookmarkItemRoutes({
  stopLocations
}: {
  stopLocations: StopLocation[];
}) {
  return (
    <div>
      {stopLocations.map((stopLocation: StopLocation) => {
        return (
          <div key={stopLocation.id}>
            {stopLocation.route.map((route: TrimetRoute) => {
              return <RouteIndicator key={route.route} routeId={route.route} />;
            })}
          </div>
        );
      })}
    </div>
  );
}
 
function BookmarkItem({ index, bookmark, stopData }: BookmarkItemProps) {
  return (
    <ListItem key={index}>
      <div
        className="transit-responsive-nowrap"
        style={{
          display: "flex",
          justifyContent: "space-between",
          flexWrap: "wrap"
        }}
      >
        <Link to={`/stop/${index}`}>{bookmark.desc}</Link>
        {stopData && (
          <div>
            <BookmarkItemRoutes stopLocations={stopData.location} />
          </div>
        )}
      </div>
    </ListItem>
  );
}
 
function Bookmark({ index, bookmark }: BookmarkProps) {
  const [stopInfo, setStopInfo] = useState(undefined);
 
  useEffect(() => {
    const location = {
      coords: {
        latitude: bookmark.lat,
        longitude: bookmark.lng
      }
    };
    getNearbyStops(location, 50).then((result: StopData) => {
      setStopInfo(result);
    });
  }, []);
 
  return <BookmarkItem index={index} bookmark={bookmark} stopData={stopInfo} />;
}
 
function getBookmarkedStops(bookmarks: StoredBookmarks) {
  return map(bookmarks, (bookmark: StopLocation, index) => {
    return <Bookmark index={index} bookmark={bookmark} />;
  });
}
 
interface Props {
  bookmarks: StoredBookmarks;
}
 
function BookmarksCard({ bookmarks }: Props) {
  return (
    <Card>
      <CardHeader as="h5">
        <FontAwesome name="bell" />
        Bookmarked Stops
      </CardHeader>
      <CardBody>Your favourite stops.</CardBody>
      <ListGroup>{getBookmarkedStops(bookmarks)}</ListGroup>
      <CardBody>
        <Link to="/bookmarks">Manage Bookmarks</Link>
      </CardBody>
    </Card>
  );
}
 
export default BookmarksCard;