All files / app/components MapSearch.tsx

88.09% Statements 37/42
81.25% Branches 13/16
85.71% Functions 12/14
89.18% Lines 33/37

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 1563x       3x 3x   3x   3x         3x 3x   343x                                               343x                                     338x           343x 343x 343x     343x                       343x 7x 7x     343x 2x       2x   2x   1x 1x 1x     1x         1x                       2x           5x   1x 1x         2x     1x     5x                                
import { Box, IconButton, styled } from "@mui/material";
import { AutocompleteChangeReason } from "@mui/material/Autocomplete";
import { SignupAccountInputs } from "features/auth/signup/AccountForm";
import { EditProfileFormValues } from "features/profile/edit/EditProfile";
import { LngLat } from "maplibre-gl";
import React, { useEffect, useState } from "react";
import { ControllerRenderProps, FieldError } from "react-hook-form";
import { useGeocodeQuery } from "utils/hooks";
 
import Autocomplete from "./Autocomplete";
import {
  NO_LOCATION_RESULTS_TEXT,
  PRESS_ENTER_TO_SEARCH,
  SEARCH_FOR_LOCATION,
} from "./constants";
import { SearchIcon } from "./Icons";
 
const StyledBox = styled(Box)(({ theme }) => ({
  "& *": {
    opacity: 1,
  },
  "& .MuiAutocomplete-input": {
    fontSize: "0.75rem",
  },
  "& .MuiFormHelperText-root": {
    fontSize: "0.65rem",
  },
  "& .MuiInputLabel-root": {
    fontSize: "0.75rem",
  },
  background: theme.palette.background.default,
  borderRadius: theme.shape.borderRadius * 3,
  left: 10,
  opacity: 0.9,
  padding: theme.spacing(1),
  position: "absolute",
  top: 10,
  width: "70%",
  zIndex: 1,
}));
 
const StyledForm = styled("div")(({ theme }) => ({
  display: "flex",
  alignItems: "center",
  width: "100%",
}));
 
interface MapSearchProps {
  setError: (error: string) => void;
  setResult: (
    lngLat: LngLat,
    address: string,
    simplifiedAddress: string,
  ) => void;
  inputFieldProps?:
    | ControllerRenderProps<SignupAccountInputs, "location">
    | ControllerRenderProps<EditProfileFormValues, "location">;
  inputFieldError?: FieldError;
}
 
export default function MapSearch({
  setError,
  setResult,
  inputFieldProps,
  inputFieldError,
}: MapSearchProps) {
  const [open, setOpen] = useState(false);
  const [value, setValue] = useState("");
  const { query, isLoading, results, error } = useGeocodeQuery();
 
  //create a dummy search options if there are no results
  const searchOptions = isLoading
    ? []
    : results && results.length === 0
      ? [
          {
            location: new LngLat(0, 0),
            name: NO_LOCATION_RESULTS_TEXT,
            simplifiedName: "",
          },
        ]
      : results;
 
  useEffect(() => {
    setError(error || "");
    Iif (error) setOpen(false);
  }, [error, setError]);
 
  const searchSubmit = (value: string, reason: AutocompleteChangeReason) => {
    Iif (reason === "blur") {
      setOpen(false);
      return;
    }
    const searchOption = results?.find((o) => value === o.name);
 
    if (!searchOption) {
      //createOption is when enter is pressed on user-entered string
      if (reason === "createOption") {
        query(value);
        setOpen(true);
      }
    } else {
      setResult(
        searchOption.location,
        searchOption.name,
        searchOption.simplifiedName,
      );
      setOpen(false);
    }
  };
 
  return (
    <StyledBox>
      <StyledForm>
        <Autocomplete
          id="map-search"
          label={SEARCH_FOR_LOCATION}
          value={value}
          size="small"
          options={searchOptions?.map((o) => o.name) || []}
          loading={isLoading}
          open={open}
          onBlur={() => setOpen(false)}
          inputProps={inputFieldProps}
          error={inputFieldError?.message}
          onInputChange={(e, v) => setValue(v)}
          onChange={(e, v, reason) => {
            setValue(v);
            searchSubmit(v, reason);
          }}
          freeSolo
          multiple={false}
          // show all returned results, don't do a filter client side
          filterOptions={(x) => x}
          disableClearable
          sx={{ flexGrow: 1 }}
          getOptionDisabled={(option) => option === NO_LOCATION_RESULTS_TEXT}
          helperText={PRESS_ENTER_TO_SEARCH}
          onKeyDown={(e) => {
            if (e.key === "Enter") searchSubmit(value, "createOption");
          }}
        />
        <IconButton
          aria-label="Search location"
          size="medium"
          onClick={() => {
            searchSubmit(value, "createOption");
          }}
        >
          <SearchIcon />
        </IconButton>
      </StyledForm>
    </StyledBox>
  );
}