All files / react-app/src/features/package/package-activity index.tsx

94.28% Statements 33/35
89.47% Branches 34/38
100% Functions 11/11
94.28% Lines 33/35

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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231                                                                    1x                             72x 4x 4x                                                   1x                                   72x 28x 28x                     4x     4x   4x     4x     8x     4x                                                     72x 5x   5x 1x     4x 1x 7x       7x     1x       1x                   72x 15x 15x   15x       7x 2x     5x     28x     5x                                                                  
import { useMemo } from "react";
import { opensearch } from "shared-types";
import { format } from "date-fns";
import {
  Accordion,
  DetailsSection,
  AccordionContent,
  AccordionItem,
  AccordionTrigger,
  LoadingSpinner,
} from "@/components";
import * as Table from "@/components";
import { BLANK_VALUE } from "@/consts";
import { useAttachmentService, Attachments } from "./hook";
import { useParams } from "react-router";
import { useGetItem } from "@/api";
import { ItemResult } from "shared-types/opensearch/changelog";
 
type AttachmentDetailsProps = {
  id: string;
  attachments: opensearch.changelog.Document["attachments"];
  onClick: (attachment: Attachments[number]) => Promise<string>;
};
 
const AttachmentDetails = ({ id, attachments, onClick }: AttachmentDetailsProps) => (
  <Table.TableBody>
    {attachments.map((attachment) => {
      return (
        <Table.TableRow key={`${id}-${attachment.key}`}>
          <Table.TableCell>{attachment.title}</Table.TableCell>
          <Table.TableCell>
            <Table.Button
              className="ml-[-15px]"
              variant="link"
              onClick={() => onClick(attachment).then(window.open)}
            >
              {attachment.filename}
            </Table.Button>
          </Table.TableCell>
        </Table.TableRow>
      );
    })}
  </Table.TableBody>
);
 
type SubmissionProps = {
  packageActivity: opensearch.changelog.Document;
};
 
const Submission = ({ packageActivity }: SubmissionProps) => {
  const { attachments = [], id, packageId, additionalInformation } = packageActivity;
  const { onUrl, loading, onZip } = useAttachmentService({ packageId });
 
  return (
    <div className="flex flex-col gap-6">
      <div>
        <h2 className="font-bold text-lg mb-2">Attachments</h2>
        {attachments.length > 0 ? (
          <Table.Table>
            <Table.TableHeader>
              <Table.TableRow>
                <Table.TableHead className="w-[300px]">Document Type</Table.TableHead>
                <Table.TableHead>Attached File</Table.TableHead>
              </Table.TableRow>
            </Table.TableHeader>
            <AttachmentDetails attachments={attachments} id={id} onClick={onUrl} />
          </Table.Table>
        ) : (
          <p>No information submitted</p>
        )}
      </div>
 
      {attachments.length > 1 && (
        <Table.Button
          variant="outline"
          className="w-max"
          loading={loading}
          onClick={() => onZip(attachments)}
        >
          Download documents
        </Table.Button>
      )}
 
      <div>
        <h2 className="font-bold text-lg mb-2">Additional Information</h2>
        <p className="whitespace-pre-line">{additionalInformation || "No information submitted"}</p>
      </div>
    </div>
  );
};
 
type PackageActivityProps = {
  packageActivity: opensearch.changelog.Document;
};
 
const PackageActivity = ({ packageActivity }: PackageActivityProps) => {
  const label = useMemo(() => {
    switch (packageActivity.event) {
      case "capitated-amendment":
      case "capitated-initial":
      case "capitated-renewal":
      case "contracting-amendment":
      case "contracting-initial":
      case "contracting-renewal":
      case "new-chip-submission":
      case "new-medicaid-submission":
      case "temporary-extension":
      case "app-k":
        return "Initial Package Submitted";
 
      case "withdraw-package":
        return "Package - Withdrawal Requested";
      case "withdraw-rai":
        return "Formal RAI Response - Withdrawal Requested";
 
      case "respond-to-rai":
        return "RAI Response Submitted";
 
      case "upload-subsequent-documents":
        return "Subsequent Documentation Uploaded";
 
      default:
        return BLANK_VALUE;
    }
  }, [packageActivity.event]);
 
  return (
    <AccordionItem value={packageActivity.id}>
      <AccordionTrigger className="bg-gray-100 px-3">
        <p className="flex flex-row gap-2 text-gray-600">
          <strong>{label}</strong>
          {" - "}
          {packageActivity.timestamp
            ? format(new Date(packageActivity.timestamp), "eee, MMM d, yyyy hh:mm:ss a")
            : "Unknown"}
        </p>
      </AccordionTrigger>
      <AccordionContent className="p-4">
        <Submission packageActivity={packageActivity} />
      </AccordionContent>
    </AccordionItem>
  );
};
 
type DownloadAllButtonProps = {
  packageId: string;
  submissionChangelog: ItemResult[];
};
 
const DownloadAllButton = ({ packageId, submissionChangelog }: DownloadAllButtonProps) => {
  const { onZip, loading } = useAttachmentService({ packageId });
 
  if (submissionChangelog?.length === 0) {
    return null;
  }
 
  const onDownloadAll = () => {
    const attachmentsAggregate = submissionChangelog.reduce<Attachments>((acc, changelogItem) => {
      Iif (!changelogItem._source.attachments) {
        return acc;
      }
 
      return acc.concat(changelogItem._source.attachments);
    }, []);
 
    Iif (attachmentsAggregate.length === 0) {
      return;
    }
 
    onZip(attachmentsAggregate);
  };
 
  return (
    <Table.Button loading={loading} onClick={onDownloadAll} variant="outline">
      Download all documents
    </Table.Button>
  );
};
 
export const PackageActivities = () => {
  const { id: packageId } = useParams<{ id: string }>();
  const { data: submission, isLoading: isSubmissionLoading } = useGetItem(packageId);
 
  if (isSubmissionLoading === true) {
    return <LoadingSpinner />;
  }
 
  if (submission === undefined || submission === null || submission?.found === false) {
    return null;
  }
 
  const submissionChangelogWithoutAdminChanges = submission?._source?.changelog?.filter(
    (activity) =>
      // isAdminChange can be `undefined` or `boolean`
      !activity?._source?.isAdminChange,
  );
 
  const keyAndDefaultValue = submissionChangelogWithoutAdminChanges?.[0]?._source?.id;
 
  return (
    <DetailsSection
      id="package_activity"
      title={
        <div className="flex justify-between">
          Package Activity ({submissionChangelogWithoutAdminChanges?.length || 0})
          <DownloadAllButton
            submissionChangelog={submissionChangelogWithoutAdminChanges}
            packageId={packageId}
          />
        </div>
      }
    >
      {submissionChangelogWithoutAdminChanges?.length > 0 ? (
        <Accordion
          // `key` to re-render the `defaultValue` whenever `keyAndDefaultValue` changes
          key={keyAndDefaultValue}
          type="multiple"
          className="flex flex-col gap-2"
          defaultValue={[keyAndDefaultValue]}
        >
          {submissionChangelogWithoutAdminChanges.map(({ _source: packageActivity }) => (
            <PackageActivity key={packageActivity.id} packageActivity={packageActivity} />
          ))}
        </Accordion>
      ) : (
        <p className="text-gray-500">No package activity recorded</p>
      )}
    </DetailsSection>
  );
};