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
/
ViewLab.tsx
255 lines (230 loc) · 7.47 KB
/
ViewLab.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { Row, Column, Badge, Button, Alert, Toast, Callout, Label } from '@hospitalrun/components'
import format from 'date-fns/format'
import React, { useEffect, useState } from 'react'
import { useSelector, useDispatch } from 'react-redux'
import { useParams, useHistory } from 'react-router-dom'
import useAddBreadcrumbs from '../page-header/breadcrumbs/useAddBreadcrumbs'
import useTitle from '../page-header/title/useTitle'
import TextFieldWithLabelFormGroup from '../shared/components/input/TextFieldWithLabelFormGroup'
import useTranslator from '../shared/hooks/useTranslator'
import Lab from '../shared/model/Lab'
import Patient from '../shared/model/Patient'
import Permissions from '../shared/model/Permissions'
import { RootState } from '../shared/store'
import { uuid } from '../shared/util/uuid'
import { cancelLab, completeLab, updateLab, fetchLab } from './lab-slice'
const getTitle = (patient: Patient | undefined, lab: Lab | undefined) =>
patient && lab ? `${lab.type} for ${patient.fullName}(${lab.code})` : ''
const ViewLab = () => {
const { id } = useParams()
const { t } = useTranslator()
const history = useHistory()
const dispatch = useDispatch()
const { permissions } = useSelector((state: RootState) => state.user)
const { lab, patient, status, error } = useSelector((state: RootState) => state.lab)
const [labToView, setLabToView] = useState<Lab>()
const [newNotes, setNewNotes] = useState<string>()
const [isEditable, setIsEditable] = useState<boolean>(true)
useTitle(getTitle(patient, labToView))
const breadcrumbs = [
{
i18nKey: 'labs.requests.view',
location: `/labs/${labToView?.id}`,
},
]
useAddBreadcrumbs(breadcrumbs)
useEffect(() => {
if (id) {
dispatch(fetchLab(id))
}
}, [id, dispatch])
useEffect(() => {
if (lab) {
setLabToView({ ...lab })
setIsEditable(lab.status === 'requested')
}
}, [lab])
const onResultChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
const result = event.currentTarget.value
const newLab = labToView as Lab
setLabToView({ ...newLab, result })
}
const onNotesChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
const notes = event.currentTarget.value
setNewNotes(notes)
}
const onUpdate = async () => {
const onSuccess = (update: Lab) => {
history.push(`/labs/${update.id}`)
Toast(
'success',
t('states.success'),
`${t('labs.successfullyUpdated')} ${update.type} for ${patient?.fullName}`,
)
}
if (labToView) {
const newLab = labToView as Lab
if (newNotes) {
newLab.notes = newLab.notes ? [...newLab.notes, newNotes] : [newNotes]
setNewNotes('')
}
dispatch(updateLab(newLab, onSuccess))
}
}
const onComplete = async () => {
const onSuccess = (complete: Lab) => {
history.push(`/labs/${complete.id}`)
Toast(
'success',
t('states.success'),
`${t('labs.successfullyCompleted')} ${complete.type} for ${patient?.fullName} `,
)
}
if (labToView) {
dispatch(completeLab(labToView, onSuccess))
}
}
const onCancel = async () => {
const onSuccess = () => {
history.push('/labs')
}
if (labToView) {
dispatch(cancelLab(labToView, onSuccess))
}
}
const getButtons = () => {
const buttons: React.ReactNode[] = []
if (labToView?.status === 'completed' || labToView?.status === 'canceled') {
return buttons
}
buttons.push(
<Button className="mr-2" color="success" onClick={onUpdate} key="actions.update">
{t('actions.update')}
</Button>,
)
if (permissions.includes(Permissions.CompleteLab)) {
buttons.push(
<Button className="mr-2" onClick={onComplete} color="primary" key="labs.requests.complete">
{t('labs.requests.complete')}
</Button>,
)
}
if (permissions.includes(Permissions.CancelLab)) {
buttons.push(
<Button onClick={onCancel} color="danger" key="labs.requests.cancel">
{t('labs.requests.cancel')}
</Button>,
)
}
return buttons
}
if (labToView && patient) {
const getBadgeColor = () => {
if (labToView.status === 'completed') {
return 'primary'
}
if (labToView.status === 'canceled') {
return 'danger'
}
return 'warning'
}
const getCanceledOnOrCompletedOnDate = () => {
if (labToView.status === 'completed' && labToView.completedOn) {
return (
<Column>
<div className="form-group completed-on">
<h4>{t('labs.lab.completedOn')}</h4>
<h5>{format(new Date(labToView.completedOn), 'yyyy-MM-dd hh:mm a')}</h5>
</div>
</Column>
)
}
if (labToView.status === 'canceled' && labToView.canceledOn) {
return (
<Column>
<div className="form-group canceled-on">
<h4>{t('labs.lab.canceledOn')}</h4>
<h5>{format(new Date(labToView.canceledOn), 'yyyy-MM-dd hh:mm a')}</h5>
</div>
</Column>
)
}
return <></>
}
const getPastNotes = () => {
if (labToView.notes && labToView.notes[0] !== '') {
return labToView.notes.map((note: string) => (
<Callout key={uuid()} data-test="note" color="info">
<p>{note}</p>
</Callout>
))
}
return <></>
}
return (
<>
{status === 'error' && (
<Alert color="danger" title={t('states.error')} message={t(error.message || '')} />
)}
<Row>
<Column>
<div className="form-group lab-status">
<h4>{t('labs.lab.status')}</h4>
<Badge color={getBadgeColor()}>
<h5>{labToView.status}</h5>
</Badge>
</div>
</Column>
<Column>
<div className="form-group for-patient">
<h4>{t('labs.lab.for')}</h4>
<h5>{patient.fullName}</h5>
</div>
</Column>
<Column>
<div className="form-group lab-type">
<h4>{t('labs.lab.type')}</h4>
<h5>{labToView.type}</h5>
</div>
</Column>
<Column>
<div className="form-group requested-on">
<h4>{t('labs.lab.requestedOn')}</h4>
<h5>{format(new Date(labToView.requestedOn), 'yyyy-MM-dd hh:mm a')}</h5>
</div>
</Column>
{getCanceledOnOrCompletedOnDate()}
</Row>
<div className="border-bottom" />
<form>
<TextFieldWithLabelFormGroup
name="result"
label={t('labs.lab.result')}
value={labToView.result}
isEditable={isEditable}
isInvalid={!!error.result}
feedback={t(error.result as string)}
onChange={onResultChange}
/>
<Label text={t('labs.lab.notes')} htmlFor="notesTextField" />
{getPastNotes()}
{isEditable && (
<TextFieldWithLabelFormGroup
name="notes"
value={newNotes}
isEditable={isEditable}
onChange={onNotesChange}
/>
)}
{isEditable && (
<div className="row float-right">
<div className="btn-group btn-group-lg mt-3">{getButtons()}</div>
</div>
)}
</form>
</>
)
}
return <h1>Loading...</h1>
}
export default ViewLab