All files / applications/go-by-transit/src/view/nearby/components NearbyStopsDetail.tsx

95.65% Statements 44/46
93.1% Branches 27/29
100% Functions 15/15
97.5% Lines 39/40

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                                                          95x 95x 95x 95x         95x 95x   95x 28x       6x 3x       95x 29x 29x   29x 29x 28x 29x   29x   1x   27x 26x     2x       29x 29x 29x       95x 84x 29x 28x 28x 28x             1x     95x 57x                           38x 38x 39x     38x           23x                                                                
import { Button } from "@wintermuted/ui-react";
import { Feature, LineString } from "geojson";
import React, { useEffect, useState } from "react";
import { Link, useLocation, useParams } from "react-router-dom";
import { getArrivals } from "../../../api/trimet/arrivals";
import {
  ArrivalData,
  ArrivalLocation,
} from "../../../api/trimet/interfaces/arrivals";
import StopBookmarkControlContainer from "../../nearbyStops/containers/StopBookmarkControlContainer";
import StopInfo from "./common/StopInfo";
import StopArrivals from "./NearbyStopArrivals";
import { TopNavBar } from "./common/TopNavBar";
import WalkingAccess from "./WalkingAccess";
import { stopBackUrl } from "../util/stopNavigation";
 
interface Props {
  currentLocation: number[];
  handleStopOpened: (stopLocation: ArrivalLocation) => void | (() => void);
  walkingEnabled?: boolean;
  onWalkingRouteChanged?: (feature: Feature<LineString>) => void | (() => void);
}
 
export function NearbyStopsDetail({
  currentLocation,
  handleStopOpened,
  walkingEnabled = false,
  onWalkingRouteChanged,
}: Props) {
  const { id } = useParams();
  const { search } = useLocation();
  const backTo = stopBackUrl(search);
  const [state, setState] = useState<{
    id: string;
    data?: ArrivalData;
    error?: boolean;
  }>({ id });
  const [retry, setRetry] = useState(0);
  const [selectedRoutes, setSelectedRoutes] = useState<number[]>([]);
 
  useEffect(() => {
    setSelectedRoutes([]);
  }, [id]);
 
  function toggleRoute(route: number) {
    setSelectedRoutes((selected) => selected.includes(route)
      ? selected.filter((item) => item !== route)
      : [...selected, route]);
  }
 
  useEffect(() => {
    let active = true;
    setState({ id });
    async function fetchData() {
      try {
        const arrivals = await getArrivals(id, 90);
        const stopLocation = arrivals.location?.find(
          (location) => location.id === Number(id)
        );
        if (!stopLocation || !Number.isFinite(stopLocation.lat) ||
            !Number.isFinite(stopLocation.lng) || !Array.isArray(arrivals.arrival)) {
          throw new Error("The requested stop was not reported.");
        }
        if (active) {
          setState({ data: arrivals, id });
        }
      } catch {
        Eif (active) setState({ error: true, id });
      }
    }
 
    void fetchData();
    return () => {
      active = false;
    };
  }, [id, retry]);
 
  useEffect(() => {
    if (state.id !== id || !state.data) return;
    const stop = state.data.location.find((location) => location.id === Number(id));
    Iif (!stop) return;
    try {
      return handleStopOpened(stop);
    } catch (error) {
      console.error("Could not display the stop on the map.", error);
    }
  }, [id, state, handleStopOpened]);
 
  function retryStop() {
    setRetry((value) => value + 1);
  }
 
  if (state.id !== id || !state.data) {
    return (
      <div className="scrollarea">
        <TopNavBar id={`Stop ${id}`} backTo={backTo} truncateTitle />
        {state.id === id && state.error ? (
          <div role="alert">
            <p>Could not load this stop&apos;s departures and location.</p>
            <Button onClick={retryStop}>Retry stop</Button>
          </div>
        ) : (
          <p role="status">Loading stop...</p>
        )}
      </div>
    );
  }
  const data = state.data;
  const stopLocation = data.location.find(
    (location) => location.id === Number(id)
  );
 
  return (
    <div className="scrollarea">
      <TopNavBar id={stopLocation.desc} backTo={backTo} truncateTitle />
      <br />
      <StopInfo
        stopLocation={stopLocation}
        arrivalRoutes={data.arrival.map((arrival) => arrival.route)}
        selectedRoutes={selectedRoutes}
        onToggleRoute={toggleRoute}
      />
      <div className="transit-nearby-stop-plan">
        <Link className="wm-btn wm-btn-primary" to={`/nearby/planner?to=${stopLocation.id}`}>
          Plan a trip to this stop
        </Link>
        <StopBookmarkControlContainer
          locationId={stopLocation.id}
          stopLocation={{ ...stopLocation, locid: stopLocation.id }}
          showLabel
        />
      </div>
      {onWalkingRouteChanged && (
        <WalkingAccess
          currentLocation={currentLocation}
          enabled={walkingEnabled}
          onRouteChanged={onWalkingRouteChanged}
          stop={{
            lat: stopLocation.lat,
            lng: stopLocation.lng,
            locid: stopLocation.id,
          }}
          title="Walk to stop"
        />
      )}
      <br />
      <StopArrivals data={data} selectedRoutes={selectedRoutes} />
    </div>
  );
}