All files / src/components/Disputes/GiveDisputePanel GiveDisputePanel.tsx

66.66% Statements 40/60
58.33% Branches 28/48
70% Functions 7/10
66.1% Lines 39/59

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                                          1x           1x   40x   40x   40x 40x 40x 40x     40x     40x           40x       40x 2x 2x 2x     40x                                           40x 1x 1x     40x 1x       1x   1x 1x         40x 1x 1x           40x 1x         1x       1x 1x     40x                                                                                                                                   7x 7x 3x               4x 1x                   3x                
import NiceModal from "@ebay/nice-modal-react";
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import GiveSidePanel, {
  MIN_ALLOWED_SCREEN_SIZE_FOR_DOUBLE_PANELS,
} from "@shared/SidePanel/GiveSidePanel";
import GiveRiskProfile from "@features/GiveRiskProfile";
import { useState } from "react";
import GiveDisputePanelContent from "../GiveDisputePanel/components/GiveDisputePanelContent";
import { useDisputePreview } from "./hooks/useDisputePreview";
import {
  DisputeDataset,
  DisputeFirstPanel,
  DisputeSecondPanel,
  IDisputePreviewHeaderProps,
} from "./types";
import CustomerPanelContent from "@features/MerchantPortal/Customer/Modals/CustomerPanelContent";
import CollapseNavigation from "@features/Merchants/MerchantSidePanel/components/Collapse1stLevel/CollapseNavigation";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { useMediaQuery } from "@mui/material";
import TransactionPanelContent from "@features/TransactionPanel/components/Summary/TransactionPanelContent";
 
const GIVE_DISPUTE_PANEL_WIDTH = 640;
 
interface IComponentProps extends IDisputePreviewHeaderProps {
  id: string;
}
 
const GiveDisputePanel = NiceModal.create(
  ({ isFirst, isLast, setSelectedRow, id }: IComponentProps) => {
    const { modal, onRemove } = useNiceModal();
    const [secondPanelType, setSecondPanelType] =
      useState<DisputeSecondPanel>(null);
    const [firstPanelType, setFirstPanelType] =
      useState<DisputeFirstPanel>("dispute");
    const { data, isLoading } = useDisputePreview({ id });
    const { isMobileView } = useCustomThemeV2();
    const isSinglePanelSize = useMediaQuery(
      `(max-width:${MIN_ALLOWED_SCREEN_SIZE_FOR_DOUBLE_PANELS}px)`,
    );
    const [showFirstPanel, setShowFirstPanel] = useState(true);
 
    const showCollapseNavigation =
      !isMobileView &&
      ((isSinglePanelSize && !!secondPanelType) ||
        !showFirstPanel ||
        firstPanelType === "transaction");
 
    const navigationOffset =
      firstPanelType === "transaction"
        ? GIVE_DISPUTE_PANEL_WIDTH * 2
        : GIVE_DISPUTE_PANEL_WIDTH;
 
    const resetToDispute = (panel?: DisputeSecondPanel | null) => {
      setFirstPanelType("dispute");
      setSecondPanelType(panel || null);
      setShowFirstPanel(true);
    };
 
    const handleSidePanelOpen = (type: DisputeSecondPanel | null) => {
      switch (type) {
        case "customer":
        case "risk":
          // Transaction becomes first, customer/risk becomes second
          setFirstPanelType("transaction");
          setSecondPanelType(type);
          break;
        case "transaction":
          // Dispute as first, Transaction as second
          setFirstPanelType("dispute");
          setShowFirstPanel(true);
          setSecondPanelType("transaction");
          break;
        case null:
        default:
          // Reset to base view
          resetToDispute();
          break;
      }
    };
 
    const handleClose = () => {
      Eif (setSelectedRow) setSelectedRow(-1);
      onRemove();
    };
 
    const handleCloseSecondPanel = () => {
      Iif (firstPanelType === "transaction") {
        // When transaction is first, closing secondary panel restores transaction view
        resetToDispute("transaction");
      } else {
        if (showFirstPanel) {
          //only keep open if its already open
          setSecondPanelType(null);
          setShowFirstPanel(true);
        } else EonRemove();
      }
    };
 
    const handleBack = () => {
      if (firstPanelType === "transaction" || isSinglePanelSize) {
        resetToDispute();
      } else Eif (firstPanelType === "dispute" && secondPanelType) {
        setShowFirstPanel(true);
      }
    };
 
    const onCloseFirstPanel = () => {
      Iif (firstPanelType === "transaction") {
        resetToDispute();
        return;
      }
 
      Iif (secondPanelType) {
        setShowFirstPanel(false);
        return;
      }
      resetToDispute();
      handleClose();
    };
 
    return (
      <GiveSidePanel
        open={modal.visible}
        onClose={handleClose}
        secondPanel={
          <SecondPanelType
            type={secondPanelType}
            data={data}
            onClose={handleCloseSecondPanel}
            setSecondPanelType={handleSidePanelOpen}
          />
        }
        isSecondPanelOpen={!!secondPanelType}
        isFirstPannelHidden={!showFirstPanel}
        floatingContent={
          showCollapseNavigation && (
            <CollapseNavigation
              show={showCollapseNavigation}
              navigationOffSet={navigationOffset}
              onBack={handleBack}
            />
          )
        }
        width={GIVE_DISPUTE_PANEL_WIDTH}
      >
        {showFirstPanel &&
          (firstPanelType === "dispute" ? (
            <GiveDisputePanelContent
              data={data}
              isLoading={isLoading}
              isFirst={isFirst}
              isLast={isLast}
              setSelectedRow={setSelectedRow}
              setSecondPanelType={setSecondPanelType}
              onClose={onCloseFirstPanel}
            />
          ) : (
            <TransactionPanelContent
              data={data?.dispute?.preChargebackRefundTransaction}
              isFirst={isFirst}
              isLast={isLast}
              handleCustomer={(open) =>
                handleSidePanelOpen(open ? "customer" : null)
              }
              handleSidePanelOpen={handleSidePanelOpen as any}
              onClose={onCloseFirstPanel}
            />
          ))}
      </GiveSidePanel>
    );
  },
);
 
export default GiveDisputePanel;
 
function SecondPanelType({
  type,
  data,
  onClose,
  setSecondPanelType,
}: {
  type: DisputeSecondPanel;
  data: DisputeDataset;
  onClose: () => void;
  setSecondPanelType: (type: DisputeSecondPanel) => void;
}) {
  const { preChargebackRefundTransaction } = data?.dispute || {};
  if (type === "risk") {
    return (
      <GiveRiskProfile
        data={data?.dispute?.originalTransaction}
        onClose={onClose}
      />
    );
  }
  //we are only opening pre chargeback refunds for now
  if (type === "transaction" && preChargebackRefundTransaction) {
    return (
      <TransactionPanelContent
        data={preChargebackRefundTransaction}
        open={true}
        onClose={onClose}
        handleCustomer={() => setSecondPanelType("customer")}
        handleSidePanelOpen={setSecondPanelType as any}
      />
    );
  }
  return (
    <CustomerPanelContent
      onClose={onClose}
      data={{ ...data?.dispute?.customer, id: data?.dispute?.customer?.ID }}
      merchantID={data?.dispute?.merchant?.accID}
    />
  );
}