From 1717c82d7181cb272bb377657f35c3bfe1bb340a Mon Sep 17 00:00:00 2001 From: Shailesh Parmar Date: Tue, 16 Nov 2021 22:41:07 +0530 Subject: [PATCH] Fix: Change API calls in mydata page (#1208) * Fix: Change API calls in mydata page * Fix failing test in mydata component --- .../components/MyData/MyData.component.tsx | 21 ++--- .../src/components/MyData/MyData.interface.ts | 4 +- .../ui/src/components/MyData/MyData.test.tsx | 5 +- .../ui/src/constants/Mydata.constants.ts | 10 +++ .../pages/MyDataPage/MyDataPage.component.tsx | 82 ++++++------------- 5 files changed, 47 insertions(+), 75 deletions(-) diff --git a/catalog-rest-service/src/main/resources/ui/src/components/MyData/MyData.component.tsx b/catalog-rest-service/src/main/resources/ui/src/components/MyData/MyData.component.tsx index c86a1ba55b1..5a1061c5146 100644 --- a/catalog-rest-service/src/main/resources/ui/src/components/MyData/MyData.component.tsx +++ b/catalog-rest-service/src/main/resources/ui/src/components/MyData/MyData.component.tsx @@ -30,10 +30,8 @@ import { MyDataProps } from './MyData.interface'; const MyData: React.FC = ({ error, - errorHandler, countServices, userDetails, - rejectedResult, searchResult, fetchData, entityCounts, @@ -122,19 +120,16 @@ const MyData: React.FC = ({ useEffect(() => { if (searchResult) { - const formatedData: Array = []; - let totalValue = 0; - searchResult.forEach((res) => { - totalValue = totalValue + res.data.hits.total.value; - formatedData.push(...formatDataResponse(res.data.hits.hits)); - }); - - if (formatedData.length === 0 && rejectedResult.length > 0) { - errorHandler(rejectedResult[0].response?.data?.responseMessage); + const hits = searchResult.data.hits.hits; + if (hits.length > 0) { + setTotalNumberOfValues(searchResult.data.hits.total.value); + setData(formatDataResponse(hits)); + } else { + setData([]); + setTotalNumberOfValues(0); } - setTotalNumberOfValues(totalValue); - setData(formatedData); } + setIsEntityLoading(false); }, [searchResult]); diff --git a/catalog-rest-service/src/main/resources/ui/src/components/MyData/MyData.interface.ts b/catalog-rest-service/src/main/resources/ui/src/components/MyData/MyData.interface.ts index 9deef41ffd3..420d7b20fb6 100644 --- a/catalog-rest-service/src/main/resources/ui/src/components/MyData/MyData.interface.ts +++ b/catalog-rest-service/src/main/resources/ui/src/components/MyData/MyData.interface.ts @@ -5,9 +5,7 @@ export interface MyDataProps { error: string; countServices: number; userDetails: User; - rejectedResult: PromiseRejectedResult['reason'][]; - errorHandler: (error: string) => void; - searchResult: SearchResponse[] | undefined; + searchResult: SearchResponse | undefined; fetchData: (value: SearchDataFunctionType) => void; entityCounts: EntityCounts; } diff --git a/catalog-rest-service/src/main/resources/ui/src/components/MyData/MyData.test.tsx b/catalog-rest-service/src/main/resources/ui/src/components/MyData/MyData.test.tsx index 1b7449e3ea5..e723a800442 100644 --- a/catalog-rest-service/src/main/resources/ui/src/components/MyData/MyData.test.tsx +++ b/catalog-rest-service/src/main/resources/ui/src/components/MyData/MyData.test.tsx @@ -252,7 +252,6 @@ jest.mock('../../utils/ServiceUtils', () => ({ })); const fetchData = jest.fn(); -const errorHandler = jest.fn(); describe('Test MyData page', () => { it('Check if there is an element in the page', async () => { @@ -266,10 +265,8 @@ describe('Test MyData page', () => { pipelineCount: 1, }} error="" - errorHandler={errorHandler} fetchData={fetchData} - rejectedResult={[]} - searchResult={[mockData] as unknown as SearchResponse[]} + searchResult={mockData as unknown as SearchResponse} userDetails={mockUserDetails as unknown as User} />, { diff --git a/catalog-rest-service/src/main/resources/ui/src/constants/Mydata.constants.ts b/catalog-rest-service/src/main/resources/ui/src/constants/Mydata.constants.ts index 4fdb406b5d8..1091d9c4534 100644 --- a/catalog-rest-service/src/main/resources/ui/src/constants/Mydata.constants.ts +++ b/catalog-rest-service/src/main/resources/ui/src/constants/Mydata.constants.ts @@ -19,6 +19,16 @@ import { FilterObject } from 'Models'; import { getCurrentUserId } from '../utils/CommonUtils'; import { getFilterString } from '../utils/FilterUtils'; +export const myDataSearchIndex = + 'dashboard_search_index,topic_search_index,table_search_index,pipeline_search_index'; + +export const myDataEntityCounts = { + tableCount: 0, + topicCount: 0, + dashboardCount: 0, + pipelineCount: 0, +}; + export const myDataFilterType = [ { key: 'Owned', value: 'owner' }, { key: 'Following', value: 'followers' }, diff --git a/catalog-rest-service/src/main/resources/ui/src/pages/MyDataPage/MyDataPage.component.tsx b/catalog-rest-service/src/main/resources/ui/src/pages/MyDataPage/MyDataPage.component.tsx index 0d58295d96e..27e102d8fd4 100644 --- a/catalog-rest-service/src/main/resources/ui/src/pages/MyDataPage/MyDataPage.component.tsx +++ b/catalog-rest-service/src/main/resources/ui/src/pages/MyDataPage/MyDataPage.component.tsx @@ -15,20 +15,20 @@ * limitations under the License. */ +import { AxiosError } from 'axios'; import { isUndefined } from 'lodash'; import { observer } from 'mobx-react'; -import { - Bucket, - EntityCounts, - SearchDataFunctionType, - SearchResponse, -} from 'Models'; +import { EntityCounts, SearchDataFunctionType, SearchResponse } from 'Models'; import React, { useEffect, useState } from 'react'; import AppState from '../../AppState'; import { searchData } from '../../axiosAPIs/miscAPI'; import Loader from '../../components/Loader/Loader'; import MyData from '../../components/MyData/MyData.component'; import { PAGE_SIZE } from '../../constants/constants'; +import { + myDataEntityCounts, + myDataSearchIndex, +} from '../../constants/Mydata.constants'; import { getAllServices, getEntityCountByService, @@ -38,62 +38,36 @@ const MyDataPage = () => { const [error, setError] = useState(''); const [countServices, setCountServices] = useState(); const [isLoading, setIsLoading] = useState(true); - const [searchResult, setSearchResult] = useState(); - const [rejectedResult, setRejectedResult] = useState< - PromiseRejectedResult['reason'][] - >([]); + const [searchResult, setSearchResult] = useState(); const [entityCounts, setEntityCounts] = useState(); - const errorHandler = (error: string) => { - setError(error); - }; - const fetchData = (value: SearchDataFunctionType, fetchService = false) => { setError(''); - const entityIndexList = [ - 'table_search_index', - 'topic_search_index', - 'dashboard_search_index', - 'pipeline_search_index', - ]; - - const entityResponse = entityIndexList.map((entity) => - searchData( - value.queryString, - value.from, - PAGE_SIZE, - value.filters, - value.sortField, - value.sortOrder, - entity - ) - ); - - Promise.allSettled(entityResponse).then((response) => { - const fulfilledRes: SearchResponse[] = []; - const aggregations: Bucket[] = []; - const rejectedRes: PromiseRejectedResult['reason'][] = []; - - response.forEach((entity) => { - if (entity.status === 'fulfilled') { - fulfilledRes.push(entity.value); - aggregations.push( - ...entity.value.data.aggregations?.['sterms#Service']?.buckets + searchData( + value.queryString, + value.from, + PAGE_SIZE, + value.filters, + value.sortField, + value.sortOrder, + myDataSearchIndex + ) + .then((res: SearchResponse) => { + setSearchResult(res); + if (isUndefined(entityCounts)) { + setEntityCounts( + getEntityCountByService( + res.data.aggregations?.['sterms#Service']?.buckets + ) ); - } else { - rejectedRes.push(entity.reason); } + }) + .catch((err: AxiosError) => { + setError(err.response?.data?.responseMessage); + setEntityCounts(myDataEntityCounts); }); - if (fulfilledRes.length === 0 && response[0].status === 'rejected') { - setError(response[0].reason.response?.data?.responseMessage); - } - setRejectedResult(rejectedRes); - setEntityCounts(getEntityCountByService(aggregations)); - setSearchResult(fulfilledRes as unknown as SearchResponse[]); - }); - if (fetchService) { getAllServices() .then((res) => setCountServices(res.length)) @@ -124,9 +98,7 @@ const MyDataPage = () => { countServices={countServices} entityCounts={entityCounts} error={error} - errorHandler={errorHandler} fetchData={fetchData} - rejectedResult={rejectedResult} searchResult={searchResult} userDetails={AppState.userDetails} />