diff --git a/src/templates/DatasetSearch/index.jsx b/src/templates/DatasetSearch/index.jsx index d74a85c5..999b912a 100644 --- a/src/templates/DatasetSearch/index.jsx +++ b/src/templates/DatasetSearch/index.jsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useState, useRef } from 'react'; import { useSearchParams, useLocation } from 'react-router-dom'; import qs from 'qs'; import { useSearchAPI, separateFacets } from '@civicactions/data-catalog-services'; @@ -47,6 +47,8 @@ const DatasetSearch = ({ }) => { const [currentResultNumbers, setCurrentResultNumbers] = useState(null); const [noResults, setNoResults] = useState(false); + const [loadingData, setLoadingData] = useState(true); + const loadingRef = useRef(); const location = useLocation(); let [searchParams, setSearchParams] = useSearchParams(); const [filterText, setFilterText] = useState(''); @@ -111,6 +113,14 @@ const DatasetSearch = ({ } }, [page, sort, sortOrder, fulltext, selectedFacets]); + useEffect(() => { + // when loading state was previously true and is now false, display data + if (!loading && loadingRef.current === true) { + setLoadingData(false) + } + loadingRef.current = loading; + }, [loading]) + useEffect(() => { const baseNumber = Number(totalItems) > 0 ? 1 : 0; const startingNumber = baseNumber + (Number(pageSize) * Number(page) - Number(pageSize)); @@ -136,7 +146,10 @@ const DatasetSearch = ({
{ e.preventDefault(); - () => setFulltext(filterText); + () => { + if (filterText !== fulltext) setLoadingData(true); + setFulltext(filterText) + }; }} className={formClassName} > @@ -154,59 +167,72 @@ const DatasetSearch = ({ type="submit" htmlFor="dataset_fulltext_search" variation="primary" - onClick={() => setFulltext(filterText)} + onClick={() => { + if (filterText !== fulltext) setLoadingData(true); + setFulltext(filterText) + }} > Search
-
-
- {currentResultNumbers && ( + {loadingData ? ( + + ) : ( + <> +
+
+ {currentResultNumbers && ( +

+ Showing {currentResultNumbers.startingNumber} -{' '} + {currentResultNumbers.endingNumber} of {currentResultNumbers.total} datasets +

+ )}

- Showing {currentResultNumbers.startingNumber} -{' '} - {currentResultNumbers.endingNumber} of {currentResultNumbers.total} datasets + {selectedFacetsMessage(selectedFacets, { + theme: 'Categories', + keyword: 'Tags', + })}

- )} -

- {selectedFacetsMessage(selectedFacets, { - theme: 'Categories', - keyword: 'Tags', - })} -

-
+
- -
-
    - {noResults && } - {items.map((item) => ( -
  1. - -
  2. - ))} -
- {totalItems && ( - { - evt.preventDefault(); - window.scroll(0, 0); - setPage(page); - }} - renderHref={(page) => { - const searchParams = buildSearchParams(false); - const includeAnd = searchParams ? '&' : ''; - return `/datasets?page=${page}${includeAnd}${searchParams}`; - }} - /> + +
+
    + {noResults && } + {items.map((item) => ( +
  1. + +
  2. + ))} +
+ {totalItems && ( + { + evt.preventDefault(); + window.scroll(0, 0); + setPage(page); + }} + renderHref={(page) => { + const searchParams = buildSearchParams(false); + const includeAnd = searchParams ? '&' : ''; + return `/datasets?page=${page}${includeAnd}${searchParams}`; + }} + /> + )} + )}