All files / app/components MapSearch.tsx

88.88% Statements 40/45
81.25% Branches 13/16
85.71% Functions 12/14
90.24% Lines 37/41

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 1552x   2x 2x 2x 2x   2x         2x 2x   5x                                                                                     2x 43x   43x 43x 43x     43x                       43x 5x 5x     43x 3x       3x   3x   2x 2x 2x     1x         1x       43x       1x 1x                 4x       5x   1x 1x         4x     2x     5x                                
import { Box, IconButton } from "@material-ui/core";
import { AutocompleteChangeReason } from "@material-ui/lab/Autocomplete";
import { LngLat } from "maplibre-gl";
import React, { useEffect, useState } from "react";
import { useGeocodeQuery } from "utils/hooks";
import makeStyles from "utils/makeStyles";
 
import Autocomplete from "./Autocomplete";
import {
  NO_LOCATION_RESULTS_TEXT,
  PRESS_ENTER_TO_SEARCH,
  SEARCH_FOR_LOCATION,
} from "./constants";
import { SearchIcon } from "./Icons";
 
const useSearchStyles = makeStyles((theme) => ({
  autocomplete: {
    flexGrow: 1,
  },
  form: {
    display: "flex",
    alignItems: "center",
    width: "100%",
  },
  root: {
    "& *": {
      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,
  },
}));
 
interface MapSearchProps {
  setError: (error: string) => void;
  setResult: (
    lngLat: LngLat,
    address: string,
    simplifiedAddress: string
  ) => void;
}
 
export default function MapSearch({ setError, setResult }: MapSearchProps) {
  const classes = useSearchStyles();
 
  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) {
      //create-option is when enter is pressed on user-entered string
      if (reason === "create-option") {
        query(value);
        setOpen(true);
      }
    } else {
      setResult(
        searchOption.location,
        searchOption.name,
        searchOption.simplifiedName
      );
      setOpen(false);
    }
  };
 
  return (
    <Box className={classes.root}>
      <form
        onSubmit={(e) => {
          e.preventDefault();
          searchSubmit(value, "create-option");
        }}
        className={classes.form}
      >
        <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)}
          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
          className={classes.autocomplete}
          getOptionDisabled={(option) => option === NO_LOCATION_RESULTS_TEXT}
          helperText={PRESS_ENTER_TO_SEARCH}
          onKeyDown={(e) => {
            if (e.key === "Enter") searchSubmit(value, "create-option");
          }}
        />
        <IconButton
          aria-label="Search location"
          size="medium"
          onClick={() => {
            searchSubmit(value, "create-option");
          }}
        >
          <SearchIcon />
        </IconButton>
      </form>
    </Box>
  );
}