Skip to content

Commit

Permalink
SearchBox: add loader for overpass queries #191
Browse files Browse the repository at this point in the history
  • Loading branch information
zbycz committed Oct 5, 2023
1 parent d7651e9 commit 6bb3045
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/components/SearchBox/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const AutocompleteInput = ({
setInputValue,
options,
autocompleteRef,
setOverpassLoading,
}) => {
const { setFeature, setPreview } = useFeatureContext();
const { bbox, showToast } = useMapStateContext();
Expand Down Expand Up @@ -84,6 +85,7 @@ export const AutocompleteInput = ({
mobileMode,
bbox,
showToast,
setOverpassLoading,
)}
onHighlightChange={onHighlightFactory(setPreview)}
getOptionDisabled={(o) => o.loader}
Expand Down
39 changes: 28 additions & 11 deletions src/components/SearchBox/SearchBox.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import styled from 'styled-components';
import debounce from 'lodash/debounce';
import SearchIcon from '@material-ui/icons/Search';
Expand All @@ -7,6 +7,7 @@ import IconButton from '@material-ui/core/IconButton';
import Router from 'next/router';
import match from 'autosuggest-highlight/match';

import { CircularProgress } from '@material-ui/core';
import { fetchJson } from '../../services/fetch';
import { useMapStateContext } from '../utils/MapStateContext';
import { useFeatureContext } from '../utils/FeatureContext';
Expand Down Expand Up @@ -55,6 +56,10 @@ const SearchIconButton = styled(IconButton)`
}
`;

const OverpassCircularProgress = styled(CircularProgress)`
padding: 10px;
`;

const PHOTON_SUPPORTED_LANGS = ['en', 'de', 'fr'];

const getApiUrl = (inputValue, view) => {
Expand Down Expand Up @@ -162,15 +167,9 @@ const fetchOptions = debounce(
400,
);

const SearchBox = () => {
const { featureShown, feature, setFeature, setPreview } = useFeatureContext();
const useFetchOptions = (inputValue: string, setOptions) => {
const { view } = useMapStateContext();
const [inputValue, setInputValue] = useState('');
const [options, setOptions] = useState([]);
const autocompleteRef = useRef();
const mobileMode = useMobileMode();

React.useEffect(() => {
useEffect(() => {
if (inputValue === '') {
setOptions([]);
return;
Expand All @@ -190,15 +189,31 @@ const SearchBox = () => {
fetchOptions(inputValue, view, setOptions);
}
}, [inputValue]);
};

const closePanel = () => {
const useOnClosePanel = (setInputValue) => {
const { feature, setFeature, setPreview } = useFeatureContext();
const mobileMode = useMobileMode();

return () => {
setInputValue('');
if (mobileMode) {
setPreview(feature);
}
setFeature(null);
Router.push(`/${window.location.hash}`);
};
};

const SearchBox = () => {
const { featureShown } = useFeatureContext();
const [inputValue, setInputValue] = useState('');
const [options, setOptions] = useState([]);
const [overpassLoading, setOverpassLoading] = useState(false);
const autocompleteRef = useRef();
const onClosePanel = useOnClosePanel(setInputValue);

useFetchOptions(inputValue, setOptions);

return (
<TopPanel>
Expand All @@ -212,9 +227,11 @@ const SearchBox = () => {
setInputValue={setInputValue}
options={options}
autocompleteRef={autocompleteRef}
setOverpassLoading={setOverpassLoading}
/>

{featureShown && <ClosePanelButton onClick={closePanel} />}
{featureShown && <ClosePanelButton onClick={onClosePanel} />}
{overpassLoading && <OverpassCircularProgress />}
</StyledPaper>
</TopPanel>
);
Expand Down
12 changes: 11 additions & 1 deletion src/components/SearchBox/onSelectedFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ const fitBounds = (option, panelShown = false) => {
};

export const onSelectedFactory =
(setFeature, setPreview, mobileMode, bbox, showToast) => (_, option) => {
(setFeature, setPreview, mobileMode, bbox, showToast, setOverpassLoading) =>
(_, option) => {
if (option.overpass || option.preset) {
const tags = option.overpass || option.preset.presetForSearch.tags;

const timeout = setTimeout(() => {
setOverpassLoading(true);
}, 300);

performOverpassSearch(bbox, tags)
.then((geojson) => {
const count = geojson.features.length;
Expand All @@ -70,6 +76,10 @@ export const onSelectedFactory =
const message = `${e}`.substring(0, 100);
const content = t('searchbox.overpass_error', { message });
showToast({ content, type: 'error' });
})
.finally(() => {
clearTimeout(timeout);
setOverpassLoading(false);
});
return;
}
Expand Down

0 comments on commit 6bb3045

Please sign in to comment.