-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAppointmentForm.tsx
140 lines (132 loc) · 4.27 KB
/
AppointmentForm.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import dayjs from 'dayjs';
import { Box, Text } from '@mantine/core';
import { Appointment, Patient, Practitioner } from '@medplum/fhirtypes';
import { createReference, formatHumanName } from '@medplum/core';
import { Form, ResourceForm } from '../Form';
export function formatTimeInput(baseDate: Date, timeInput: string) {
const [hour, minute] = timeInput.split(':');
return dayjs(baseDate).set('hour', parseInt(hour)).set('minute', parseInt(minute)).set('second', 0).toISOString();
}
interface AppointmentFormProps {
editAppointment?: Appointment;
patient?: Patient;
selectedPractitioner?: Practitioner;
selectedDate: Date;
disabled?: boolean;
onSuccess?: () => void;
}
export function AppointmentForm(props: AppointmentFormProps): JSX.Element {
const { editAppointment, patient, selectedPractitioner, selectedDate, disabled = false, onSuccess } = props;
function appointmentToFormData(appointment: Appointment): Record<string, any> {
const patient = appointment.participant.find((p) => p?.actor?.resource?.resourceType === 'Patient')?.actor
?.resource as Patient | undefined;
return {
patient,
serviceType: appointment.serviceType?.[0].coding?.[0],
startTime: dayjs(appointment.start).format('HH:mm'),
endTime: dayjs(appointment.end).format('HH:mm'),
};
}
function formDataToAppointment(formData: Record<string, any>): Record<string, any> {
return {
...formData,
resourceType: 'Appointment',
status: 'booked',
startTime: undefined,
endTime: undefined,
patient: undefined,
start: formatTimeInput(selectedDate, formData.startTime),
end: formatTimeInput(selectedDate, formData.endTime),
participant: [
{
actor: createReference(selectedPractitioner as Practitioner),
status: 'accepted',
},
{
actor: createReference(patient ?? formData.patient),
status: 'accepted',
},
],
};
}
return (
<>
{selectedDate && (
<Box pb="xs">
<Text display="inline-block" fw="bold">
Date:
</Text>
<Text display="inline-block" pl="xs">
{dayjs(selectedDate).format('MMM DD, YYYY')}
</Text>
</Box>
)}
<Box pb="xs">
<Text display="inline-block" fw="bold">
Practitioner:
</Text>
<Text display="inline-block" pl="xs">
{selectedPractitioner?.name && formatHumanName(selectedPractitioner.name[0])}
</Text>
</Box>
{patient && (
<Box pb="xs">
<Text display="inline-block" fw="bold">
Patient:
</Text>
<Text display="inline-block" pl="xs">
{patient.name?.[0] && formatHumanName(patient.name?.[0])}
</Text>
</Box>
)}
<ResourceForm
resourceToFormData={appointmentToFormData}
formDataToResource={formDataToAppointment}
defaultData={editAppointment}
onSuccess={onSuccess}
>
{({ onChange, defaultData }) => (
<>
{!patient && (
<Form.ResourceInput
resourceType="Patient"
label="Select patient"
name="patient"
onChange={onChange}
defaultValue={patient ?? defaultData?.patient}
disabled={disabled}
required
/>
)}
<Form.CodingInput
label="Service Type"
name="serviceType"
path="serviceType"
binding="http://hl7.org/fhir/ValueSet/service-type"
onChange={onChange}
defaultValue={defaultData?.serviceType}
disabled={disabled}
required
/>
<Form.TimeInput
label="Starts at"
name="startTime"
onChange={onChange}
defaultValue={defaultData?.startTime}
disabled={disabled}
required
/>
<Form.TimeInput
label="Ends at"
name="endTime"
onChange={onChange}
defaultValue={defaultData?.endTime}
disabled={disabled}
required
/>
</>
)}
</ResourceForm>
</>
);
}