Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) O3-2779: Error messages in drug order form should be translated #1652

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ import { Add, ArrowLeft, Subtract } from '@carbon/react/icons';
import { z } from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
import { Controller, useController, useForm } from 'react-hook-form';
import { age, formatDate, parseDate, useConfig, useLayoutType, usePatient } from '@openmrs/esm-framework';
import {
age,
formatDate,
parseDate,
translateFrom,
useConfig,
useLayoutType,
usePatient,
} from '@openmrs/esm-framework';
import { useOrderConfig } from '../api/order-config';
import { type ConfigObject } from '../config-schema';
import type {
Expand All @@ -37,6 +45,7 @@ import type {
QuantityUnit,
} from '../types';
import styles from './drug-order-form.scss';
import { moduleName } from '../dashboard.meta';

export interface DrugOrderFormProps {
initialOrderBasketItem: DrugOrderBasketItem;
Expand All @@ -51,10 +60,32 @@ const comboSchema = {
};

const schemaFields = {
freeTextDosage: z.string().refine((value) => value !== '', { message: 'Add free dosage note' }),
dosage: z.number({ invalid_type_error: 'A dosage is required' }),
unit: z.object({ ...comboSchema }, { invalid_type_error: 'Please select a unit' }),
route: z.object({ ...comboSchema }, { invalid_type_error: 'Please select a route' }),
// t( 'freeDosageErrorMessage', 'Add free dosage note')
freeTextDosage: z.string().refine((value) => value !== '', {
message: translateFrom(moduleName, 'freeDosageErrorMessage', 'Add free dosage note'),
}),

// t( 'dosageRequiredErrorMessage', 'A dosage is required' )
dosage: z.number({
invalid_type_error: translateFrom(moduleName, 'dosageRequiredErrorMessage', 'A dosage is required'),
vasharma05 marked this conversation as resolved.
Show resolved Hide resolved
}),

// t( 'selectUnitErrorMessage', 'Please select a unit' )
unit: z.object(
{ ...comboSchema },
{
invalid_type_error: translateFrom(moduleName, 'selectUnitErrorMessage', 'Please select a unit'),
},
),

// t( 'selectRouteErrorMessage', 'Please select a route' )
route: z.object(
{ ...comboSchema },
{
invalid_type_error: translateFrom(moduleName, 'selectRouteErrorMessage', 'Please select a route'),
},
),
Comment on lines +69 to +87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not directly related to your changes, but it appears that invalid_type_error is getting conflated with required_error here.

Per the Zod documentation, invalid_type_error is intended to customize the error message for type mismatches. This means it will trigger an error if the data types of the provided values do not align with those specified in the schema. So in this case, we'll expect to see the error if the provided content is not a string.

However, the current error messages suggest an intent to enforce the presence of these fields, which aligns more closely with the purpose of required_error.


patientInstructions: z.string().nullable(),
asNeeded: z.boolean(),
asNeededCondition: z.string().nullable(),
Expand All @@ -63,9 +94,17 @@ const schemaFields = {
pillsDispensed: z.number().nullable(),
quantityUnits: z.object({ ...comboSchema }).nullable(),
numRefills: z.number().nullable(),
indication: z.string().refine((value) => value !== '', { message: 'Please add an indication' }),
indication: z.string().refine((value) => value !== '', {
message: translateFrom(moduleName, 'indicationErrorMessage', 'Please add an indication'),
}),
startDate: z.date(),
frequency: z.object({ ...comboSchema }, { invalid_type_error: 'Please select a frequency' }),
// t( 'selectFrequencyErrorMessage', 'Please select a frequency' )
frequency: z.object(
{ ...comboSchema },
{
invalid_type_error: translateFrom(moduleName, 'selectFrequencyErrorMessage', 'Please select a frequency'),
},
),
};

const medicationOrderFormSchema = z.discriminatedUnion('isFreeTextDosage', [
Expand Down
1 change: 1 addition & 0 deletions packages/esm-patient-medications-app/src/dashboard.meta.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const moduleName = '@openmrs/esm-patient-medications-app';
export const dashboardMeta = {
slot: 'patient-chart-medications-dashboard-slot',
columns: 1,
Expand Down
4 changes: 1 addition & 3 deletions packages/esm-patient-medications-app/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { defineConfigSchema, getAsyncLifecycle, getSyncLifecycle } from '@openmrs/esm-framework';
import { createDashboardLink, registerWorkspace } from '@openmrs/esm-patient-common-lib';
import { configSchema } from './config-schema';
import { dashboardMeta } from './dashboard.meta';
import { dashboardMeta, moduleName } from './dashboard.meta';
import medicationsSummaryComponent from './medications-summary/medications-summary.component';
import activeMedicationsComponent from './active-medications/active-medications.component';

export const importTranslation = require.context('../translations', false, /.json$/, 'lazy');

const moduleName = '@openmrs/esm-patient-medications-app';

const options = {
featureName: 'patient-medications',
moduleName,
Expand Down
5 changes: 5 additions & 0 deletions packages/esm-patient-medications-app/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"discontinue": "Discontinue",
"dispensingInformation": "3. Dispensing instructions",
"dosageInstructions": "1. Dosage instructions",
"dosageRequiredErrorMessage": "A dosage is required",
"dose": "Dose",
"drugAlreadyPrescribed": "Already prescribed",
"drugOrders": "Drug orders",
Expand All @@ -31,6 +32,7 @@
"errorFetchingDrugResults": "Error fetching results for \"{{searchTerm}}\"",
"errorFetchingOrderConfig": "Error occured when fetching Order config",
"female": "Female",
"freeDosageErrorMessage": "Add free dosage note",
"freeTextDosage": "Free text dosage",
"goToDrugOrderForm": "Order form",
"increment": "Increment",
Expand Down Expand Up @@ -73,6 +75,9 @@
"searchFieldPlaceholder": "Search for a drug or orderset (e.g. \"Aspirin\")",
"searchResultsMatchesForTerm_one": "{{count}} result for \"{{searchTerm}}\"",
"searchResultsMatchesForTerm_other": "{{count}} results for \"{{searchTerm}}\"",
"selectFrequencyErrorMessage": "Please select a frequency",
"selectRouteErrorMessage": "Please select a route",
"selectUnitErrorMessage": "Please select a unit",
"startDate": "Start date",
"takeAsNeeded": "Take as needed",
"tryReopeningTheForm": "Please try launching the form again",
Expand Down
Loading