All files / applications/go-by-transit/src/view/stop/component StopLocationView.tsx

0% Statements 0/33
0% Branches 0/16
0% Functions 0/13
0% Lines 0/30

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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157                                                                                                                                                                                                                                                                                                                         
import {
  Card,
  CardBody,
  CardHeader,
  CardText
} from "@wintermuted/ui-react";
import { sortBy, uniq } from "lodash";
import React, { useEffect, useState } from "react";
import { getAlertsByLocationId } from "../../../api/trimet/alerts";
import { getArrivals } from "../../../api/trimet/arrivals";
import { Alert, AlertsData } from "../../../api/trimet/interfaces/alertsData";
import { ArrivalData } from "../../../api/trimet/interfaces/arrivals";
import { StopLocation } from "../../../api/trimet/interfaces/types";
import Map, { LatLngCoords } from "../../../component/map/Map";
import { AlertsCard } from "../../lineDetail/component/AlertsCard";
import Loading from "../../loading/Loading";
import StopLocationArrivals from "./StopLocationArrivals";
import StopLocationArrivalsTableNav from "./StopLocationArrivalsTableNav";
import StopLocationHeader from "./StopLocationHeader";
import LocationInfoPane from "./StopLocationInfoPane";
 
export type OnBookmarkClick = (
  stopLocation: StopLocation,
  stopIsBookmarked: boolean
) => void;
 
interface Props {
  stopIsBookmarked: boolean;
  onBookmarkClick: OnBookmarkClick;
  locationId: number;
}
 
interface MapCardParams {
  coordinates: number[];
}
 
interface StopAlertsState {
  locationId: number;
  alerts: Alert[] | undefined;
  loading: boolean;
  error: boolean;
}
 
function MapCard({ coordinates }: MapCardParams) {
  return (
    <Card>
      <CardHeader as="h5">Map</CardHeader>
      <CardBody>
        <CardText>
          <Map currentLocation={coordinates} />
        </CardText>
      </CardBody>
    </Card>
  );
}
 
export default function StopLocationView(props: Props) {
  const { onBookmarkClick, stopIsBookmarked, locationId } = props;
  const [arrivalData, setArrivalData] = useState<ArrivalData>(undefined);
  const [alertsState, setAlertsState] = useState<StopAlertsState>({
    locationId,
    alerts: undefined,
    loading: true,
    error: false
  });
  const [alertRefresh, setAlertRefresh] = useState(0);
 
  useEffect(() => {
    const locIDs = locationId.toString(10);
    getArrivals(locIDs, 45).then(results => {
      setArrivalData(results);
    });
  }, [locationId]);
 
  useEffect(() => {
    let active = true;
    setAlertsState(previous => ({
      locationId,
      alerts: previous.locationId === locationId ? previous.alerts : undefined,
      loading: true,
      error: false
    }));
    getAlertsByLocationId(locationId)
      .then((result: AlertsData) => {
        if (!result || !Array.isArray(result.alert)) {
          throw new Error("Invalid stop alerts response");
        }
        if (active) {
          setAlertsState({ locationId, alerts: result.alert, loading: false, error: false });
        }
      })
      .catch(() => {
        if (active) {
          setAlertsState(previous => ({
            locationId,
            alerts: previous.locationId === locationId ? previous.alerts : undefined,
            loading: false,
            error: true
          }));
        }
      });
    return () => {
      active = false;
    };
  }, [locationId, alertRefresh]);
  const currentAlertsState = alertsState.locationId === locationId
    ? alertsState
    : { alerts: undefined, loading: true, error: false };
 
  if (!arrivalData) {
    return <Loading />;
  }
 
  const location = arrivalData.location[0];
  const routes = sortBy(
    uniq(arrivalData.arrival.map(arrival => arrival.route))
  );
 
  const coordinates: LatLngCoords = [location.lng, location.lat];
  return (
    <div className="transit-container">
      {/*<br />*/}
      <div className="transit-columns">
        <StopLocationHeader
          location={location}
          onBookmarkClick={onBookmarkClick}
          stopIsBookmarked={stopIsBookmarked}
        />
      </div>
      <br />
      <div className="transit-columns">
        <div className="transit-column transit-column-3">
          <LocationInfoPane arrivalData={arrivalData} />
          <br />
          <AlertsCard
            alertsData={currentAlertsState.alerts}
            stopId={locationId}
            loading={currentAlertsState.loading}
            error={currentAlertsState.error}
            onRefresh={() => setAlertRefresh(value => value + 1)}
          />
        </div>
        <div className="transit-column transit-column-9">
          <StopLocationArrivalsTableNav
            routes={routes}
            locationId={location.id}
          />
          <br />
          <StopLocationArrivals arrivalData={arrivalData} />
          <br />
          <MapCard coordinates={coordinates} />
        </div>
      </div>
    </div>
  );
}