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

74.35% Statements 29/39
60% Branches 24/40
64.28% Functions 9/14
73.68% Lines 28/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 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                                                  21x 21x   21x                 21x 8x   21x 21x 21x                 21x     21x                                         21x       1x 1x             1x 1x   21x 7x     21x 9x   9x               21x 4x   21x       84x   84x             84x       1x                                                                                                             13x                                    
import React, { useEffect, useMemo, useState } from "react";
import Chip, { ChipOption } from "./Chip";
import CustomDateRangePicker from "@pages/Dashboard/components/CustomRangePicker";
import { useFormContext } from "react-hook-form";
import { toUnixDateFormat } from "@utils/date.helpers";
import { generateCustomLabel, getDateValue } from "./utils";
import moment from "moment";
 
interface Props {
  dateName: string;
  selectedDate?: any;
  rangeSaveCallback: (value: {
    startDate?: Date;
    endDate?: Date;
    label?: string;
    val?: string | number | undefined | ChipOption;
    dateName: string;
  }) => void;
}
 
function WrapperWithDateRange({
  dateName,
  selectedDate,
  rangeSaveCallback,
}: Props) {
  const [rangePickerAnchorEl, setRangePickerAnchorEl] = useState(null);
  const selectedDateValues = selectedDate?.[dateName] || selectedDate;
 
  const [selectedValue, setSelectedValue] = useState<
    | {
        startDate?: Date;
        endDate?: Date;
        label?: string | number;
        val?: string | number | undefined | ChipOption;
      }
    | undefined
  >(selectedDateValues);
  useEffect(() => {
    setSelectedValue(selectedDateValues);
  }, [selectedDateValues]);
  const { setValue, watch } = useFormContext();
  const values = watch();
  const customInitialRange = {
    startDate: selectedValue?.startDate
      ? toUnixDateFormat(selectedValue.startDate)
      : undefined,
    endDate: selectedValue?.endDate
      ? toUnixDateFormat(selectedValue.endDate)
      : undefined,
  };
 
  const handleSelect = (event: React.MouseEvent<any>) =>
    setRangePickerAnchorEl(event.currentTarget);
 
  const handleSave = () => {
    if (selectedValue) {
      setValue(
        dateName,
        getDateValue({
          modifier: "is between",
          range: selectedValue,
          prefix: dateName,
        }),
      );
      rangeSaveCallback({
        ...selectedValue,
        label: "Custom",
        dateName,
        val: `${selectedValue?.startDate} ${selectedValue?.endDate}`,
      });
    }
 
    setRangePickerAnchorEl(null);
  };
 
  const handleSelectChip = (
    val?: string | number | undefined | ChipOption,
    label?: string,
  ) => {
    const isValue = !!val;
    rangeSaveCallback({
      startDate: undefined,
      endDate: undefined,
      label: isValue ? label : undefined,
      val: val,
      dateName,
    });
    isValue && setSelectedValue(undefined);
    setValue(dateName, val || "");
  };
  const signupDateOptions = useMemo(
    () => getSignupDateOptions(dateName),
    [dateName],
  );
  const customLabel = useMemo(() => {
    Eif (!rangePickerAnchorEl) {
 
      return generateCustomLabel({
        endDate: selectedValue?.endDate,
        startDate: selectedValue?.startDate,
      });
    }
  }, [rangePickerAnchorEl, selectedValue]);
 
  const isThisSetSelected =
    !!values[dateName] &&
    signupDateOptions?.some((obj) => obj.value === values[dateName]);
 
  return (
    <>
      {signupDateOptions.map((option) => {
        const isCustom =
          option?.value === "custom" && customLabel !== undefined;
 
        const refinedOption = isCustom
          ? {
              ...option,
              label: `Custom${customLabel}`,
            }
          : option;
 
        return (
          <Chip
            key={refinedOption.value}
            item={refinedOption}
            onSelect={(e) => handleSelectChip(e, option?.label)}
            selected={
              isCustom
                ? !!customLabel
                : isThisSetSelected || selectedValue?.startDate
                ? option.value === values[dateName]
                : selectedValue?.label === option?.label && !!selectedValue?.val
            }
            {...(isCustom && {
              onClick: handleSelect,
            })}
            deleteCallback={() => {
              setSelectedValue(
                isCustom && selectedDateValues?.startDate
                  ? undefined
                  : selectedDateValues?.startDate
                  ? selectedDateValues
                  : selectedDateValues?.label === option?.label
                  ? undefined
                  : selectedDateValues,
              );
              setValue(dateName, "");
              rangeSaveCallback({
                startDate: undefined,
                endDate: undefined,
                dateName,
                label: undefined,
                val: undefined,
              });
            }}
          />
        );
      })}
 
      <CustomDateRangePicker
        customInitialRange={customInitialRange}
        setRangePickerAnchorEl={setRangePickerAnchorEl}
        rangePickerAnchorEl={rangePickerAnchorEl}
        isNewStyle
        handleSave={handleSave}
        onSelection={(e: { start: string; end: string }) => {
          setSelectedValue({
            startDate: moment(e.start, "MM-DD-YYYY").toDate(),
            endDate: moment(e.end, "MM-DD-YYYY").toDate(),
          });
        }}
        onCloseCallback={() => setSelectedValue(undefined)}
        useUTCMoment
      />
    </>
  );
}
 
export default WrapperWithDateRange;
 
const getSignupDateOptions = (prefix: string): ChipOption[] => [
  {
    label: "Today",
    value: getDateValue({ modifier: "on", date: new Date(), prefix }),
  },
  {
    label: "Last 7 days",
    value: getDateValue({ modifier: "in the last", days: 7, prefix }),
  },
  {
    label: "Last 30 days",
    value: getDateValue({ modifier: "in the last", days: 30, prefix }),
  },
  {
    label: "Custom",
    value: "custom",
  },
];