From ea696f270a79c5731aa2a56b522f94c70e42c03e Mon Sep 17 00:00:00 2001 From: shon-button Date: Fri, 20 Dec 2024 07:34:25 -0500 Subject: [PATCH] refactor: ProductionData using page factory pattern chore: cleanup --- .../[facility_id]/production-data/page.tsx | 20 +------ .../[facility_id]/production-data/page.tsx | 20 +------ .../components/products/ProductionData.tsx | 60 ------------------- .../products/ProductionDataPage.tsx | 48 +++++++++++++++ ...a.test.tsx => ProductionDataPage.test.tsx} | 15 +++-- 5 files changed, 65 insertions(+), 98 deletions(-) delete mode 100644 bciers/apps/reporting/src/app/components/products/ProductionData.tsx create mode 100644 bciers/apps/reporting/src/app/components/products/ProductionDataPage.tsx rename bciers/apps/reporting/src/tests/components/products/{ProductionData.test.tsx => ProductionDataPage.test.tsx} (87%) diff --git a/bciers/apps/reporting/src/app/bceidbusiness/industry_user/reports/[version_id]/facilities/[facility_id]/production-data/page.tsx b/bciers/apps/reporting/src/app/bceidbusiness/industry_user/reports/[version_id]/facilities/[facility_id]/production-data/page.tsx index c9f58abe07..929d5a55a1 100644 --- a/bciers/apps/reporting/src/app/bceidbusiness/industry_user/reports/[version_id]/facilities/[facility_id]/production-data/page.tsx +++ b/bciers/apps/reporting/src/app/bceidbusiness/industry_user/reports/[version_id]/facilities/[facility_id]/production-data/page.tsx @@ -1,18 +1,4 @@ -import { Suspense } from "react"; -import Loading from "@bciers/components/loading/SkeletonForm"; -import ProductionData from "@reporting/src/app/components/products/ProductionData"; +import Page from "@reporting/src/app/components/products/ProductionDataPage"; +import defaultPageFactory from "@bciers/components/nextPageFactory/defaultPageFactory"; -export default async function Page({ - params, -}: { - params: { version_id: number; facility_id: string }; -}) { - return ( - }> - - - ); -} +export default defaultPageFactory(Page); diff --git a/bciers/apps/reporting/src/app/bceidbusiness/industry_user_admin/reports/[version_id]/facilities/[facility_id]/production-data/page.tsx b/bciers/apps/reporting/src/app/bceidbusiness/industry_user_admin/reports/[version_id]/facilities/[facility_id]/production-data/page.tsx index c9f58abe07..929d5a55a1 100644 --- a/bciers/apps/reporting/src/app/bceidbusiness/industry_user_admin/reports/[version_id]/facilities/[facility_id]/production-data/page.tsx +++ b/bciers/apps/reporting/src/app/bceidbusiness/industry_user_admin/reports/[version_id]/facilities/[facility_id]/production-data/page.tsx @@ -1,18 +1,4 @@ -import { Suspense } from "react"; -import Loading from "@bciers/components/loading/SkeletonForm"; -import ProductionData from "@reporting/src/app/components/products/ProductionData"; +import Page from "@reporting/src/app/components/products/ProductionDataPage"; +import defaultPageFactory from "@bciers/components/nextPageFactory/defaultPageFactory"; -export default async function Page({ - params, -}: { - params: { version_id: number; facility_id: string }; -}) { - return ( - }> - - - ); -} +export default defaultPageFactory(Page); diff --git a/bciers/apps/reporting/src/app/components/products/ProductionData.tsx b/bciers/apps/reporting/src/app/components/products/ProductionData.tsx deleted file mode 100644 index 2ebe427136..0000000000 --- a/bciers/apps/reporting/src/app/components/products/ProductionData.tsx +++ /dev/null @@ -1,60 +0,0 @@ -import ProductionDataForm from "./ProductionDataForm"; -import { buildProductionDataSchema } from "@reporting/src/data/jsonSchema/productionData"; -import { getProductionData } from "@bciers/actions/api"; -import { getOrderedActivities } from "../../utils/getOrderedActivities"; -import { - ActivePage, - getFacilitiesInformationTaskList, -} from "../taskList/2_facilitiesInformation"; -import { Suspense } from "react"; - -interface Props { - report_version_id: number; - facility_id: string; // UUID -} - -const ProductionData: React.FC = async ({ - report_version_id, - facility_id, -}) => { - const response = await getProductionData(report_version_id, facility_id); - - const allowedProductNames = response.allowed_products.map((p) => p.name); - const allowedProducts = response.allowed_products.map((p) => ({ - product_id: p.id, - product_name: p.name, - unit: p.unit, - })); - - const schema: any = buildProductionDataSchema( - "Jan 1", - "Dec 31", - allowedProductNames, - ); - - const orderedActivities = await getOrderedActivities( - report_version_id, - facility_id, - ); - const taskListElements = await getFacilitiesInformationTaskList( - report_version_id, - facility_id, - orderedActivities, - ActivePage.ProductionData, - ); - - return ( - - - - ); -}; - -export default ProductionData; diff --git a/bciers/apps/reporting/src/app/components/products/ProductionDataPage.tsx b/bciers/apps/reporting/src/app/components/products/ProductionDataPage.tsx new file mode 100644 index 0000000000..0827f9cc6d --- /dev/null +++ b/bciers/apps/reporting/src/app/components/products/ProductionDataPage.tsx @@ -0,0 +1,48 @@ +import ProductionDataForm from "@reporting/src/app/components/products/ProductionDataForm"; +import { buildProductionDataSchema } from "@reporting/src/data/jsonSchema/productionData"; +import { getProductionData } from "@bciers/actions/api"; +import { getOrderedActivities } from "@reporting/src/app/utils/getOrderedActivities"; +import { + ActivePage, + getFacilitiesInformationTaskList, +} from "@reporting/src/app/components/taskList/2_facilitiesInformation"; +import { HasFacilityId } from "@reporting/src/app/utils/defaultPageFactoryTypes"; + +export default async function ProductionDataPage({ + version_id, + facility_id, +}: HasFacilityId) { + const response = await getProductionData(version_id, facility_id); + + const allowedProductNames = response.allowed_products.map((p) => p.name); + const allowedProducts = response.allowed_products.map((p) => ({ + product_id: p.id, + product_name: p.name, + unit: p.unit, + })); + + const schema: any = buildProductionDataSchema( + "Jan 1", + "Dec 31", + allowedProductNames, + ); + + const orderedActivities = await getOrderedActivities(version_id, facility_id); + const taskListElements = await getFacilitiesInformationTaskList( + version_id, + facility_id, + orderedActivities, + ActivePage.ProductionData, + ); + + return ( + + ); +} diff --git a/bciers/apps/reporting/src/tests/components/products/ProductionData.test.tsx b/bciers/apps/reporting/src/tests/components/products/ProductionDataPage.test.tsx similarity index 87% rename from bciers/apps/reporting/src/tests/components/products/ProductionData.test.tsx rename to bciers/apps/reporting/src/tests/components/products/ProductionDataPage.test.tsx index 1019276379..a8e661ade5 100644 --- a/bciers/apps/reporting/src/tests/components/products/ProductionData.test.tsx +++ b/bciers/apps/reporting/src/tests/components/products/ProductionDataPage.test.tsx @@ -1,6 +1,7 @@ import { getProductionData } from "@bciers/actions/api"; -import ProductionData from "@reporting/src/app/components/products/ProductionData"; +import ProductionDataPage from "@reporting/src/app/components/products/ProductionDataPage"; import { render, screen } from "@testing-library/react"; +import { HasFacilityId } from "@reporting/src/app/utils/defaultPageFactoryTypes"; vi.mock("@bciers/actions/api", () => ({ getProductionData: vi.fn(), @@ -11,6 +12,12 @@ vi.mock("@reporting/src/app/utils/getOrderedActivities", () => ({ const getProductionDataMock = getProductionData as ReturnType; +// 🏷 Constants +const props: HasFacilityId = { + version_id: 1, + facility_id: "abc", +}; + describe("The Production Data component", () => { beforeEach(() => { vi.clearAllMocks(); @@ -22,7 +29,7 @@ describe("The Production Data component", () => { report_products: [], }); - render(await ProductionData({ facility_id: "abc", report_version_id: 1 })); + render(await ProductionDataPage(props)); expect(getProductionData).toHaveBeenCalledWith(1, "abc"); }); @@ -34,7 +41,7 @@ describe("The Production Data component", () => { ], report_products: [], }); - render(await ProductionData({ facility_id: "abc", report_version_id: 1 })); + render(await ProductionDataPage(props)); expect(screen.getAllByText(/production data/i)).toHaveLength(2); // One for the page, one for the tasklist expect( @@ -55,7 +62,7 @@ describe("The Production Data component", () => { ], }); - render(await ProductionData({ facility_id: "abc", report_version_id: 1 })); + render(await ProductionDataPage(props)); expect(screen.getByText("testProduct")).toBeInTheDocument(); expect(screen.getByText("Unit")).toBeInTheDocument(); expect(screen.getByText("tonnes of tests")).toBeInTheDocument();