Skip to content

Commit

Permalink
Add validations for quantity
Browse files Browse the repository at this point in the history
  • Loading branch information
makombe committed Oct 14, 2024
1 parent 4a5a923 commit bd0f30e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 51 deletions.
18 changes: 2 additions & 16 deletions src/form/add-medical-supply-order/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ export function useOrderReasons(conceptUuids: Array<string>) {
}

export interface MedicalSupplyOrderPost extends OrderPost {
scheduledDate?: Date | string;
commentToFulfiller?: string;
laterality?: string;
bodySite?: string;
quantity?: number;
quantityUnits?: string;
}

export function prepMedicalSupplyOrderPostData(
Expand All @@ -61,12 +59,8 @@ export function prepMedicalSupplyOrderPostData(
encounter: encounterUuid,
concept: order.testType.conceptUuid,
instructions: order.instructions,
orderReason: order.orderReason,
urgency: order.urgency,
};
if (order.urgency === "ON_SCHEDULED_DATE") {
payload["scheduledDate"] = order.scheduleDate;
}
return payload;
} else if (order.action === "REVISE") {
payload = {
Expand All @@ -78,12 +72,8 @@ export function prepMedicalSupplyOrderPostData(
encounter: encounterUuid,
concept: order.testType.conceptUuid,
instructions: order.instructions,
orderReason: order.orderReason,

};
if (order.urgency === "ON_SCHEDULED_DATE") {
payload["scheduledDate"] = order.scheduleDate;
}
return payload;
} else if (order.action === "DISCONTINUE") {
payload = {
Expand All @@ -94,11 +84,7 @@ export function prepMedicalSupplyOrderPostData(
orderer: order.orderer,
encounter: encounterUuid,
concept: order.testType.conceptUuid,
orderReason: order.orderReason,
};
if (order.urgency === "ON_SCHEDULED_DATE") {
payload["scheduledDate"] = order.scheduleDate;
}
return payload;
} else {
throw new Error(`Unknown order action: ${order.action}.`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import { type MedicalSupplyConfig } from '../../../config-schema';
import styles from './medical-supply-form.scss';
import { type MedicalSupplyOrderBasketItem } from '../../../types';
import { priorityOptions } from './medical-supply-order';
import { useMedicalSupplyTypes } from '../../../hooks/useMedicalSupplyTypes';
import { careSettingUuid, prepMedicalSupplyOrderPostData } from '../api';

export interface MedicalSupplyOrderFormProps {
Expand All @@ -54,7 +53,6 @@ export function MedicalSupplyOrderForm({
'medicalsupply',
prepMedicalSupplyOrderPostData,
);
const { medicalSupplyTypes, isLoading: isLoadingTestTypes, error: errorLoadingTestTypes } = useMedicalSupplyTypes();
const [showErrorNotification, setShowErrorNotification] = useState(false);

const config = useConfig<MedicalSupplyConfig>();
Expand All @@ -74,12 +72,14 @@ export function MedicalSupplyOrderForm({
),
},
),
scheduleDate: z.union([z.string(), z.date(), z.string().optional()]),
commentsToFulfiller: z.string().optional(),
quantityUnits: z.string().optional(),
quantity: z.number().refine((value) => value !== null && value !== undefined && value > 0, {
message: translateFrom(moduleName, 'quantityRequired', 'Quantity is required'),
}),
quantity: z
.number({
required_error: translateFrom(moduleName, 'quantityRequired', 'Quantity is required'),
invalid_type_error: translateFrom(moduleName, 'quantityInvalid', 'Quantity must be a number'),
})
.min(1, { message: translateFrom(moduleName, 'quantityMin', 'Quantity must be greater than 0') }),
});

const {
Expand Down Expand Up @@ -128,19 +128,8 @@ export function MedicalSupplyOrderForm({
promptBeforeClosing(() => isDirty);
}, [isDirty, promptBeforeClosing]);

const [showScheduleDate, setShowScheduleDate] = useState(false);

return (
<>
{errorLoadingTestTypes && (
<InlineNotification
kind="error"
lowContrast
className={styles.inlineNotification}
title={t('errorLoadingMedicalSupplyTypes', 'Error occurred when loading medical supply types')}
subtitle={t('tryReopeningTheForm', 'Please try launching the form again')}
/>
)}
<Form
className={styles.orderForm}
onSubmit={handleSubmit(handleFormSubmission, onError)}
Expand All @@ -161,15 +150,10 @@ export function MedicalSupplyOrderForm({
id="medicalSupplyTypeInput"
titleText={t('medicalSupplyType', 'Medical supply type')}
selectedItem={value}
items={''}
placeholder={
isLoadingTestTypes
? `${t('loading', 'Loading')}...`
: t('medicalSupplyTypePlaceholder', 'Select one')
}
onBlur={onBlur}
disabled={isLoadingTestTypes}
onChange={({ selectedItem }) => onChange(selectedItem)}
onChange={({ selectedItem }) => {
onChange(selectedItem);
}}
invalid={errors.testType?.message}
invalidText={errors.testType?.message}
/>
Expand All @@ -194,7 +178,6 @@ export function MedicalSupplyOrderForm({
onBlur={onBlur}
onChange={({ selectedItem }) => {
onChange(selectedItem?.value || '');
setShowScheduleDate(selectedItem?.label === 'Scheduled');
}}
invalid={errors.urgency?.message}
invalidText={errors.urgency?.message}
Expand All @@ -212,15 +195,18 @@ export function MedicalSupplyOrderForm({
control={control}
render={({ field: { onChange, onBlur, value } }) => (
<NumberInput
enableCounter
hideSteppers={true}
allowEmpty={false}
id="quantityInput"
size="lg"
label={t('quantity', 'Quantity')}
value={value}
onChange={onChange}
onChange={(e) => {
onChange(Number(e.target?.value));
}}
onBlur={onBlur}
min={0}
invalid={errors.quantity?.message}
min={1}
invalid={!!errors.quantity?.message}
invalidText={errors.quantity?.message}
/>
)}
Expand Down Expand Up @@ -267,8 +253,6 @@ export function MedicalSupplyOrderForm({
onChange={onChange}
onBlur={onBlur}
maxCount={500}
invalid={errors.instructions?.message}
invalidText={errors.instructions?.message}
/>
)}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"name": "medical-supply-order-panel",
"component": "medicalSupplyOrderPanel",
"slot": "order-basket-slot",
"order": 3
"order": 5
}

],
Expand Down
2 changes: 0 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ export interface MedicalSupplyOrderBasketItem extends OrderBasketItem {
};
urgency?: string;
instructions?: string;
orderReason?: string;
scheduleDate?: Date | string;
quantity?: number;
quantityUnits?: string;
}
Expand Down

0 comments on commit bd0f30e

Please sign in to comment.