Skip to content

Commit

Permalink
change zod schema to use t function
Browse files Browse the repository at this point in the history
  • Loading branch information
NethmiRodrigo committed Jan 17, 2025
1 parent 03a7ec0 commit 1241ffa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<BedAdministrationFormProps> = ({
allLocations,
availableBedTypes,
Expand All @@ -90,6 +75,30 @@ const BedAdministrationForm: React.FC<BedAdministrationFormProps> = ({
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,
Expand Down
4 changes: 4 additions & 0 deletions packages/esm-bed-management-app/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 1241ffa

Please sign in to comment.