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

58.62% Statements 17/29
50% Branches 4/8
60% Functions 9/15
61.53% Lines 16/26

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                                    6x   3x 1x 3x         14x           14x   14x 5x 5x 5x   5x     5x 5x                               5x 5x       14x                                                                                            
import React, { useEffect, useState } from "react";
 
import { fetchStoredBookmarks } from "../../api/localstorage/bookmarks.localstorage";
import { getSystemAlerts } from "../../api/trimet/alerts";
import { Alert } from "../../api/trimet/interfaces/alertsData";
import AllAlertsCard from "./AllAlertsCard";
import BookmarksCard from "./BookmarksCard";
import { InfoCard } from "./InfoCard";
import { SystemAlertsCard } from "./SystemAlertsCard";
 
interface AlertsState {
  alerts: Alert[] | undefined;
  systemAlerts: Alert[] | undefined;
  loading: boolean;
  error: boolean;
}
 
export function partitionAlerts(alerts: Alert[]) {
  return {
    alerts: alerts
      .filter(alert => alert.system_wide_flag !== true)
      .sort((a, b) => b.begin - a.begin),
    systemAlerts: alerts.filter(alert => alert.system_wide_flag === true)
  };
}
 
export function useHomeAlerts() {
  const [alertsState, setAlertsState] = useState<AlertsState>({
    alerts: undefined,
    systemAlerts: undefined,
    loading: true,
    error: false
  });
  const [alertRefresh, setAlertRefresh] = useState(0);
 
  useEffect(() => {
    let active = true;
    setAlertsState(previous => ({ ...previous, loading: true, error: false }));
    getSystemAlerts()
      .then(result => {
        Iif (!result || !Array.isArray(result.alert)) {
          throw new Error("Invalid system alerts response");
        }
        Eif (active) {
          setAlertsState({
            ...partitionAlerts(result.alert),
            loading: false,
            error: false
          });
        }
      })
      .catch(() => {
        if (active) {
          setAlertsState(previous => ({
            ...previous,
            loading: false,
            error: true
          }));
        }
      });
    return () => {
      active = false;
    };
  }, [alertRefresh]);
 
  return {
    ...alertsState,
    onRefresh: () => setAlertRefresh(value => value + 1)
  };
}
 
function Home() {
  const alertsState = useHomeAlerts();
  const [bookmarksData, setBookmarksData] = useState(undefined);
 
  useEffect(() => {
    const storedBookmarks = fetchStoredBookmarks();
    setBookmarksData(storedBookmarks);
  }, []);
 
  return (
    <div className="transit-container">
      <br />
      <div className="transit-columns">
        <div className="transit-column transit-column-5">
          <InfoCard />
          <br />
          <SystemAlertsCard
            systemAlertsData={alertsState.systemAlerts}
            loading={alertsState.loading}
            error={alertsState.error}
            onRefresh={alertsState.onRefresh}
          />
        </div>
        <div className="transit-column transit-column-4">
          <AllAlertsCard
            alertsData={alertsState.alerts}
            loading={alertsState.loading}
            error={alertsState.error}
            onRefresh={alertsState.onRefresh}
          />
        </div>
        <div className="transit-column transit-column-3">
          <BookmarksCard bookmarks={bookmarksData} />
        </div>
      </div>
    </div>
  );
}
 
export default Home;