All files / src/components/common/TableFilters CustomPopover.tsx

0% Statements 0/34
0% Branches 0/27
0% Functions 0/8
0% Lines 0/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                                                                                                                                                                                                                                                                                                                               
import { Button } from "@common/Button";
import { Text } from "@common/Text";
import { Box, Popover, Stack, SxProps, TextField } from "@mui/material";
import { XIcon } from "@phosphor-icons/react";
import { Dispatch, SetStateAction, useEffect, useState } from "react";
import { CloseButton } from "./atoms";
 
type Props = {
  anchorEl: HTMLDivElement;
  open: boolean;
  setAnchorEl: Dispatch<SetStateAction<HTMLDivElement | null>>;
  customStyle?: SxProps;
  handleUpdateFilter?: () => void;
  setCustomValues: (amount: string) => void;
  limits?: { min?: number; max?: number };
};
 
const CustomPopover = ({
  anchorEl,
  open,
  customStyle,
  setAnchorEl,
  handleUpdateFilter,
  setCustomValues,
  limits,
}: Props) => {
  const WIDTH = 460;
  const HEIGHT = 174;
 
  const [minInputValue, setMinInputValue] = useState<string>(
    limits?.min?.toString() || "",
  );
  const [maxInputValue, setMaxInputValue] = useState<string>("");
  const [isButtonDisabled, setIsButtonDisabled] = useState<boolean>(true);
 
  const getDisabled = () => {
    const limited = limits?.min ? parseInt(minInputValue) < limits?.min : false;
 
    if (maxInputValue && !minInputValue) return false;
    if (!maxInputValue && minInputValue && !limited) return false;
 
    if (
      !maxInputValue ||
      !minInputValue ||
      parseInt(maxInputValue) < parseInt(minInputValue) ||
      limited
    ) {
      return true;
    }
    return false;
  };
 
  useEffect(() => {
    setIsButtonDisabled(getDisabled());
  }, [maxInputValue, minInputValue]);
 
  const handleClose = () => {
    setAnchorEl(null);
  };
 
  const onSubmit = () => {
    if (handleUpdateFilter) handleUpdateFilter();
    let query = `amount:>=${parseInt(minInputValue || "0") * 100}`;
    if (maxInputValue) query += `%3Bamount:<=${parseInt(maxInputValue) * 100}`;
    setCustomValues(query);
    handleClose();
  };
 
  const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
    event.preventDefault();
    event.stopPropagation();
    onSubmit();
  };
 
  return (
    <Popover
      open={open}
      anchorEl={anchorEl}
      disableRestoreFocus
      onClose={handleClose}
      anchorOrigin={{
        vertical: "bottom",
        horizontal: "left",
      }}
      sx={{
        "& .MuiPaper-root": {
          width: `${WIDTH}px`,
          height: `${HEIGHT}px`,
          msOverflowStyle: "none",
          scrollbarWidth: "none",
          padding: "12px",
          overflow: "hidden",
          backgroundColor: "#FAFAFA",
          boxShadow: "0px 8px 25px 0px #00000026",
          borderRadius: "8px",
        },
        ...customStyle,
      }}
    >
      <form onSubmit={handleSubmit} style={{ height: "100%" }}>
        <Stack
          sx={{ height: "100%" }}
          justifyContent="space-between"
          alignItems="flex-end"
        >
          <CloseButton closeButtonSize={24} onClick={handleClose}>
            <XIcon />
          </CloseButton>
          <Stack direction="row" justifyContent="space-between" width="100%">
            <TextField
              InputProps={{
                type: "number",
                placeholder: "Min. Number",
                endAdornment: <Text color="#C1C1C1">USD</Text>,
                sx: { width: "214px", border: "solid 1px #e1e1e1" },
              }}
              inputProps={{
                style: { marginTop: 0 },
                onChange: (e) => {
                  setMinInputValue(e.currentTarget.value);
                },
              }}
              helperText={limits?.min ? `Min Amount: ${limits?.min}.00` : ""}
            />
            <TextField
              InputProps={{
                type: "number",
                placeholder: "Max. Number",
                endAdornment: <Text color="#C1C1C1">USD</Text>,
                sx: { width: "214px", border: "solid 1px #e1e1e1" },
              }}
              inputProps={{
                style: { marginTop: 0 },
                onChange: (e) => {
                  setMaxInputValue(e.currentTarget.value);
                },
              }}
            />
          </Stack>
          <Box width="100%">
            <Button
              type="submit"
              fullWidth={false}
              disabled={isButtonDisabled}
              sx={{
                padding: "12px 24px",
                margin: "0 auto",
              }}
            >
              <Text>Apply</Text>
            </Button>
          </Box>
        </Stack>
      </form>
    </Popover>
  );
};
 
export default CustomPopover;