From a72fcec57fd97257dd12a2323f621fd033a95370 Mon Sep 17 00:00:00 2001 From: Aniket Katkar Date: Wed, 25 Oct 2023 21:06:21 +0530 Subject: [PATCH] fix(ui): all assets not showing on my data widget (#13703) * changed the api call to fetch owned assets in my data widget * fixed unit tests --- .../MyDataWidget/MyDataWidget.component.tsx | 72 ++++++++------ .../MyData/MyDataWidget/MyDataWidget.test.tsx | 95 ++++++++++--------- 2 files changed, 95 insertions(+), 72 deletions(-) diff --git a/openmetadata-ui/src/main/resources/ui/src/components/MyData/MyDataWidget/MyDataWidget.component.tsx b/openmetadata-ui/src/main/resources/ui/src/components/MyData/MyDataWidget/MyDataWidget.component.tsx index 042f9aee0ac..b7ca4eba39d 100644 --- a/openmetadata-ui/src/main/resources/ui/src/components/MyData/MyDataWidget/MyDataWidget.component.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/components/MyData/MyDataWidget/MyDataWidget.component.tsx @@ -17,15 +17,20 @@ import { observer } from 'mobx-react'; import React, { useCallback, useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Link } from 'react-router-dom'; -import AppState from '../../../AppState'; -import { getUserPath, ROUTES } from '../../../constants/constants'; -import { AssetsType } from '../../../enums/entity.enum'; -import { EntityReference } from '../../../generated/entity/type'; +import { + getUserPath, + INITIAL_PAGING_VALUE, + PAGE_SIZE, + ROUTES, +} from '../../../constants/constants'; +import { SearchIndex } from '../../../enums/search.enum'; import { WidgetCommonProps } from '../../../pages/CustomizablePage/CustomizablePage.interface'; -import { getUserById } from '../../../rest/userAPI'; +import { searchData } from '../../../rest/miscAPI'; import { Transi18next } from '../../../utils/CommonUtils'; import { getEntityName } from '../../../utils/EntityUtils'; import { getEntityIcon, getEntityLink } from '../../../utils/TableUtils'; +import { useAuthContext } from '../../authentication/auth-provider/AuthProvider'; +import { SourceType } from '../../searched-data/SearchedData.interface'; import EntityListSkeleton from '../../Skeleton/MyData/EntityListSkeleton/EntityListSkeleton.component'; import './MyDataWidget.less'; @@ -35,34 +40,43 @@ const MyDataWidgetInternal = ({ widgetKey, }: WidgetCommonProps) => { const { t } = useTranslation(); - const currentUserDetails = AppState.getCurrentUserDetails(); + const { currentUser } = useAuthContext(); const [isLoading, setIsLoading] = useState(true); - const [data, setData] = useState([]); + const [data, setData] = useState([]); const [totalOwnedAssetsCount, setTotalOwnedAssetsCount] = useState(0); const fetchMyDataAssets = async () => { - if (!currentUserDetails || !currentUserDetails.id) { - return; - } - setIsLoading(true); - try { - const userData = await getUserById(currentUserDetails?.id, 'owns'); + if (!isUndefined(currentUser)) { + setIsLoading(true); + try { + const teamsIds = (currentUser.teams ?? []).map((team) => team.id); + const mergedIds = [ + ...teamsIds.map((id) => `owner.id:${id}`), + `owner.id:${currentUser.id}`, + ].join(' OR '); - if (userData) { - const includeData = Object.values(AssetsType); - const owns: EntityReference[] = userData.owns ?? []; - - const includedOwnsData = owns.filter((data) => - includeData.includes(data.type as AssetsType) + const queryFilter = `(${mergedIds})`; + const res = await searchData( + '', + INITIAL_PAGING_VALUE, + PAGE_SIZE, + queryFilter, + '', + '', + SearchIndex.ALL ); - setData(includedOwnsData.slice(0, 8)); - setTotalOwnedAssetsCount(includedOwnsData.length); + // Extract useful details from the Response + const totalOwnedAssets = res?.data?.hits?.total.value ?? 0; + const ownedAssets = res?.data?.hits?.hits; + + setData(ownedAssets.map((hit) => hit._source).slice(0, 8)); + setTotalOwnedAssetsCount(totalOwnedAssets); + } catch (err) { + setData([]); + } finally { + setIsLoading(false); } - } catch (err) { - setData([]); - } finally { - setIsLoading(false); } }; @@ -72,7 +86,7 @@ const MyDataWidgetInternal = ({ useEffect(() => { fetchMyDataAssets(); - }, [currentUserDetails]); + }, [currentUser]); return ( @@ -86,7 +100,7 @@ const MyDataWidgetInternal = ({ {data.length ? ( + to={getUserPath(currentUser?.name ?? '', 'mydata')}> {t('label.view-all')}{' '} @@ -132,14 +146,14 @@ const MyDataWidgetInternal = ({