All files / src/shared/GiveDraggableModal GiveDraggableModal.tsx

68.75% Statements 22/32
55.55% Branches 10/18
40% Functions 4/10
70% Lines 21/30

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                                                                            120x 120x 120x                                   502x 502x 502x 502x         502x 86x         502x 496x   8x                   8x 8x 8x       502x             502x               502x         502x                                         502x           502x                                                                
import { DialogProps } from "@mui/material";
import Draggable, {
  ControlPosition,
  DraggableEventHandler,
} from "react-draggable";
import React, { useState, useRef, useEffect } from "react";
 
import GiveDraggableModalHeader from "./GiveModalHeader";
import SlideTransition from "@components/animation/SlideTransition";
import {
  GiveDraggableBodyWrapper,
  GiveDraggableMobileDrawer,
  GiveDraggableModalContainer,
  GiveDraggableModalFooterWrapper,
  MODAL_WIDTH,
  StyledModal,
} from "./atoms.components";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { CustomHeaderRightIcon } from "@features/GiveConversation/types";
 
type Props = {
  idBase?: string;
  baseModalProps?: Partial<DialogProps>;
  open: boolean;
  StickySection: () => JSX.Element;
  LeftHeaderSection: () => JSX.Element;
  handleClose: () => void;
  Body: () => JSX.Element;
  FooterComponent: () => JSX.Element | null;
  showHeaderBorder?: boolean;
  showFooterBorder?: boolean;
  customHeaderRightIcons?: CustomHeaderRightIcon[];
  isDraggable?: boolean;
  height?: string;
  y?: number;
  x?: number;
  closeOnClickOutside?: boolean;
};
const yPosition = window.innerHeight / 2 - 250;
const xPosition = window.innerWidth / 2 - 250;
const GiveDraggableModal = ({
  idBase,
  baseModalProps,
  StickySection,
  LeftHeaderSection,
  handleClose,
  Body,
  open,
  FooterComponent,
  customHeaderRightIcons,
  showHeaderBorder,
  showFooterBorder,
  isDraggable = true,
  height = MODAL_WIDTH,
  y = yPosition,
  x = xPosition,
  closeOnClickOutside = false,
}: Props) => {
  const { isMobileView } = useCustomThemeV2();
  const modalRef = useRef<HTMLDivElement>(null);
  const [isExpanded, setIsExpanded] = useState<boolean>(false);
  const [dragPosition, setDragPosition] = React.useState<ControlPosition>({
    x,
    y,
  });
 
  useEffect(() => {
    setDragPosition({
      x,
      y,
    });
  }, [x, y]);
  useEffect(() => {
    if (!closeOnClickOutside || !open) return;
 
    const handleClickOutside = (event: MouseEvent) => {
      if (
        open &&
        modalRef.current &&
        !modalRef.current.contains(event.target as Node)
      ) {
        handleClose();
      }
    };
 
    document.addEventListener("mousedown", handleClickOutside);
    return () => {
      document.removeEventListener("mousedown", handleClickOutside);
    };
  }, [open, handleClose, closeOnClickOutside]);
 
  const resetVerticalPosition = (y: any) => {
    setDragPosition((prev) => ({
      ...prev,
      y: y,
    }));
  };
 
  const handleExpandModal = () => {
    setIsExpanded((v) => {
      const y = v ? window.innerHeight / 2 - 250 : 0;
      resetVerticalPosition(y);
      return !v;
    });
  };
 
  const handleDragStop: DraggableEventHandler = (e, data) => {
    setDragPosition({ x: data.x, y: data.y });
  };
 
  const content = (
    <GiveDraggableModalContainer>
      <GiveDraggableModalHeader
        isDraggable={isDraggable}
        isExpanded={isExpanded}
        StickySection={StickySection}
        LeftHeaderSection={LeftHeaderSection}
        handleClose={handleClose}
        toggleResize={handleExpandModal}
        customHeaderRightIcons={customHeaderRightIcons}
        showHeaderBorder={showHeaderBorder}
      />
 
      <GiveDraggableBodyWrapper>
        <Body />
      </GiveDraggableBodyWrapper>
      <GiveDraggableModalFooterWrapper showFooterBorder={showFooterBorder}>
        <FooterComponent />
      </GiveDraggableModalFooterWrapper>
    </GiveDraggableModalContainer>
  );
 
  Iif (isMobileView)
    return (
      <GiveDraggableMobileDrawer open={open} handleClose={handleClose}>
        {content}
      </GiveDraggableMobileDrawer>
    );
  return (
    <Draggable
      handle=".draggable-wrapper-header"
      position={dragPosition}
      onStop={handleDragStop}
      bounds="body"
      disabled={!isDraggable}
    >
      <StyledModal
        ref={modalRef}
        key={y + x}
        hideBackdrop
        disableEnforceFocus
        disableAutoFocus
        disableRestoreFocus
        id={`${idBase}-modal`}
        open={open}
        onClose={handleClose}
        data-testid={`${idBase}-modal`}
        isMobileView={isMobileView}
        isExpanded={isExpanded}
        height={height}
        {...baseModalProps}
        TransitionComponent={SlideTransition}
      >
        {content}
      </StyledModal>
    </Draggable>
  );
};
 
export default GiveDraggableModal;