All files / components/DateFilter DateFilter.tsx

77.77% Statements 14/18
57.14% Branches 4/7
75% Functions 3/4
77.77% Lines 14/18

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 363x 3x 3x           53x 53x       1x             1x 1x   1x 1x 1x 1x 1x             3x  
import { DatePicker } from "antd";
import { useSearchParams } from "react-router-dom";
import dayjs from "dayjs"
interface Props {
  paramName: string;
}
 
function DateFilter({ paramName }: Props) {
  const [searchParams, setSearchParams] = useSearchParams();
  return (
      <DatePicker data-testid="filter"
        defaultValue={searchParams.get(paramName)?dayjs(searchParams.get(paramName), "YYYY-MM-DD"):null}
        onChange={(e) => {
          Iif (e == null){
            setSearchParams(p=>{
              p.delete(paramName);
              p.delete("page");
              return p;
            })
          }
          setSearchParams((p) => {
            const date = e.date() >= 10 ? e.date().toString() : "0" + e.date();
            const month =
              e.month() + 1 >= 10 ? e.month() + 1 : "0" + String(e.month() + 1);
            const year = e.year();
            p.set(paramName, year + "-" + month + "-" + date  );
            p.delete("page");
            return p;
          });
        }}
      />
  );
}
 
export default DateFilter;