Skip to content

Commit

Permalink
Working search
Browse files Browse the repository at this point in the history
Signed-off-by: Kipruto <[email protected]>
  • Loading branch information
kelvinkipruto committed Sep 10, 2024
1 parent 32c6c97 commit 40ea31c
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions apps/climatemappedafrica/src/components/DropdownSearch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const useStyles = makeStyles(({ palette, typography }) => ({
function DropdownSearch({
href: hrefProp = "/explore",
label = "Search for a location",
counties,
locations,
onClick,
icon: iconProp = SearchIcon,
placeholder,
Expand All @@ -74,38 +74,38 @@ function DropdownSearch({
const classes = useStyles(props);
const router = useRouter();
const [query, setQuery] = useState("");
const [countyCode, setCountyCode] = useState(null);
const [selectedLocation, setSelectedLocation] = useState(null);
const [suggestions, setSuggestions] = useState([]);

const handleChange = (e) => {
setQuery(e.target.value);
setCountyCode(null);
setSelectedLocation(null);
};

const handleSelect = (code, name) => {
setQuery(name.toLowerCase());
setCountyCode(code);
setSelectedLocation(code);
if (code && hrefProp?.length) {
router.push(`${hrefProp}/${code}`);
}
};

useEffect(() => {
if (query?.length > 0 && !countyCode) {
const matchedGeo = counties?.filter(({ name }) =>
if (query?.length > 0 && !selectedLocation) {
const matchedGeo = locations?.filter(({ name }) =>
name.toLowerCase()?.startsWith(query.toLowerCase()),
);
setSuggestions(matchedGeo);
} else {
setSuggestions([]);
}
}, [counties, countyCode, query]);
}, [locations, selectedLocation, query]);

const handleClickSearch = () => {
if (onClick) {
onClick(countyCode);
} else if (hrefProp?.length && countyCode) {
const href = `${hrefProp}/${countyCode}`;
onClick(selectedLocation);
} else if (selectedLocation) {
const href = `${hrefProp}/${selectedLocation}`;
router.push(href);
} else if (query) {
router.push("/404");
Expand Down Expand Up @@ -155,6 +155,7 @@ function DropdownSearch({
underline="none"
onClick={() => handleSelect(code, name)}
classes={{ root: classes.menuItem }}
key={code}
>
{name.toLowerCase()}
</ListItem>
Expand All @@ -171,7 +172,7 @@ DropdownSearch.propTypes = {
href: PropTypes.string,
onClick: PropTypes.func,
icon: PropTypes.string,
counties: PropTypes.arrayOf(PropTypes.shape({})),
locations: PropTypes.arrayOf(PropTypes.shape({})),
variant: PropTypes.string,
placeholder: PropTypes.string,
};
Expand Down

0 comments on commit 40ea31c

Please sign in to comment.