Skip to content

Commit

Permalink
refactor: ProductionData using page factory pattern
Browse files Browse the repository at this point in the history
chore: cleanup
  • Loading branch information
shon-button authored and pbastia committed Dec 31, 2024
1 parent fee479b commit ea696f2
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -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 (
<Suspense fallback={<Loading />}>
<ProductionData
report_version_id={params.version_id}
facility_id={params.facility_id}
/>
</Suspense>
);
}
export default defaultPageFactory(Page);
Original file line number Diff line number Diff line change
@@ -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 (
<Suspense fallback={<Loading />}>
<ProductionData
report_version_id={params.version_id}
facility_id={params.facility_id}
/>
</Suspense>
);
}
export default defaultPageFactory(Page);

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 (
<ProductionDataForm
report_version_id={version_id}
facility_id={facility_id}
allowedProducts={allowedProducts}
initialData={response.report_products}
schema={schema}
taskListElements={taskListElements}
/>
);
}
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -11,6 +12,12 @@ vi.mock("@reporting/src/app/utils/getOrderedActivities", () => ({

const getProductionDataMock = getProductionData as ReturnType<typeof vi.fn>;

// 🏷 Constants
const props: HasFacilityId = {
version_id: 1,
facility_id: "abc",
};

describe("The Production Data component", () => {
beforeEach(() => {
vi.clearAllMocks();
Expand All @@ -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");
});

Expand All @@ -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(
Expand All @@ -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();
Expand Down

0 comments on commit ea696f2

Please sign in to comment.