All files / applications/go-by-transit/src/view/lineDetail/component LineDetailView.tsx

85.36% Statements 35/41
62.96% Branches 17/27
69.23% Functions 9/13
88.88% Lines 32/36

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                                                              9x 9x 9x           9x 9x   9x 3x 3x 3x 3x 2x 2x 2x 1x   1x       1x 1x     3x     9x 3x 3x           3x   3x     3x 3x                                   3x     9x       9x 2x     7x 6x         1x                                                            
import React, { useEffect, useState } from "react";
 
import { getAlertsByRouteId } from "../../../api/trimet/alerts";
import { Alert, AlertsData } from "../../../api/trimet/interfaces/alertsData";
import {
  RouteDataResultSet,
  TrimetRoute
} from "../../../api/trimet/interfaces/routes";
import { getRouteById } from "../../../api/trimet/routeConfig";
import RouteListItem from "../../../component/route/RouteListItem";
import { maxLightRail } from "../../../data/trimet/schedules/maxLightRail";
import RouteBookmarkButtonContainer from "../../bookmarks/container/RouteBookmarkButtonContainer";
import Loading from "../../loading/Loading";
import RouteAlerts from "../../nearbyRouteDetail/components/RouteAlerts";
import "./LineDetailView.scss";
import { MapCard } from "./MapCard";
import { ScheduleCard } from "./ScheduleCard";
import { StopsCard } from "./StopsCard";
 
interface Props {
  id: number;
}
 
interface RouteAlertsState {
  routeId: number;
  alerts: Alert[] | undefined;
  loading: boolean;
  error: boolean;
}
 
export default function LinesViewComponent(props: Props) {
  const { id } = props;
  const [route, setRouteData] = useState<TrimetRoute>(undefined);
  const [routeAlertsState, setRouteAlertsState] = useState<RouteAlertsState>({
    routeId: id,
    alerts: undefined,
    loading: true,
    error: false
  });
  const [alertRefresh, setAlertRefresh] = useState(0);
  const [routeError, setRouteError] = useState<string>(undefined);
 
  useEffect(() => {
    let active = true;
    setRouteData(undefined);
    setRouteError(undefined);
    getRouteById(id).then((results: RouteDataResultSet) => {
      Eif (active) {
        const foundRoute = results && results.route && results.route[0];
        if (foundRoute) {
          setRouteData(foundRoute);
        } else {
          setRouteError("This route is no longer available.");
        }
      }
    }).catch(() => {
      Eif (active) {
        setRouteError("Unable to load this route. Please try again.");
      }
    });
    return () => { active = false; };
  }, [id]);
 
  useEffect(() => {
    let active = true;
    setRouteAlertsState(previous => ({
      routeId: id,
      alerts: previous.routeId === id ? previous.alerts : undefined,
      loading: true,
      error: false
    }));
    getAlertsByRouteId(id)
      .then((result: AlertsData) => {
        Iif (!result || !Array.isArray(result.alert)) {
          throw new Error("Invalid route alerts response");
        }
        Eif (active) {
          setRouteAlertsState({
            routeId: id,
            alerts: result.alert,
            loading: false,
            error: false
          });
        }
      })
      .catch(() => {
        if (active) {
          setRouteAlertsState(previous => ({
            routeId: id,
            alerts: previous.routeId === id ? previous.alerts : undefined,
            loading: false,
            error: true
          }));
        }
      });
    return () => { active = false; };
  }, [id, alertRefresh]);
 
  const currentRouteAlerts = routeAlertsState.routeId === id
    ? routeAlertsState
    : { alerts: undefined, loading: true, error: false };
 
  if (routeError) {
    return <p role="alert">{routeError}</p>;
  }
 
  if (!route) {
    return <Loading />;
  }
 
  const routeSchedule = maxLightRail[id];
 
  return (
    <div className="transit-container">
      <div className="transit-columns">
        <div className="transit-column">
          <RouteListItem route={route} />
          <RouteBookmarkButtonContainer route={route} />
        </div>
      </div>
      <br />
      <div className="transit-columns">
        <div className="transit-column transit-column-3">
          <ScheduleCard routeSchedule={routeSchedule} route={route} />
          <br />
          <MapCard />
          <br />
          <RouteAlerts
            id={id}
            alerts={currentRouteAlerts.alerts}
            loading={currentRouteAlerts.loading}
            error={currentRouteAlerts.error}
            onRetry={() => setAlertRefresh(value => value + 1)}
          />
        </div>
        <div className="transit-column transit-column-9">
          <StopsCard route={route} alertsData={currentRouteAlerts.alerts} />
        </div>
      </div>
    </div>
  );
}