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

93.1% Statements 27/29
88.23% Branches 30/34
100% Functions 11/11
92.85% Lines 26/28

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                                                      1x                             4x 4x 4x                                                   1x                                   4x 28x 28x                       4x     4x   4x     4x     8x     4x                                                     4x 7x   7x 3x     4x 1x 7x       7x     1x       1x                             4x 34x                                                            
import { useMemo } from "react";
import { opensearch } from "shared-types";
import { ItemResult } from "shared-types/opensearch/changelog";
import { formatDateToET } from "shared-utils";
 
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components";
import * as Table from "@/components";
import { BLANK_VALUE } from "@/consts";
 
import { Attachments, useAttachmentService } from "./hook";
 
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 section attachments
        </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":
      case "new-legacy-submission":
        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 Document 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} {packageActivity.submitterName ? `By ${packageActivity.submitterName}` : ""}
          </strong>
          {" - "}
          {packageActivity.timestamp ? formatDateToET(packageActivity.timestamp) : "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 attachments
    </Table.Button>
  );
};
 
type PackageActivitiesProps = {
  id: string;
  changelog: ItemResult[];
};
 
export const PackageActivities = ({ id, changelog }: PackageActivitiesProps) => {
  const changelogWithoutAdminChanges = changelog.filter((item) => !item._source.isAdminChange);
 
  return (
    <Table.DetailsSection
      id="package_activity"
      title={
        <div className="flex justify-between">
          Package Activity ({changelogWithoutAdminChanges.length || 0})
          <DownloadAllButton submissionChangelog={changelogWithoutAdminChanges} packageId={id} />
        </div>
      }
    >
      {changelogWithoutAdminChanges.length > 0 ? (
        <Accordion
          // `changelogWithoutAdminChanges[0]._source.id` to re-render the `defaultValue` whenever `keyAndDefaultValue` changes
          key={changelogWithoutAdminChanges[0]._source.id}
          type="multiple"
          className="flex flex-col gap-2"
          defaultValue={[changelogWithoutAdminChanges[0]._source.id]}
        >
          {changelogWithoutAdminChanges.map(({ _source: packageActivity }) => (
            <PackageActivity key={packageActivity.id} packageActivity={packageActivity} />
          ))}
        </Accordion>
      ) : (
        <p className="text-gray-500">No package activity recorded</p>
      )}
    </Table.DetailsSection>
  );
};