Skip to content
Snippets Groups Projects
Unverified Commit 54aa2147 authored by Ryan Laurie's avatar Ryan Laurie Committed by GitHub
Browse files

Remove Popular Items Entity (#41851)

* remove popular items entity

* support secondary icons
parent 131594bc
No related branches found
No related tags found
No related merge requests found
Showing
with 15 additions and 150 deletions
......@@ -12,7 +12,6 @@ export * from "./use-dashboard-query";
export * from "./use-database-list-query";
export * from "./use-database-query";
export * from "./use-group-list-query";
export * from "./use-popular-item-list-query";
export * from "./use-question-list-query";
export * from "./use-question-query";
export * from "./use-revision-list-query";
......
export * from "./use-popular-item-list-query";
import PopularItems from "metabase/entities/popular-items";
import type { PopularItem } from "metabase-types/api";
import type {
UseEntityListQueryProps,
UseEntityListQueryResult,
} from "../use-entity-list-query";
import { useEntityListQuery } from "../use-entity-list-query";
/**
* @deprecated use "metabase/api" instead
*/
export const usePopularItemListQuery = (
props: UseEntityListQueryProps = {},
): UseEntityListQueryResult<PopularItem> => {
return useEntityListQuery(props, {
fetchList: PopularItems.actions.fetchList,
getList: PopularItems.selectors.getList,
getLoading: PopularItems.selectors.getLoading,
getLoaded: PopularItems.selectors.getLoaded,
getError: PopularItems.selectors.getError,
getListMetadata: PopularItems.selectors.getListMetadata,
});
};
import { setupPopularItemsEndpoints } from "__support__/server-mocks";
import {
renderWithProviders,
screen,
waitForLoaderToBeRemoved,
within,
} from "__support__/ui";
import LoadingAndErrorWrapper from "metabase/components/LoadingAndErrorWrapper";
import { createMockPopularItem } from "metabase-types/api/mocks";
import { usePopularItemListQuery } from "./use-popular-item-list-query";
const TEST_ITEM = createMockPopularItem();
const TestComponent = () => {
const { data = [], metadata, isLoading, error } = usePopularItemListQuery();
if (isLoading || error) {
return <LoadingAndErrorWrapper loading={isLoading} error={error} />;
}
return (
<div>
{data.map((item, index) => (
<div key={index}>{item.model_object.name}</div>
))}
<div data-testid="metadata">
{(!metadata || Object.keys(metadata).length === 0) && "No metadata"}
</div>
</div>
);
};
const setup = () => {
setupPopularItemsEndpoints([TEST_ITEM]);
renderWithProviders(<TestComponent />);
};
describe("usePopularItemListQuery", () => {
it("should be initially loading", () => {
setup();
expect(screen.getByTestId("loading-spinner")).toBeInTheDocument();
});
it("should show data from the response", async () => {
setup();
await waitForLoaderToBeRemoved();
expect(screen.getByText(TEST_ITEM.model_object.name)).toBeInTheDocument();
});
it("should not have any metadata in the response", async () => {
setup();
await waitForLoaderToBeRemoved();
expect(
within(screen.getByTestId("metadata")).getByText("No metadata"),
).toBeInTheDocument();
});
});
......@@ -26,5 +26,4 @@ export { default as groups } from "./groups";
export { default as search } from "./search";
export { default as persistedModels } from "./persisted-models";
export { default as popularItems } from "./popular-items";
export { default as snippets } from "./snippets";
import { activityApi } from "metabase/api";
import { createEntity, entityCompatibleQuery } from "metabase/lib/entities";
import { entityTypeForObject } from "metabase/lib/schema";
import { PopularItemSchema } from "metabase/schema";
export const getEntity = item => {
const entities = require("metabase/entities");
return entities[entityTypeForObject(item)];
};
export const getName = item => {
return item.model_object.display_name || item.model_object.name;
};
export const getIcon = item => {
const entity = getEntity(item);
const options = { variant: "secondary" };
return entity.objectSelectors.getIcon(item.model_object, options);
};
/**
* @deprecated use "metabase/api" instead
*/
const PopularItems = createEntity({
name: "popularItems",
nameOne: "popularItem",
path: "/api/activity/popular_items",
schema: PopularItemSchema,
api: {
list: (entityQuery, dispatch) =>
entityCompatibleQuery(
entityQuery,
dispatch,
activityApi.endpoints.listPopularItems,
),
},
wrapEntity(item, dispatch = null) {
const entity = getEntity(item);
return entity.wrapEntity(item, dispatch);
},
objectSelectors: {
getName,
getIcon,
},
});
export default PopularItems;
import { useListRecentItemsQuery } from "metabase/api";
import {
useDatabaseListQuery,
usePopularItemListQuery,
useSetting,
} from "metabase/common/hooks";
useListRecentItemsQuery,
useListPopularItemsQuery,
} from "metabase/api";
import { useDatabaseListQuery, useSetting } from "metabase/common/hooks";
import LoadingAndErrorWrapper from "metabase/components/LoadingAndErrorWrapper";
import { useSelector } from "metabase/lib/redux";
import { isSyncCompleted } from "metabase/lib/syncing";
......@@ -26,7 +25,7 @@ export const HomeContent = (): JSX.Element | null => {
const { data: recentItems, error: recentItemsError } =
useListRecentItemsQuery(undefined, { refetchOnMountOrArgChange: true });
const { data: popularItems, error: popularItemsError } =
usePopularItemListQuery({ reload: true });
useListPopularItemsQuery(undefined, { refetchOnMountOrArgChange: true });
const error = databasesError || recentItemsError || popularItemsError;
if (error) {
......
......@@ -12,6 +12,7 @@ interface HomeModelCardProps {
export interface HomeModelIconProps {
name: IconName;
variant?: "secondary";
}
export const HomeModelCard = ({
......
import { t } from "ttag";
import _ from "underscore";
import { usePopularItemListQuery } from "metabase/common/hooks";
import { useListPopularItemsQuery } from "metabase/api";
import LoadingAndErrorWrapper from "metabase/components/LoadingAndErrorWrapper";
import { getIcon, getName } from "metabase/entities/popular-items";
import { getIcon } from "metabase/lib/icon";
import { getName } from "metabase/lib/name";
import * as Urls from "metabase/lib/urls";
import type { PopularItem } from "metabase-types/api";
......@@ -18,7 +19,7 @@ export const HomePopularSection = (): JSX.Element => {
data: popularItems = [],
isLoading,
error,
} = usePopularItemListQuery();
} = useListPopularItemsQuery(undefined, { refetchOnMountOrArgChange: true });
if (isLoading || error) {
return <LoadingAndErrorWrapper loading={isLoading} error={error} />;
......@@ -31,8 +32,11 @@ export const HomePopularSection = (): JSX.Element => {
{popularItems.map((item, index) => (
<HomeModelCard
key={index}
title={getName(item)}
icon={getIcon(item)}
title={getName(item.model_object)}
icon={getIcon(
{ ...item.model_object, model: item.model },
{ variant: "secondary" },
)}
url={Urls.modelToUrl(item) ?? ""}
/>
))}
......
......@@ -140,7 +140,3 @@ export const ObjectUnionSchema = new schema.Union(
CollectionSchema.define({
items: [ObjectUnionSchema],
});
export const PopularItemSchema = new schema.Entity("popularItems", undefined, {
idAttribute: ({ model, model_id }) => `${model}:${model_id}`,
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment