All files / app/features/search SearchBox.tsx

86.66% Statements 26/30
100% Branches 2/2
71.42% Functions 5/7
86.2% Lines 25/29

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                3x 3x 3x 3x 3x 3x 3x   3x   3x   9x                                                     29x               31x 31x         31x       31x 1x 1x 1x       1x       31x     17x 17x           31x                                                                                     1x 1x                                                                                                                
import {
  FormControl,
  FormControlLabel,
  IconButton,
  InputAdornment,
  Radio,
  RadioGroup,
  Typography,
} from "@mui/material";
import { CrossIcon } from "components/Icons";
import LocationAutocomplete from "components/LocationAutocomplete";
import TextField from "components/TextField";
import { useTranslation } from "i18n";
import { GLOBAL, SEARCH } from "i18n/namespaces";
import { LngLat } from "maplibre-gl";
import { Dispatch, SetStateAction } from "react";
import { useForm } from "react-hook-form";
import { GeocodeResult } from "utils/hooks";
import makeStyles from "utils/makeStyles";
 
const useStyles = makeStyles((theme) => ({
  filterDialogButtonDesktop: {
    marginInlineStart: "auto",
  },
  mobileHide: {
    [theme.breakpoints.down("md")]: {
      display: "none",
    },
  },
  flexRow: {
    display: "flex",
    width: "100%",
  },
  justifyContent: {
    justifyContent: "space-around",
  },
}));
 
interface SearchBoxProps {
  searchType: "location" | "keyword";
  setSearchType: Dispatch<"location" | "keyword">;
  locationResult: GeocodeResult;
  setLocationResult: Dispatch<SetStateAction<GeocodeResult>>;
  setQueryName: Dispatch<SetStateAction<string>>;
  queryName: string;
}
 
export default function SearchBox({
  searchType,
  setSearchType,
  locationResult,
  setLocationResult,
  setQueryName,
  queryName,
}: SearchBoxProps) {
  const { t } = useTranslation([GLOBAL, SEARCH]);
  const classes = useStyles();
 
  const {
    control,
    formState: { errors },
  } = useForm<{ location?: GeocodeResult; keyword?: string }>({
    mode: "onChange",
  });
 
  const handleOnChangeAutocomplete = (event: GeocodeResult) => {
    if (event) {
      const { bbox } = event;
      const newLocationResult: GeocodeResult = {
        ...event,
        bbox: [bbox[2], bbox[3], bbox[0], bbox[1]], //sw long, sw lat, ne long, ne lat
      };
      setLocationResult(newLocationResult);
    }
  };
 
  const handleOnChangeKeyword = (
    event: React.ChangeEvent<HTMLInputElement>
  ) => {
    setQueryName(event.target.value);
    setLocationResult({
      ...locationResult,
      location: new LngLat(0, 0),
    });
  };
 
  const handleOnChangeRadioButton = (
    event: React.ChangeEvent<HTMLInputElement>,
    value: "location" | "keyword"
  ) => {
    setSearchType(value);
    setLocationResult({
      ...locationResult,
      name: "",
      simplifiedName: "",
      location: new LngLat(0, 0),
    });
    setQueryName("");
  };
 
  return (
    <>
      {searchType === "location" ? (
        <LocationAutocomplete
          control={control}
          name="location"
          defaultValue={locationResult}
          label={t("search:form.location_field_label")}
          onChange={handleOnChangeAutocomplete}
          fieldError={errors.location?.message}
          disableRegions
        />
      ) : (
        <TextField
          fullWidth
          id="query"
          value={queryName}
          label={t("search:form.keywords.field_label")}
          variant="standard"
          helperText=" "
          onChange={handleOnChangeKeyword}
          InputProps={{
            endAdornment: (
              <InputAdornment position="end">
                <IconButton
                  aria-label={t(
                    "search:form.keywords.clear_field_action_a11y_label"
                  )}
                  onClick={() => {
                    setQueryName("");
                    setLocationResult({
                      ...locationResult,
                      location: new LngLat(0, 0),
                    });
                  }}
                  size="small"
                >
                  <CrossIcon />
                </IconButton>
              </InputAdornment>
            ),
          }}
        />
      )}
      <div className={classes.flexRow}>
        <FormControl
          variant="standard"
          className={classes.flexRow}
          component="fieldset"
        >
          <RadioGroup
            className={classes.justifyContent}
            row
            onChange={
              (event, value) =>
                handleOnChangeRadioButton(
                  event,
                  value as "location" | "keyword"
                ) // coercion due material-ui has this type as unknown
            }
            value={searchType}
          >
            <FormControlLabel
              value="location"
              control={<Radio />}
              label={
                <Typography variant="body2">
                  {t("search:form.by_location_filter_label")}
                </Typography>
              }
            />
            <FormControlLabel
              value="keyword"
              control={<Radio />}
              label={
                <Typography variant="body2">
                  {t("search:form.by_keyword_filter_label")}
                </Typography>
              }
            />
          </RadioGroup>
        </FormControl>
      </div>
    </>
  );
}