From b12cdc7658500e8da0aedace1195c76b652fa548 Mon Sep 17 00:00:00 2001 From: Lucas Vogel Date: Wed, 9 Aug 2023 15:54:22 +0200 Subject: [PATCH] refactor(FiltersList): Add loading indication when loading search results --- pages/map.tsx | 3 +- src/components/FiltersList.tsx | 52 ++++++++++++++++++------------ src/components/FiltersTagsList.tsx | 3 ++ src/components/Label.tsx | 20 ++++++++---- src/components/Listbox.tsx | 16 ++++++--- src/components/TextSearch.tsx | 5 +++ src/components/icons/Spinner.tsx | 27 ++++++++++++++++ src/lib/UrlStateContext.tsx | 11 ++++++- 8 files changed, 102 insertions(+), 35 deletions(-) create mode 100644 src/components/icons/Spinner.tsx diff --git a/pages/map.tsx b/pages/map.tsx index b431c7ed..ac8dbc81 100644 --- a/pages/map.tsx +++ b/pages/map.tsx @@ -103,6 +103,7 @@ const MapPage: Page = ({ records: originalRecords }) => { [getDistanceToUser, useGeolocation, defaultSort, sortByTagsCount] ) + const tagsKey = urlState.tags?.join('-') || '' useEffect(() => { const filteredRecords = getFilteredFacilities({ facilities: originalRecords, @@ -111,7 +112,7 @@ const MapPage: Page = ({ records: originalRecords }) => { }) return setFilteredRecords(sortFacilities(filteredRecords)) // eslint-disable-next-line react-hooks/exhaustive-deps - }, [urlState.tags?.join('-'), activeIdsBySearchTerm.key]) + }, [tagsKey, activeIdsBySearchTerm.key, activeIdsBySearchTerm.isLoading]) return ( <> diff --git a/src/components/FiltersList.tsx b/src/components/FiltersList.tsx index 1fa23d7b..cec4f9e9 100644 --- a/src/components/FiltersList.tsx +++ b/src/components/FiltersList.tsx @@ -20,6 +20,7 @@ import { Button } from './Button' import { Arrow } from './icons/Arrow' import TextSearch from './TextSearch' import { useActiveIdsBySearchTerm } from '@lib/hooks/useActiveIdsBySearchTerm' +import { Spinner } from './icons/Spinner' export const FiltersList: FC<{ recordsWithOnlyLabels: RecordsWithOnlyLabelsType[] @@ -44,6 +45,7 @@ export const FiltersList: FC<{ recordsWithOnlyLabels ) const { isLoading: textSearchLoading, total } = useActiveIdsBySearchTerm() + const fieldsDisabled = textSearchLoading const group1 = labels.filter(({ fields }) => fields.group2 === 'gruppe-1') const group2 = labels.filter(({ fields }) => fields.group2 === 'gruppe-2') @@ -57,21 +59,17 @@ export const FiltersList: FC<{ .map((label) => label.id) const [activeTargetGroupId, setActiveTargetGroupId] = useState( - queryTagIds.find((tagId) => { - return targetGroupIds.includes(tagId) - }) + queryTagIds.find((tagId) => targetGroupIds.includes(tagId)) || null ) + const tagsKey = queryTagIds.join('-') useEffect(() => { - if (queryTagIds.length === 0) return - const currentTargetGroupId = queryTagIds.find((tagId) => targetGroupIds.includes(tagId) ) - if (currentTargetGroupId) { - setActiveTargetGroupId(currentTargetGroupId) - } - }, [queryTagIds, targetGroupIds]) + setActiveTargetGroupId(currentTargetGroupId || null) + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [tagsKey, targetGroupIds]) const someGroupFiltersActive = labels .filter(({ fields }) => fields.group2 !== 'zielpublikum') @@ -88,10 +86,12 @@ export const FiltersList: FC<{ {someGroupFiltersActive && (