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) Conditionally make the order reason field of the lab order form required when configured #1698

Merged
merged 3 commits into from
Mar 6, 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
6 changes: 6 additions & 0 deletions packages/esm-patient-labs-app/src/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export const configSchema = {
_description: 'UUID of the lab test that requires a reason for ordering',
_default: '',
},
required: {
_type: Type.Boolean,
_description: 'Whether the order reason is required or not',
_default: false,
},
orderReasons: {
_type: Type.Array,
_elements: {
Expand All @@ -87,6 +92,7 @@ export interface LabTestReason {
}
export interface OrderReason {
labTestUuid: string;
required: boolean;
orderReasons: Array<string>;
}
export interface ConfigObject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,6 @@ export interface LabOrderFormProps {
promptBeforeClosing: DefaultWorkspaceProps['promptBeforeClosing'];
}

const labOrderFormSchema = z.object({
instructions: z.string().optional(),
urgency: z.string().refine((value) => value !== '', {
message: translateFrom(moduleName, 'addLabOrderPriorityRequired', 'Priority is required'),
}),
labReferenceNumber: z.string().optional(),
testType: z.object(
{ label: z.string(), conceptUuid: z.string() },
{
required_error: translateFrom(moduleName, 'addLabOrderLabTestTypeRequired', 'Test type is required'),
invalid_type_error: translateFrom(moduleName, 'addLabOrderLabReferenceRequired', 'Test type is required'),
},
),
orderReason: z.string().optional(),
});

// Designs:
// https://app.zeplin.io/project/60d5947dd636aebbd63dce4c/screen/640b06c440ee3f7af8747620
// https://app.zeplin.io/project/60d5947dd636aebbd63dce4c/screen/640b06d286e0aa7b0316db4a
Expand All @@ -69,6 +53,34 @@ export function LabOrderForm({
const { orders, setOrders } = useOrderBasket<LabOrderBasketItem>('labs', prepLabOrderPostData);
const { testTypes, isLoading: isLoadingTestTypes, error: errorLoadingTestTypes } = useTestTypes();
const [showErrorNotification, setShowErrorNotification] = useState(false);
const config = useConfig<ConfigObject>();
const orderReasonRequired = (
config.labTestsWithOrderReasons?.find((c) => c.labTestUuid === initialOrder?.testType?.conceptUuid) || {}
).required;
const labOrderFormSchema = z.object({
instructions: z.string().optional(),
urgency: z.string().refine((value) => value !== '', {
message: translateFrom(moduleName, 'addLabOrderPriorityRequired', 'Priority is required'),
}),
labReferenceNumber: z.string().optional(),
testType: z.object(
{ label: z.string(), conceptUuid: z.string() },
{
required_error: translateFrom(moduleName, 'addLabOrderLabTestTypeRequired', 'Test type is required'),
invalid_type_error: translateFrom(moduleName, 'addLabOrderLabReferenceRequired', 'Test type is required'),
},
),
orderReason: orderReasonRequired
? z
.string({
required_error: translateFrom(moduleName, 'addLabOrderLabOrderReasonRequired', 'Order reason is required'),
})
.refine(
(value) => !!value,
translateFrom(moduleName, 'addLabOrderLabOrderReasonRequired', 'Order reason is required'),
)
: z.string().optional(),
});
denniskigen marked this conversation as resolved.
Show resolved Hide resolved

const {
control,
Expand All @@ -81,7 +93,6 @@ export function LabOrderForm({
...initialOrder,
},
});
const config = useConfig<ConfigObject>();
const orderReasonUuids =
(config.labTestsWithOrderReasons?.find((c) => c.labTestUuid === defaultValues?.testType?.conceptUuid) || {})
.orderReasons || [];
Expand Down Expand Up @@ -223,6 +234,8 @@ export function LabOrderForm({
items={orderReasons}
onBlur={onBlur}
onChange={({ selectedItem }) => onChange(selectedItem?.uuid || '')}
invalid={errors.orderReason?.message}
invalidText={errors.orderReason?.message}
/>
)}
/>
Expand Down
Loading