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

25% Statements 10/40
0% Branches 0/36
10% Functions 1/10
26.31% Lines 10/38

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                                        13x   13x                                     13x                                                                                                                                                                           13x   13x 13x                       13x                 13x   13x       13x      
import { CloseIcon } from "@assets/icons";
import { Button } from "@common/Button";
import { capitalizeFirstLetter } from "@common/Table/helpers";
import { Text } from "@common/Text";
import { Box, Stack, TextField } from "@mui/material";
import { ButtonBase, styled } from "@mui/material";
import { palette } from "@palette";
import { useAppSelector } from "@redux/hooks";
import { customTotalProcessing } from "@redux/slices/tableFilters";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import { useState } from "react";
import { INTEGER_REGEX } from "@validation/regex";
 
interface ICustomVolumeModal {
  handleClose: () => void;
  handleSave: (param?: { from?: number; to?: number }) => void;
  isMoney?: boolean;
}
 
type TContextValue = "from" | "to";
const contextValues: TContextValue[] = ["from", "to"];
 
const isValid = (
  fromValue: number | undefined,
  toValue: number | undefined,
) => {
  if (fromValue && !toValue) {
    return fromValue >= 0;
  }
 
  if (!fromValue && toValue) {
    return toValue >= 0;
  }
 
  if (fromValue && toValue) {
    return toValue >= fromValue && fromValue >= 0 && toValue >= 0;
  }
 
  return false;
};
 
const CustomVolumePopup = ({
  handleClose,
  handleSave,
  isMoney = true,
}: ICustomVolumeModal) => {
  const contextCustomTotalProcessing = useAppSelector(customTotalProcessing);
 
  const [fromValue, setFromValue] = useState<string>(
    `${contextCustomTotalProcessing?.from || ""}`,
  );
  const [toValue, setToValue] = useState<string>(
    `${contextCustomTotalProcessing?.to || ""}`,
  );
 
  const { isMobileView } = useCustomTheme();
 
  const isApplyBtnDisabled = !isValid(parseInt(fromValue), parseInt(toValue));
 
  const handleInputValueChange = (
    value: string,
    contextValue: TContextValue,
  ) => {
    if (INTEGER_REGEX.test(value) || value === "") {
      const setState = {
        from: setFromValue,
        to: setToValue,
      }[contextValue];
      setState(value);
    }
  };
 
  const handleApplyBtnClick = (event: React.FormEvent<HTMLFormElement>) => {
    event.preventDefault();
    event.stopPropagation();
    const parsedFromValue = parseInt(fromValue);
    const parsedToValue = parseInt(toValue);
    handleSave({
      from: !isNaN(parsedFromValue) ? parsedFromValue : undefined,
      to: !isNaN(parsedToValue) ? parsedToValue : undefined,
    });
    handleClose();
  };
 
  return (
    <StyledBox isMobile={isMobileView}>
      <form onSubmit={handleApplyBtnClick} style={{ height: "100%" }}>
        <Stack gap={1.5}>
          <StyledButtonBase disableRipple onClick={handleClose}>
            <CloseIcon stroke={palette.black[100]} />
          </StyledButtonBase>
 
          <Stack direction="row" gap={1}>
            {contextValues.map((contextValue) => (
              <TextField
                name={contextValue}
                key={contextValue}
                placeholder={capitalizeFirstLetter(contextValue)}
                value={contextValue === "from" ? fromValue : toValue}
                onChange={(e) => {
                  const newValue = e?.target?.value.replace(/[^0-9]/g, "");
                  handleInputValueChange(newValue, contextValue);
                }}
                type="text"
                {...(isMoney && {
                  InputProps: { endAdornment: USDText },
                })}
                inputProps={{ style: inputStyle, inputMode: "numeric" }}
              />
            ))}
          </Stack>
          <Box width="100%">
            <StyledButton
              type="submit"
              fullWidth={false}
              disabled={isApplyBtnDisabled}
              background="primary"
            >
              Apply
            </StyledButton>
          </Box>
        </Stack>
      </form>
    </StyledBox>
  );
};
 
const inputStyle = { marginTop: 0 };
 
const StyledBox = styled(Box, {
  shouldForwardProp: (prop) => prop !== "isMobile",
})<{ isMobile: boolean }>(({ isMobile, theme }) => ({
  ...(!isMobile && {
    padding: "12px",
    borderRadius: theme.spacing(1),
    width: isMobile ? "90vw" : 460,
    backgroundColor: palette.background.bgWhite,
    boxShadow: "0px 8px 25px 0px #00000026",
    margin: 8,
  }),
}));
 
const StyledButton = styled(Button)(({ theme }) => ({
  width: "fit-content",
  boxShadow: "none",
  alignSelf: "center",
  borderRadius: theme.spacing(4),
  fontSize: 18,
  margin: "0 auto",
}));
 
const StyledButtonBase = styled(ButtonBase)({ alignSelf: "flex-end" });
 
const StyledText = styled(Text)(({ theme }) => ({
  paddingLeft: theme.spacing(1),
}));
 
const USDText = <StyledText color={palette.neutral[40]}>USD</StyledText>;
 
export default CustomVolumePopup;