Skip to content

Commit

Permalink
(feat) Conditionally make the order reason field of the lab order for…
Browse files Browse the repository at this point in the history
…m required when configured (#1698)

Conditionally make order reason required when configured

Co-authored-by: Dennis Kigen <[email protected]>
  • Loading branch information
makombe and denniskigen authored Mar 6, 2024
1 parent 78da586 commit c93fe22
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
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(),
});

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

0 comments on commit c93fe22

Please sign in to comment.