All files / react-app/src/features/dashboard/Lists/spas consts.tsx

100% Statements 45/45
100% Branches 46/46
100% Functions 24/24
100% Lines 43/43

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                  72x 18x 1x   17x   30x                             28x           28x 914x         28x   914x           28x 28x 21x 7x   14x       28x           28x     914x 914x 697x 240x 457x                                     28x               28x             28x 84x           28x               28x               28x               28x 28x         28x 914x         72x 84x 84x   84x   84x 23x 5x   18x 18x       84x    
import { useEffect, useState } from "react";
import { useGetUser } from "@/api";
import { OsTableColumn } from "@/components";
import { BLANK_VALUE } from "@/consts";
import { removeUnderscoresAndCapitalize } from "@/utils";
import { CMS_READ_ONLY_ROLES, SEATOOL_STATUS, UserRoles } from "shared-types";
import { formatSeatoolDate } from "shared-utils";
import { CellDetailsLink, renderCellActions, renderCellDate } from "../renderCells";
 
const getColumns = (props) => {
  if (!props?.user || props.user === null) {
    return [];
  }
  return [
    // hide actions column for: readonly,help desk
    ...(!CMS_READ_ONLY_ROLES.some((UR) => props.user?.["custom:cms-roles"].includes(UR))
      ? [
          {
            locked: true,
            isSystem: true,
            label: "Actions",
            cell: renderCellActions(props.user),
          },
        ]
      : []),
    {
      props: { className: "w-[150px]" },
      field: "id.keyword",
      label: "SPA ID",
      locked: true,
      transform: (data) => data.id,
      cell: ({ id, authority }) => <CellDetailsLink id={id} authority={authority} />,
    },
    {
      field: "state.keyword",
      label: "State",
      transform: (data) => data.state ?? BLANK_VALUE,
      cell: (data) => data.state,
    },
    {
      field: "authority.keyword",
      label: "Authority",
      transform: (data) => data.authority ?? BLANK_VALUE,
      cell: (data) =>
        data?.authority ? removeUnderscoresAndCapitalize(data.authority) : BLANK_VALUE,
    },
    {
      field: props?.isCms ? "cmsStatus.keyword" : "stateStatus.keyword",
      label: "Status",
      transform: (data) => {
        const status = (() => {
          if (!props?.isCms) return data.stateStatus;
          if (props?.user?.["custom:cms-roles"].includes(UserRoles.HELPDESK)) {
            return data.stateStatus;
          }
          return data.cmsStatus;
        })();
 
        const subStatusRAI =
          data.raiWithdrawEnabled &&
          data.seatoolStatus !== SEATOOL_STATUS.PENDING_APPROVAL &&
          data.seatoolStatus !== SEATOOL_STATUS.PENDING_CONCURRENCE
            ? " (Withdraw Formal RAI Response - Enabled)"
            : "";
 
        return `${status}${subStatusRAI}`;
      },
      cell: (data) => {
        const status = (() => {
          if (!props?.isCms) return data.stateStatus;
          if (props.user?.["custom:cms-roles"].includes(UserRoles.HELPDESK))
            return data.stateStatus;
          return data.cmsStatus;
        })();
 
        return (
          <>
            <p>{status}</p>
            {data.raiWithdrawEnabled &&
              data.seatoolStatus !== SEATOOL_STATUS.PENDING_APPROVAL &&
              data.seatoolStatus !== SEATOOL_STATUS.PENDING_CONCURRENCE && (
                <p className="text-xs opacity-60">ยท Withdraw Formal RAI Response - Enabled</p>
              )}
          </>
        );
      },
    },
    {
      field: "submissionDate",
      label: "Initial Submission",
      transform: (data) =>
        data?.submissionDate ? formatSeatoolDate(data.submissionDate) : BLANK_VALUE,
      cell: renderCellDate("submissionDate"),
    },
    {
      field: "finalDispositionDate",
      label: "Final Disposition",
      hidden: true,
      transform: (data) =>
        data?.finalDispositionDate ? formatSeatoolDate(data.finalDispositionDate) : BLANK_VALUE,
      cell: renderCellDate("finalDispositionDate"),
    },
    {
      field: "origin.keyword",
      label: "Submission Source",
      hidden: true,
      transform: (data) => data.origin,
      cell: (data) => data.origin,
    },
    {
      field: "makoChangedDate",
      label: "Latest Package Activity",
      transform: (data) =>
        data.makoChangedDate ? formatSeatoolDate(data.makoChangedDate) : BLANK_VALUE,
      cell: renderCellDate("makoChangedDate"),
    },
    {
      field: "raiRequestedDate",
      label: "Formal RAI Requested",
      hidden: true,
      transform: (data) => {
        return data.raiRequestedDate ? formatSeatoolDate(data.raiRequestedDate) : BLANK_VALUE;
      },
      cell: renderCellDate("raiRequestedDate"),
    },
    {
      field: "raiReceivedDate",
      label: "Formal RAI Response",
      transform: (data) => {
        return data.raiReceivedDate ? formatSeatoolDate(data.raiReceivedDate) : BLANK_VALUE;
      },
      cell: renderCellDate("raiReceivedDate"),
    },
    {
      field: "leadAnalystName.keyword",
      label: "CPOC Name",
      hidden: true,
      transform: (data) => data.leadAnalystName ?? BLANK_VALUE,
      cell: (data) => data.leadAnalystName,
    },
    {
      field: "submitterName.keyword",
      label: "Submitted By",
      transform: (data) => data.submitterName ?? BLANK_VALUE,
      cell: (data) => data.submitterName,
    },
  ];
};
 
export const useSpaTableColumns = (): { columns?: OsTableColumn[]; isLoading: boolean } => {
  const [columns, setColumns] = useState(undefined);
  const [isLoading, setIsLoading] = useState(true);
 
  const { data: props, isLoading: isUserLoading } = useGetUser();
 
  useEffect(() => {
    if (isUserLoading === true) {
      setIsLoading(true);
    } else {
      setIsLoading(false);
      setColumns(getColumns(props));
    }
  }, [props, isUserLoading]);
 
  return { columns, isLoading };
};