All files / src/components/common/Filter FilterWithSwitch.tsx

0% Statements 0/22
0% Branches 0/4
0% Functions 0/7
0% Lines 0/21

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                                                                                                                                                                                                                                                   
import * as React from "react";
// @mui
import MenuItem from "@mui/material/MenuItem";
// components
import FilterMenu from "./FilterMenu";
import FilterButton from "./FilterButton";
import { Switch } from "@common/Switch";
import { Text } from "@common/Text";
// utils
import { isAllActive } from "utils";
// redux
import { ActionCreatorWithPayload } from "@reduxjs/toolkit";
import { useAppDispatch } from "@redux/hooks";
 
// localization
import { useTranslation } from "react-i18next";
import { namespaces } from "localization/resources/i18n.constants";
 
export default function FilterWithSwitch({
  title,
  options,
  apply,
  disable,
  remove,
  filters,
}: {
  title: string;
  options: {
    [key: string]: boolean;
  };
  apply: ActionCreatorWithPayload<any, string>;
  remove: ActionCreatorWithPayload<any, string>;
  disable: ActionCreatorWithPayload<any, string>;
  filters: {
    [key: string]: any;
  };
}) {
  const dispatch = useAppDispatch();
  const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
  const [checked, setChecked] = React.useState(options);
  const { t } = useTranslation(namespaces.common);
 
  const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const { name } = event.target;
    setChecked({
      ...checked,
      [name]: event.target.checked,
    });
 
    if (event.target.checked) {
      dispatch(
        apply({
          type: title,
          value: name,
        }),
      );
    } else {
      dispatch(
        remove({
          type: title,
          value: name,
        }),
      );
    }
  };
  const open = Boolean(anchorEl);
 
  const handleClick = (event: React.MouseEvent<HTMLElement>) => {
    setAnchorEl(event.currentTarget.parentElement);
  };
 
  const handleClose = () => {
    setAnchorEl(null);
  };
 
  const handleDisableFilter = () => {
    dispatch(disable({ type: title }));
    setAnchorEl(null);
    Object.keys(checked).forEach((i) => (checked[i] = false));
  };
 
  return (
    <>
      <FilterButton
        title={title}
        onClick={handleClick}
        active={filters[title].length > 0}
        onDisableFilter={handleDisableFilter}
      >
        {isAllActive(checked) ? "All" : filters[title].join(" - ")}
      </FilterButton>
      <FilterMenu anchorEl={anchorEl} open={open} onClose={handleClose}>
        <Text
          variant="headline"
          fontWeight="semibold"
          sx={{
            marginBottom: 2,
            fontWeight: 350,
            color: "#575353",
            span: { textTransform: "capitalize" },
          }}
        >
          Filter by <span>{title}</span>
        </Text>
        {Object.keys(options).map((option, index) => (
          <MenuItem key={index} disableGutters>
            <Switch
              name={option}
              autoFocus={false}
              onChange={handleChange}
              checked={checked[option]}
            />
            <Text ml={1} variant="body">
              {t(option)}
            </Text>
          </MenuItem>
        ))}
      </FilterMenu>
    </>
  );
}