From 8aff473445868b1958df0aba961dd42c45be6116 Mon Sep 17 00:00:00 2001 From: "K. Allagbe" Date: Mon, 10 Feb 2025 12:55:51 -0700 Subject: [PATCH] issue #234: LabelData model changes --- public/locales/en/labelDataValidator.json | 14 +++++--- public/locales/fr/labelDataValidator.json | 10 ++++-- src/app/label-data-confirmation/page.tsx | 2 +- src/components/BaseInformationForm.tsx | 5 ++- src/components/QuantityInput.tsx | 2 +- src/components/VerifiedRegistrationList.tsx | 16 ++++----- .../VerifiedQuantityMultiInput.test.tsx | 9 +++-- src/types/types.ts | 35 +++++++++++++++---- src/utils/client/constants.ts | 12 +++++-- src/utils/client/fieldValidation.ts | 1 - .../__tests__/modelTransformation.test.ts | 14 ++++---- src/utils/server/modelTransformation.ts | 8 ++--- 12 files changed, 81 insertions(+), 47 deletions(-) diff --git a/public/locales/en/labelDataValidator.json b/public/locales/en/labelDataValidator.json index 601a3059..ece148e6 100644 --- a/public/locales/en/labelDataValidator.json +++ b/public/locales/en/labelDataValidator.json @@ -6,9 +6,13 @@ "label": "Name", "placeholder": "Enter name" }, - "registrationNumber": { - "label": "Registration Number", - "placeholder": "Enter registration number" + "reg": { + "label": "Registration Numbers", + "placeholder": "Enter registration number", + "type": { + "fertilizer": "Fertilizer", + "ingredient": "Ingredient" + } }, "lotNumber": { "label": "Lot Number", @@ -146,5 +150,7 @@ "minValue": "Minimum value is 0", "duplicateUnit": "Duplicate unit" }, - "stepper": { "next": "Next", "back": "Back", "submit": "Submit" } + "stepper": { "next": "Next", "back": "Back", "submit": "Submit" }, + "fertilizer_product": "Fertilizer", + "ingredient_component": "Ingredient" } diff --git a/public/locales/fr/labelDataValidator.json b/public/locales/fr/labelDataValidator.json index bbe39c8b..b4572c9f 100644 --- a/public/locales/fr/labelDataValidator.json +++ b/public/locales/fr/labelDataValidator.json @@ -6,9 +6,13 @@ "label": "Nom", "placeholder": "Entrez le nom" }, - "registrationNumber": { - "label": "Numéro d'enregistrement", - "placeholder": "Entrez le numéro d'enregistrement" + "reg": { + "label": "Numéros d'enregistrement", + "placeholder": "Entrez le numéro d'enregistrement", + "type": { + "fertilizer": "Engrais", + "ingredient": "Ingrédient" + } }, "lotNumber": { "label": "Numéro de lot", diff --git a/src/app/label-data-confirmation/page.tsx b/src/app/label-data-confirmation/page.tsx index 4c0b1131..0d56c300 100644 --- a/src/app/label-data-confirmation/page.tsx +++ b/src/app/label-data-confirmation/page.tsx @@ -298,7 +298,7 @@ const LabelDataConfirmationPage = () => { { - labelData?.baseInformation.registrationNumber + labelData?.baseInformation.registrationNumbers .value } diff --git a/src/components/BaseInformationForm.tsx b/src/components/BaseInformationForm.tsx index f5092388..1d54c4e9 100644 --- a/src/components/BaseInformationForm.tsx +++ b/src/components/BaseInformationForm.tsx @@ -48,10 +48,9 @@ const BaseInformationForm: React.FC = ({ loading={loading} /> ( = ({ label, path, - registrationTypes, className = "", loading = false, }) => { + const valuesPath = `${path}.values`; const { control, trigger } = useFormContext(); const { fields, append, remove } = useFieldArray({ control, - name: `${path}.registrations`, + name: valuesPath, }); - const registrationsPath = `${path}.registrations`; const validateFields = async (callback: (valid: boolean) => void) => { const validationResults = await Promise.all( fields.map((_, index) => Promise.all([ - trigger(`${registrationsPath}.${index}.number`), - trigger(`${registrationsPath}.${index}.type`), + trigger(`${valuesPath}.${index}.identifier`), + trigger(`${valuesPath}.${index}.type`), ]), ), ); @@ -60,7 +59,7 @@ const VerifiedRegistrationList: React.FC = ({ append({ number: "", type: "" })} + onAppend={() => append(DEFAULT_REGISTRATION_NUMBER)} > {fields.map((fieldItem, index) => ( = ({ isLastItem={index === fields.length - 1} > setIsFocused(true)} onBlur={() => setIsFocused(false)} diff --git a/src/components/__tests__/VerifiedQuantityMultiInput.test.tsx b/src/components/__tests__/VerifiedQuantityMultiInput.test.tsx index e0547660..215ff26a 100644 --- a/src/components/__tests__/VerifiedQuantityMultiInput.test.tsx +++ b/src/components/__tests__/VerifiedQuantityMultiInput.test.tsx @@ -1,8 +1,7 @@ -import { VerifiedQuantityField } from "@/types/types"; +import { VerifiedQuantityList } from "@/types/types"; import { fireEvent, render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { FormProvider, useForm } from "react-hook-form"; -import VerifiedQuantityList from "../VerifiedQuantityList"; const Wrapper = ({ label = "Test Label", @@ -19,11 +18,11 @@ const Wrapper = ({ placeholder?: string; path?: string; unitOptions?: string[]; - defaultValues?: VerifiedQuantityField; + defaultValues?: VerifiedQuantityList; loading?: boolean; - onSubmit?: (data: VerifiedQuantityField) => void; + onSubmit?: (data: VerifiedQuantityList) => void; }) => { - const methods = useForm({ + const methods = useForm({ defaultValues, mode: "onSubmit", }); diff --git a/src/types/types.ts b/src/types/types.ts index 982c7128..dceb1610 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -104,7 +104,7 @@ export type Quantity = { export const DEFAULT_QUANTITY = { value: "", unit: "" }; -export type VerifiedQuantityField = VerifiedField & { +export type VerifiedQuantityList = VerifiedField & { quantities: Quantity[]; }; @@ -116,25 +116,46 @@ export const UNITS = { ingredients: ["%", "ppm"], }; -export const DEFAULT_QUANTITY_FIELD = { +export const DEFAULT_QUANTITY_FIELD: VerifiedQuantityList = { verified: false, quantities: [DEFAULT_QUANTITY], }; +// reg numbers +export enum RegistrationType { + FERTILIZER = "fertilizer_product", + INGREDIENT = "ingredient_component", +} +export type RegistrationNumber = { + identifier: string; + type: RegistrationType; +}; +export const DEFAULT_REGISTRATION_NUMBER: RegistrationNumber = { + identifier: "", + type: RegistrationType.FERTILIZER, +}; +export type RegistrationNumbers = VerifiedField & { + values: RegistrationNumber[]; +}; +export const DEFAULT_REGISTRATION_NUMBERS: RegistrationNumbers = { + verified: false, + values: [DEFAULT_REGISTRATION_NUMBER], +}; + // Base Information export type BaseInformation = { name: VerifiedTextField; - registrationNumber: VerifiedTextField; + registrationNumbers: RegistrationNumbers; lotNumber: VerifiedTextField; npk: VerifiedTextField; - weight: VerifiedQuantityField; - density: VerifiedQuantityField; - volume: VerifiedQuantityField; + weight: VerifiedQuantityList; + density: VerifiedQuantityList; + volume: VerifiedQuantityList; }; export const DEFAULT_BASE_INFORMATION: BaseInformation = { name: DEFAULT_TEXT_FIELD, - registrationNumber: DEFAULT_TEXT_FIELD, + registrationNumbers: DEFAULT_REGISTRATION_NUMBERS, lotNumber: DEFAULT_TEXT_FIELD, npk: DEFAULT_TEXT_FIELD, weight: DEFAULT_QUANTITY_FIELD, diff --git a/src/utils/client/constants.ts b/src/utils/client/constants.ts index cbe5b947..1c8d4760 100644 --- a/src/utils/client/constants.ts +++ b/src/utils/client/constants.ts @@ -1,4 +1,4 @@ -import { LabelData } from "@/types/types"; +import { LabelData, RegistrationType } from "@/types/types"; export const VERIFIED_LABEL_DATA: LabelData = { organizations: [ @@ -17,7 +17,15 @@ export const VERIFIED_LABEL_DATA: LabelData = { ], baseInformation: { name: { value: "SuperGrow 20-20-20", verified: true }, - registrationNumber: { value: "1234567A", verified: true }, + registrationNumbers: { + values: [ + { + identifier: "", + type: RegistrationType.FERTILIZER, + }, + ], + verified: true, + }, lotNumber: { value: "LOT-4567", verified: true }, npk: { value: "20-20-20", verified: true }, weight: { diff --git a/src/utils/client/fieldValidation.ts b/src/utils/client/fieldValidation.ts index 4f2ffb85..f7acbc2a 100644 --- a/src/utils/client/fieldValidation.ts +++ b/src/utils/client/fieldValidation.ts @@ -14,7 +14,6 @@ export const checkFieldArray = ( fields: VerifiedField[], verified: boolean = true, ): boolean => { - console.debug("type of fields", typeof fields); return fields.every((field) => field.verified === verified); }; diff --git a/src/utils/server/__tests__/modelTransformation.test.ts b/src/utils/server/__tests__/modelTransformation.test.ts index 137fb11e..c9c69c6b 100644 --- a/src/utils/server/__tests__/modelTransformation.test.ts +++ b/src/utils/server/__tests__/modelTransformation.test.ts @@ -255,7 +255,7 @@ describe("mapLabelDataOutputToLabelData", () => { // Base Information expect(result.baseInformation.name.value).toBe(input.fertiliser_name); - expect(result.baseInformation.registrationNumber.value).toBe( + expect(result.baseInformation.registrationNumbers.value).toBe( input.registration_number![0].identifier, ); expect(result.baseInformation.lotNumber.value).toBe(input.lot_number); @@ -341,7 +341,7 @@ describe("mapLabelDataOutputToLabelData", () => { // Base Information expect(result.baseInformation.name.value).toBe(""); - expect(result.baseInformation.registrationNumber.value).toBe(""); + expect(result.baseInformation.registrationNumbers.value).toBe(""); expect(result.baseInformation.lotNumber.value).toBe(""); expect(result.baseInformation.npk.value).toBe(""); expect(result.baseInformation.weight.quantities).toEqual([]); @@ -493,7 +493,7 @@ describe("mapInspectionToLabelData", () => { expect(result.organizations[1].phoneNumber.value).toBe("987-654-3210"); expect(result.baseInformation.name.value).toBe("SuperGrow"); - expect(result.baseInformation.registrationNumber.value).toBe("1234567A"); + expect(result.baseInformation.registrationNumbers.value).toBe("1234567A"); expect(result.baseInformation.lotNumber.value).toBe("LOT42"); expect(result.baseInformation.npk.value).toBe("10-5-5"); @@ -562,7 +562,7 @@ describe("mapInspectionToLabelData", () => { const result = mapInspectionToLabelData(emptyInspection); expect(result.organizations).toEqual([]); expect(result.baseInformation.name.value).toBe(""); - expect(result.baseInformation.registrationNumber.value).toBe(""); + expect(result.baseInformation.registrationNumbers.value).toBe(""); expect(result.baseInformation.lotNumber.value).toBe(""); expect(result.baseInformation.npk.value).toBe(""); expect(result.baseInformation.weight.quantities).toEqual([]); @@ -588,7 +588,7 @@ describe("mapInspectionToLabelData", () => { const result = mapInspectionToLabelData(emptyInspection); expect(result.organizations).toEqual([]); expect(result.baseInformation.name.verified).toBe(true); - expect(result.baseInformation.registrationNumber.verified).toBe(true); + expect(result.baseInformation.registrationNumbers.verified).toBe(true); expect(result.baseInformation.lotNumber.verified).toBe(true); expect(result.baseInformation.npk.verified).toBe(true); expect(result.baseInformation.weight.verified).toBe(true); @@ -630,7 +630,7 @@ const labelData: LabelData = { ], baseInformation: { name: { value: "SuperGrow", verified: false }, - registrationNumber: { value: "1234567A", verified: false }, + registrationNumbers: { value: "1234567A", verified: false }, lotNumber: { value: "LOT42", verified: false }, npk: { value: "10-5-5", verified: false }, weight: { quantities: [{ value: "20", unit: "kg" }], verified: false }, @@ -703,7 +703,7 @@ const emptyLabelData: LabelData = { organizations: [], baseInformation: { name: { value: "", verified: false }, - registrationNumber: { value: "", verified: false }, + registrationNumbers: { value: "", verified: false }, lotNumber: { value: "", verified: false }, npk: { value: "", verified: false }, weight: { quantities: [], verified: false }, diff --git a/src/utils/server/modelTransformation.ts b/src/utils/server/modelTransformation.ts index 210102b6..3992fc99 100644 --- a/src/utils/server/modelTransformation.ts +++ b/src/utils/server/modelTransformation.ts @@ -91,7 +91,7 @@ export function mapLabelDataOutputToLabelData( ], baseInformation: { name: { value: data.fertiliser_name ?? "", verified: false }, - registrationNumber: { + registrationNumbers: { value: data.registration_number?.[0]?.identifier ?? "", verified: false, }, @@ -151,7 +151,7 @@ export function mapInspectionToLabelData( })), baseInformation: { name: { value: inspection.product.name ?? "", verified: v }, - registrationNumber: { + registrationNumbers: { value: inspection.product.registration_numbers?.[0]?.registration_number ?? "", @@ -231,7 +231,7 @@ export function mapLabelDataToLabelDataInput( fertiliser_name: labelData.baseInformation.name.value, registration_number: [ { - identifier: labelData.baseInformation.registrationNumber.value, + identifier: labelData.baseInformation.registrationNumbers.value, }, ], lot_number: labelData.baseInformation.lotNumber.value, @@ -305,7 +305,7 @@ export function mapLabelDataToInspectionUpdate( registration_numbers: [ { registration_number: - labelData.baseInformation.registrationNumber.value, + labelData.baseInformation.registrationNumbers.value, }, ], lot_number: labelData.baseInformation.lotNumber.value,