diff --git a/CHANGELOG.md b/CHANGELOG.md index 00a1b3e3..eec82d5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,10 +18,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Update collection links to be relative -### Updated - -- Refactor `getHomePageData` to pull item counts from collection endpoints (DR-3297) - ## [0.2.0] 2024-11-20 ### Removed diff --git a/app/src/components/pages/divisionsPage/divisionsPage.tsx b/app/src/components/pages/divisionsPage/divisionsPage.tsx index 1e4d31c6..494c6be0 100644 --- a/app/src/components/pages/divisionsPage/divisionsPage.tsx +++ b/app/src/components/pages/divisionsPage/divisionsPage.tsx @@ -33,7 +33,7 @@ export default function DivisionsPage({ summary, divisions }: DivisionsProps) { ]} adobeAnalyticsPageName={createAdobeAnalyticsPageName("divisions")} > - {divisions && divisions?.length > 0 ? ( + {divisions && divisions.length > 0 ? ( <> { const randomNumber = Math.floor(Math.random() * 2); @@ -16,8 +15,7 @@ export const getHomePageData = async () => { const allCollectionUUIDs: string[] = lanes.reduce((acc, lane) => { return acc.concat(lane.collections.map((collection) => collection.uuid)); }, [] as string[]); - const uuidtoItemCountMap = - await getItemsCountFromCollections(allCollectionUUIDs); + const uuidtoItemCountMap = await getItemsCountFromUUIDs(allCollectionUUIDs); // Update the collections for each lane with the number of items const updatedLanes = lanes.map((lane) => { diff --git a/app/src/utils/itemCount.test.tsx b/app/src/utils/itemCount.test.tsx deleted file mode 100644 index ba93ab95..00000000 --- a/app/src/utils/itemCount.test.tsx +++ /dev/null @@ -1,67 +0,0 @@ -import { apiResponse } from "./api"; -import { getItemsCountFromCollections } from "./itemCount"; - -jest.mock("./api"); - -describe("getItemsCountFromCollections()", () => { - beforeEach(() => { - jest.clearAllMocks(); - }); - - it("should return the correct numItems for each UUID", async () => { - (apiResponse as jest.Mock) - .mockResolvedValueOnce( - Promise.resolve({ - status: 200, - numItems: 10, - }) - ) - .mockResolvedValueOnce( - Promise.resolve({ - status: 200, - numItems: 20, - }) - ); - - const uuids = ["uuid1", "uuid2"]; - - const results = await getItemsCountFromCollections(uuids); - - expect(results).toEqual({ - uuid1: 10, - uuid2: 20, - }); - }); - - it("should handle server error gracefully", async () => { - const uuids = ["uuid1"]; - (apiResponse as jest.Mock).mockRejectedValueOnce( - Promise.resolve({ - status: 400, - }) - ); - const results = await getItemsCountFromCollections(uuids); - expect(results).toEqual({ - uuid1: 0, - }); - }); - - it("should handle missing numItems field gracefully", async () => { - const uuids = ["uuid1"]; - (apiResponse as jest.Mock).mockResolvedValueOnce( - Promise.resolve({ - status: 200, - }) - ); - const results = await getItemsCountFromCollections(uuids); - expect(results).toEqual({ - uuid1: 0, - }); - }); - - it("should handle an empty array of UUIDs", async () => { - const uuids = []; - const results = await getItemsCountFromCollections(uuids); - expect(results).toEqual({}); - }); -}); diff --git a/app/src/utils/itemCount.ts b/app/src/utils/itemCount.ts deleted file mode 100644 index 0e1567d1..00000000 --- a/app/src/utils/itemCount.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { apiResponse } from "./api"; - -/** - * Returns a map of collection uuids with corresponding item counts. - * @param {string[]} uuids - collection uuids - */ -export const getItemsCountFromCollections = async (uuids: string[]) => { - const results = {}; - const apiUrl = `${process.env.API_URL}/api/v2/collections/`; - const fetchPromises = uuids.map(async (uuid) => { - try { - const response = await apiResponse(`${apiUrl}${uuid}`); - const numItems = response.numItems || 0; - results[uuid] = numItems; - } catch (error) { - console.error(`Error fetching collection ${uuid}:`, error.message); - results[uuid] = 0; - } - }); - - await Promise.all(fetchPromises); - return results; -};