From 1241ffa899428fb290129d875986b998702508a7 Mon Sep 17 00:00:00 2001 From: Nethmi Rodrigo Date: Fri, 17 Jan 2025 19:13:18 +0530 Subject: [PATCH] change zod schema to use t function --- .../bed-administration-form.component.tsx | 41 +++++++++++-------- .../translations/en.json | 4 ++ 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/packages/esm-bed-management-app/src/bed-administration/bed-administration-form.component.tsx b/packages/esm-bed-management-app/src/bed-administration/bed-administration-form.component.tsx index b03183b9f..377629b9c 100644 --- a/packages/esm-bed-management-app/src/bed-administration/bed-administration-form.component.tsx +++ b/packages/esm-bed-management-app/src/bed-administration/bed-administration-form.component.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useState, useMemo } from 'react'; import capitalize from 'lodash-es/capitalize'; import { z } from 'zod'; import { useForm, Controller } from 'react-hook-form'; @@ -59,21 +59,6 @@ const numberInString = z.string().transform((val, ctx) => { return val; }); -const BedAdministrationSchema = z.object({ - bedColumn: numberInString, - bedId: z.string().max(255), - bedRow: numberInString, - bedType: z.string().refine((value) => value != '', { - message: translateFrom(moduleName, 'invalidBedType', 'Please select a valid bed type'), - }), - location: z.object({ display: z.string(), uuid: z.string() }).refine((value) => value.display != '', { - message: translateFrom(moduleName, 'invalidLocation', 'Please select a valid location'), - }), - occupancyStatus: z.string().refine((value) => value != '', { - message: translateFrom(moduleName, 'invalidOccupancyStatus', 'Please select a valid occupied status'), - }), -}); - const BedAdministrationForm: React.FC = ({ allLocations, availableBedTypes, @@ -90,6 +75,30 @@ const BedAdministrationForm: React.FC = ({ const [showErrorNotification, setShowErrorNotification] = useState(false); const [formStateError, setFormStateError] = useState(''); + const BedAdministrationSchema = useMemo( + () => + z.object({ + bedId: z + .string() + .max(255) + .refine((value) => value !== '', { + message: t('invalidBedId', 'Bed ID cannot be empty'), + }), + bedRow: numberInString, + bedColumn: numberInString, + location: z.object({ display: z.string(), uuid: z.string() }).refine((value) => value.display != '', { + message: t('invalidLocation', 'Please select a valid location'), + }), + occupancyStatus: z.string().refine((value) => value != '', { + message: t('invalidOccupancyStatus', 'Please select a valid occupied status'), + }), + bedType: z.string().refine((value) => value != '', { + message: t('invalidBedType', 'Please select a valid bed type'), + }), + }), + [t], + ); + const { handleSubmit, control, diff --git a/packages/esm-bed-management-app/translations/en.json b/packages/esm-bed-management-app/translations/en.json index 73c476251..95b7a7cf9 100644 --- a/packages/esm-bed-management-app/translations/en.json +++ b/packages/esm-bed-management-app/translations/en.json @@ -49,6 +49,10 @@ "filterByOccupancyStatus": "Filter by occupancy status", "id": "ID", "ids": "Id", + "invalidBedId": "Bed ID cannot be empty", + "invalidBedType": "Please select a valid bed type", + "invalidLocation": "Please select a valid location", + "invalidOccupancyStatus": "Please select a valid occupied status", "itemsPerPage": "Items per page:", "loading": "Loading...", "location": "Location",