Fix: API call onClick of explore link from explore page (#1115)

* Fix: API call onClick of explore link from explore page

* fix failing test
This commit is contained in:
Shailesh Parmar 2021-11-09 19:32:49 +05:30 committed by GitHub
parent f6125a9683
commit 042561857e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 32 deletions

View File

@ -66,6 +66,7 @@ const Explore: React.FC<ExploreProps> = ({
searchResult, searchResult,
sortValue, sortValue,
error, error,
fetchCount,
handlePathChange, handlePathChange,
handleSearchText, handleSearchText,
fetchData, fetchData,
@ -99,10 +100,6 @@ const Explore: React.FC<ExploreProps> = ({
const isMounting = useRef(true); const isMounting = useRef(true);
const forceSetAgg = useRef(false); const forceSetAgg = useRef(false);
const previsouIndex = usePrevious(searchIndex); const previsouIndex = usePrevious(searchIndex);
const keyOfPreviousIndex = searchIndex.split(
'_'
)[0] as keyof ExploreProps['tabCounts'];
const previousIndexCount = usePrevious(tabCounts[keyOfPreviousIndex]);
const handleSelectedFilter = ( const handleSelectedFilter = (
checked: boolean, checked: boolean,
@ -136,6 +133,7 @@ const Explore: React.FC<ExploreProps> = ({
setFilters((prevFilters) => { setFilters((prevFilters) => {
return { return {
...prevFilters, ...prevFilters,
...getQueryParam(location.search),
[type]: [], [type]: [],
}; };
}); });
@ -250,7 +248,7 @@ const Explore: React.FC<ExploreProps> = ({
}; };
const getFacetedFilter = () => { const getFacetedFilter = () => {
const facetFilters: FilterObject = filterObject; const facetFilters: FilterObject = cloneDeep(filterObject);
for (const key in filters) { for (const key in filters) {
if (visibleFilters.includes(key)) { if (visibleFilters.includes(key)) {
facetFilters[key as keyof typeof filterObject] = facetFilters[key as keyof typeof filterObject] =
@ -354,14 +352,8 @@ const Explore: React.FC<ExploreProps> = ({
searchQuery, searchQuery,
tabsInfo[selectedTab - 1].path tabsInfo[selectedTab - 1].path
), ),
search: location.search,
}); });
if (previousIndexCount) {
// setTimeout because we need to reset previous index count after tab change
setTimeout(() => {
setCount(previousIndexCount as number, previsouIndex as string);
}, 100);
}
} }
}; };
const getTabs = () => { const getTabs = () => {
@ -412,6 +404,9 @@ const Explore: React.FC<ExploreProps> = ({
setCurrentTab(getCurrentTab(tab)); setCurrentTab(getCurrentTab(tab));
setSearchIndex(getCurrentIndex(tab)); setSearchIndex(getCurrentIndex(tab));
setCurrentPage(1); setCurrentPage(1);
if (!isMounting.current) {
fetchCount();
}
}, [tab]); }, [tab]);
useEffect(() => { useEffect(() => {
@ -461,7 +456,7 @@ const Explore: React.FC<ExploreProps> = ({
useEffect(() => { useEffect(() => {
if (!isMounting.current && previsouIndex === getCurrentIndex(tab)) { if (!isMounting.current && previsouIndex === getCurrentIndex(tab)) {
forceSetAgg.current = false; forceSetAgg.current = Boolean(searchIndex);
fetchTableData(); fetchTableData();
} }
}, [currentPage, filters, sortField, sortOrder]); }, [currentPage, filters, sortField, sortOrder]);

View File

@ -40,13 +40,7 @@ jest.mock('../../components/searched-data/SearchedData', () => {
)); ));
}); });
const handleSearchText = jest.fn(); const mockFunction = jest.fn();
const updateTableCount = jest.fn();
const updateTopicCount = jest.fn();
const updateDashboardCount = jest.fn();
const updatePipelineCount = jest.fn();
const handlePathChange = jest.fn();
const fetchData = jest.fn();
const mockSearchResult = { const mockSearchResult = {
resSearchResults: mockResponse as unknown as SearchResponse, resSearchResults: mockResponse as unknown as SearchResponse,
@ -60,9 +54,10 @@ describe('Test Explore component', () => {
const { container } = render( const { container } = render(
<Explore <Explore
error="" error=""
fetchData={fetchData} fetchCount={mockFunction}
handlePathChange={handlePathChange} fetchData={mockFunction}
handleSearchText={handleSearchText} handlePathChange={mockFunction}
handleSearchText={mockFunction}
searchQuery="" searchQuery=""
searchResult={mockSearchResult} searchResult={mockSearchResult}
searchText="" searchText=""
@ -74,10 +69,10 @@ describe('Test Explore component', () => {
dashboard: 8, dashboard: 8,
pipeline: 5, pipeline: 5,
}} }}
updateDashboardCount={updateDashboardCount} updateDashboardCount={mockFunction}
updatePipelineCount={updatePipelineCount} updatePipelineCount={mockFunction}
updateTableCount={updateTableCount} updateTableCount={mockFunction}
updateTopicCount={updateTopicCount} updateTopicCount={mockFunction}
/>, />,
{ {
wrapper: MemoryRouter, wrapper: MemoryRouter,

View File

@ -41,6 +41,7 @@ export interface ExploreProps {
tab: string; tab: string;
error: string; error: string;
searchQuery: string; searchQuery: string;
fetchCount: () => void;
handlePathChange: (path: string) => void; handlePathChange: (path: string) => void;
handleSearchText: (text: string) => void; handleSearchText: (text: string) => void;
updateTableCount: (count: number) => void; updateTableCount: (count: number) => void;

View File

@ -44,6 +44,7 @@ import { getFilterString } from '../../utils/FilterUtils';
import { getTotalEntityCountByService } from '../../utils/ServiceUtils'; import { getTotalEntityCountByService } from '../../utils/ServiceUtils';
const ExplorePage: FunctionComponent = () => { const ExplorePage: FunctionComponent = () => {
const initialFilter = getFilterString(getQueryParam(location.search));
const [isLoading, setIsLoading] = useState(true); const [isLoading, setIsLoading] = useState(true);
const [isLoadingForData, setIsLoadingForData] = useState(true); const [isLoadingForData, setIsLoadingForData] = useState(true);
const [error, setError] = useState<string>(''); const [error, setError] = useState<string>('');
@ -93,7 +94,15 @@ const ExplorePage: FunctionComponent = () => {
]; ];
const entityCounts = entities.map((entity) => const entityCounts = entities.map((entity) =>
searchData(searchText, 0, 0, emptyValue, emptyValue, emptyValue, entity) searchData(
searchText,
0,
0,
initialFilter,
emptyValue,
emptyValue,
entity
)
); );
Promise.allSettled(entityCounts) Promise.allSettled(entityCounts)
@ -189,7 +198,7 @@ const ExplorePage: FunctionComponent = () => {
queryString: searchText, queryString: searchText,
from: INITIAL_FROM, from: INITIAL_FROM,
size: PAGE_SIZE, size: PAGE_SIZE,
filters: getFilterString(getQueryParam(location.search)), filters: initialFilter,
sortField: initialSortField, sortField: initialSortField,
sortOrder: INITIAL_SORT_ORDER, sortOrder: INITIAL_SORT_ORDER,
searchIndex: getCurrentIndex(tab), searchIndex: getCurrentIndex(tab),
@ -198,7 +207,7 @@ const ExplorePage: FunctionComponent = () => {
queryString: searchText, queryString: searchText,
from: INITIAL_FROM, from: INITIAL_FROM,
size: ZERO_SIZE, size: ZERO_SIZE,
filters: getFilterString(getQueryParam(location.search)), filters: initialFilter,
sortField: initialSortField, sortField: initialSortField,
sortOrder: INITIAL_SORT_ORDER, sortOrder: INITIAL_SORT_ORDER,
searchIndex: getCurrentIndex(tab), searchIndex: getCurrentIndex(tab),
@ -207,7 +216,7 @@ const ExplorePage: FunctionComponent = () => {
queryString: searchText, queryString: searchText,
from: INITIAL_FROM, from: INITIAL_FROM,
size: ZERO_SIZE, size: ZERO_SIZE,
filters: getFilterString(getQueryParam(location.search)), filters: initialFilter,
sortField: initialSortField, sortField: initialSortField,
sortOrder: INITIAL_SORT_ORDER, sortOrder: INITIAL_SORT_ORDER,
searchIndex: getCurrentIndex(tab), searchIndex: getCurrentIndex(tab),
@ -216,7 +225,7 @@ const ExplorePage: FunctionComponent = () => {
queryString: searchText, queryString: searchText,
from: INITIAL_FROM, from: INITIAL_FROM,
size: ZERO_SIZE, size: ZERO_SIZE,
filters: getFilterString(getQueryParam(location.search)), filters: initialFilter,
sortField: initialSortField, sortField: initialSortField,
sortOrder: INITIAL_SORT_ORDER, sortOrder: INITIAL_SORT_ORDER,
searchIndex: getCurrentIndex(tab), searchIndex: getCurrentIndex(tab),
@ -231,6 +240,7 @@ const ExplorePage: FunctionComponent = () => {
) : ( ) : (
<Explore <Explore
error={error} error={error}
fetchCount={fetchCounts}
fetchData={fetchData} fetchData={fetchData}
handlePathChange={handlePathChange} handlePathChange={handlePathChange}
handleSearchText={handleSearchText} handleSearchText={handleSearchText}