All files / src/features/Merchants/MerchantSidePanel/Modals/MatchReport/components NewReportView.tsx

93.75% Statements 15/16
66.66% Branches 6/9
87.5% Functions 7/8
93.75% Lines 15/16

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                                                        4x         11x 11x 11x   11x 4x     11x 1x     11x                             1x                                                               3x                                 4x             3x                               4x 14x            
import { Stack } from "@mui/system";
import { useAppTheme } from "@theme/v2/Provider";
import GiveText from "@shared/Text/GiveText";
import { Icon as StatusIcon } from "@shared/Popup/GivePopupIcons";
import { ArrowRightIcon } from "@phosphor-icons/react";
import GiveChip from "@shared/Chip/GiveChip";
import moment from "moment";
import { PLATFORM_TIMEZONE } from "@utils/timezones";
import { statusOptions } from "../utils";
import {
  StyledAddNewReportButton,
  StyledPreviousCheckRowContainer,
  StyledPreviousCheckStack,
  StyledLatestCheckStack,
  StyledAddNewReportStack,
  StyledMatchStatusStackContainer,
  StyledMatchStatusBoxRoot,
} from "../styles/NewReportView.style";
import { SUMMARY_TEXT } from "@features/Merchants/MerchantSidePanel/constants";
import { ContentViewTypes } from "../match.types";
import { ReportType } from "../types";
 
interface IComponentProps {
  handleChangeView: (value: ContentViewTypes) => void;
  handleSetDetailsView: (report: ReportType) => void;
  reportsData: ReportType[];
}
 
const NewReportView = ({
  handleChangeView,
  handleSetDetailsView,
  reportsData,
}: IComponentProps) => {
  const theme = useAppTheme();
  const hasPreviousChecks = reportsData.length > 1;
  const recentReport = reportsData[0];
 
  const showAddNewReportView = () => {
    handleChangeView(ContentViewTypes.ADD_NEW_REPORT);
  };
 
  const showReportDetailsView = (report: ReportType = recentReport) => {
    handleSetDetailsView(report);
  };
 
  return (
    <Stack gap={3}>
      <StyledAddNewReportStack>
        <GiveText variant="bodyS" color="secondary">
          {SUMMARY_TEXT}
        </GiveText>
        <StyledAddNewReportButton
          label="Add New Report"
          size="small"
          onClick={showAddNewReportView}
        />
      </StyledAddNewReportStack>
      {reportsData.length ? (
        <StyledMatchStatusBoxRoot>
          <StyledMatchStatusStackContainer
            onClick={() => showReportDetailsView()}
          >
            <StatusIcon
              type={
                recentReport.statusName === "clear"
                  ? "success-regular"
                  : "warning"
              }
            />
            <Stack direction="row" justifyContent="space-between" pt={3}>
              <GiveText variant="h5">
                {statusOptions[recentReport.statusName].label}
              </GiveText>
              <ArrowRightIcon
                size={20}
                color={theme.palette.icon?.["icon-primary"]}
              />
            </Stack>
            <StyledLatestCheckStack>
              <GiveText variant="bodyXS" color="secondary">
                Latest check:
              </GiveText>
              <GiveText variant="bodyXS">
                {convertToET(recentReport.createdAt)}
              </GiveText>
            </StyledLatestCheckStack>
          </StyledMatchStatusStackContainer>
          {hasPreviousChecks && (
            <Stack gap={2.5}>
              <GiveText>Previous Checks</GiveText>
              <StyledPreviousCheckStack>
                {reportsData.slice(1).map((report, index) => (
                  <PreviousCheckRow
                    report={report}
                    showReportDetailsView={showReportDetailsView}
                    key={`prev-check-${index}`}
                  />
                ))}
              </StyledPreviousCheckStack>
            </Stack>
          )}
        </StyledMatchStatusBoxRoot>
      ) : (
        <></>
      )}
    </Stack>
  );
};
 
const PreviousCheckRow = ({
  report,
  showReportDetailsView,
}: {
  report: ReportType;
  showReportDetailsView: (report: ReportType) => void;
}) => {
  return (
    <StyledPreviousCheckRowContainer
      onClick={() => showReportDetailsView(report)}
    >
      <GiveText variant="bodyS" color="secondary">
        {convertToET(report.createdAt)}
      </GiveText>
      <GiveChip
        color={report.statusName === "clear" ? "success" : "error"}
        label={statusOptions[report.statusName].label}
        variant="light"
      />
    </StyledPreviousCheckRowContainer>
  );
};
 
export const convertToET = (value: number) => {
  return moment(value * 1000)
    .tz(PLATFORM_TIMEZONE)
    .format("MMMM DD, YYYY, hh:mm:ss A z");
};
 
export default NewReportView;