All files / publisher/pages/Metrics TerritoryMetrics.tsx

100% Statements 9/9
91.66% Branches 11/12
100% Functions 2/2
100% Lines 9/9

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                  1x             4x                           4x   4x 4x 1x 1x           1x       4x                                                                          
import { useParams } from "react-router-dom";
import { Row, Col, Spinner, CodeSnippet } from "@canonical/react-components";
 
import { useEffect } from "react";
import { renderTerritoriesMetrics } from "./metrics/metrics";
import useCountryMetrics from "../../hooks/useCountryMetrics";
 
import type { TerritoriesMetricsData } from "../../types/shared";
 
export const TerritoryMetrics = ({
  isEmpty,
  onDataLoad,
}: {
  isEmpty: boolean;
  onDataLoad: (dataLength: number | undefined) => void;
}): React.JSX.Element => {
  const { snapId } = useParams();
  const {
    status,
    data: countryInfo,
    isFetching,
  }: {
    status: string;
    data:
      | {
          active_devices: TerritoriesMetricsData;
          territories_total: number;
        }
      | undefined;
    isFetching: boolean;
  } = useCountryMetrics(snapId);
 
  useEffect(() => {
    if (countryInfo) {
      Eif (countryInfo.active_devices) {
        renderTerritoriesMetrics({
          selector: "#territories",
          metrics: countryInfo.active_devices,
        });
      }
      // @ts-expect-error Type clash
      onDataLoad(countryInfo.active_devices?.length);
    }
  }, [countryInfo]);
 
  return (
    <section className={`p-strip is-shallow ${isEmpty ? "is-empty" : ""}`}>
      <Row>
        <Col size={12} key="activeServices">
          <h1 className="u-float-left p-heading--4">Territories</h1>
          <div className="p-heading--4 u-float-right u-no-margin--top">
            <strong>{countryInfo?.territories_total}</strong>
          </div>
        </Col>
        <Col size={12} key="territoriesSeparator">
          <hr />
        </Col>
        {isFetching ? (
          <Spinner />
        ) : (
          <>
            {isEmpty && <div>No data found.</div>}
            {status === "error" && (
              <CodeSnippet
                blocks={[
                  {
                    code: <div>An error occurred. Please try again.</div>,
                    wrapLines: true,
                  },
                ]}
              />
            )}
          </>
        )}
 
        <Col size={12} key="territories">
          <div id="territories" className="snapcraft-territories"></div>
        </Col>
      </Row>
    </section>
  );
};