This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
NewImagingRequest.tsx
220 lines (201 loc) · 6.93 KB
/
NewImagingRequest.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { Typeahead, Label, Button, Alert, Column, Row } from '@hospitalrun/components'
import format from 'date-fns/format'
import React, { useState, useEffect } from 'react'
import { useSelector } from 'react-redux'
import { useHistory } from 'react-router-dom'
import useAddBreadcrumbs from '../../page-header/breadcrumbs/useAddBreadcrumbs'
import { useUpdateTitle } from '../../page-header/title/TitleContext'
import SelectWithLabelFormGroup, {
Option,
} from '../../shared/components/input/SelectWithLabelFormGroup'
import TextFieldWithLabelFormGroup from '../../shared/components/input/TextFieldWithLabelFormGroup'
import TextInputWithLabelFormGroup from '../../shared/components/input/TextInputWithLabelFormGroup'
import PatientRepository from '../../shared/db/PatientRepository'
import useTranslator from '../../shared/hooks/useTranslator'
import Patient from '../../shared/model/Patient'
import { RootState } from '../../shared/store'
import useRequestImaging, { ImagingRequest } from '../hooks/useRequestImaging'
import { ImagingRequestError } from '../util/validate-imaging-request'
const NewImagingRequest = () => {
const { t } = useTranslator()
const history = useHistory()
const { user } = useSelector((state: RootState) => state.user)
const updateTitle = useUpdateTitle()
useEffect(() => {
updateTitle(t('imagings.requests.new'))
})
const [mutate] = useRequestImaging(user)
const [error, setError] = useState<ImagingRequestError>()
const [visitOption, setVisitOption] = useState([] as Option[])
const statusOptions: Option[] = [
{ label: t('imagings.status.requested'), value: 'requested' },
{ label: t('imagings.status.completed'), value: 'completed' },
{ label: t('imagings.status.canceled'), value: 'canceled' },
]
const [newImagingRequest, setNewImagingRequest] = useState<ImagingRequest>({
patient: '',
fullName: '',
status: 'requested',
notes: '',
type: '',
visitId: '',
})
const breadcrumbs = [
{
i18nKey: 'imagings.requests.new',
location: `/imaging/new`,
},
]
useAddBreadcrumbs(breadcrumbs)
const onPatientChange = (patient: Patient) => {
if (patient) {
setNewImagingRequest((previousNewImagingRequest) => ({
...previousNewImagingRequest,
patient: patient.id,
fullName: patient.fullName as string,
}))
const visits = patient.visits?.map((v) => ({
label: `${v.type} at ${format(new Date(v.startDateTime), 'yyyy-MM-dd hh:mm a')}`,
value: v.id,
})) as Option[]
setVisitOption(visits)
} else {
setNewImagingRequest((previousNewImagingRequest) => ({
...previousNewImagingRequest,
patient: '',
fullName: '',
visitId: '',
}))
setVisitOption([])
}
}
const onImagingTypeChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const type = event.currentTarget.value
setNewImagingRequest((previousNewImagingRequest) => ({
...previousNewImagingRequest,
type,
}))
}
const onStatusChange = (value: string) => {
setNewImagingRequest((previousNewImagingRequest) => ({
...previousNewImagingRequest,
status: value as 'completed' | 'canceled' | 'requested',
}))
}
const onVisitChange = (value: string) => {
setNewImagingRequest((previousNewImagingRequest) => ({
...previousNewImagingRequest,
visitId: value,
}))
}
const onNoteChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
const notes = event.currentTarget.value
setNewImagingRequest((previousNewImagingRequest) => ({
...previousNewImagingRequest,
notes,
}))
}
const onSave = async () => {
try {
await mutate(newImagingRequest)
history.push(`/imaging`)
} catch (e) {
setError(e)
}
}
const onCancel = () => {
history.push('/imaging')
}
const defaultSelectedVisitsOption = () => {
if (visitOption !== undefined) {
return visitOption.filter(({ value }) => value === newImagingRequest.visitId)
}
return []
}
return (
<>
{error !== undefined && (
<Alert color="danger" title={t('states.error')} message={t(error.message)} />
)}
<form>
<Row>
<Column>
<div className="form-group patient-typeahead">
<Label htmlFor="patientTypeahead" isRequired text={t('imagings.imaging.patient')} />
<Typeahead
id="patientTypeahead"
placeholder={t('imagings.imaging.patient')}
onChange={(p: Patient[]) => {
onPatientChange(p[0])
}}
onSearch={async (query: string) => PatientRepository.search(query)}
searchAccessor="fullName"
renderMenuItemChildren={(p: Patient) => <div>{`${p.fullName} (${p.code})`}</div>}
isInvalid={!!error?.patient}
feedback={t(error?.patient)}
/>
</div>
</Column>
<Column>
<div className="visits">
<SelectWithLabelFormGroup
name="visit"
label={t('patient.visits.label')}
isRequired
isEditable={newImagingRequest.patient !== undefined}
options={visitOption || []}
defaultSelected={defaultSelectedVisitsOption()}
onChange={(values) => {
onVisitChange(values[0])
}}
/>
</div>
</Column>
</Row>
<TextInputWithLabelFormGroup
name="imagingType"
label={t('imagings.imaging.type')}
isRequired
isEditable
isInvalid={!!error?.type}
feedback={t(error?.type)}
value={newImagingRequest.type}
onChange={onImagingTypeChange}
/>
<div className="imaging-status">
<SelectWithLabelFormGroup
name="status"
label={t('imagings.imaging.status')}
options={statusOptions}
isRequired
isEditable
defaultSelected={statusOptions.filter(
({ value }) => value === newImagingRequest.status,
)}
onChange={(values) => onStatusChange(values[0])}
/>
</div>
<div className="form-group">
<TextFieldWithLabelFormGroup
name="ImagingNotes"
label={t('imagings.imaging.notes')}
isEditable
value={newImagingRequest.notes}
onChange={onNoteChange}
/>
</div>
<div className="row float-right">
<div className="btn-group btn-group-lg mt-3">
<Button className="mr-2" color="success" onClick={onSave}>
{t('imagings.requests.create')}
</Button>
<Button color="danger" onClick={onCancel}>
{t('actions.cancel')}
</Button>
</div>
</div>
</form>
</>
)
}
export default NewImagingRequest