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
241 lines (217 loc) · 6.81 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
import React, { useEffect, useState } from 'react'
import { useParams, useHistory } from 'react-router'
import format from 'date-fns/format'
import LabRepository from 'clients/db/LabRepository'
import Lab from 'model/Lab'
import Patient from 'model/Patient'
import PatientRepository from 'clients/db/PatientRepository'
import useTitle from 'page-header/useTitle'
import { useTranslation } from 'react-i18next'
import { Row, Column, Badge, Button, Alert } from '@hospitalrun/components'
import TextFieldWithLabelFormGroup from 'components/input/TextFieldWithLabelFormGroup'
import { useButtonToolbarSetter } from 'page-header/ButtonBarProvider'
import useAddBreadcrumbs from 'breadcrumbs/useAddBreadcrumbs'
import { useSelector } from 'react-redux'
import Permissions from 'model/Permissions'
import { RootState } from '../store'
const getTitle = (patient: Patient | undefined, lab: Lab | undefined) =>
patient && lab ? `${lab.type} for ${patient.fullName}` : ''
const ViewLab = () => {
const { id } = useParams()
const { t } = useTranslation()
const history = useHistory()
const setButtons = useButtonToolbarSetter()
const { permissions } = useSelector((state: RootState) => state.user)
const [patient, setPatient] = useState<Patient | undefined>()
const [lab, setLab] = useState<Lab | undefined>()
const [isEditable, setIsEditable] = useState<boolean>(true)
const [isResultInvalid, setIsResultInvalid] = useState<boolean>(false)
const [resultFeedback, setResultFeedback] = useState()
const [errorMessage, setErrorMessage] = useState()
useTitle(getTitle(patient, lab))
const breadcrumbs = [
{
i18nKey: 'labs.requests.view',
location: `/labs/${lab?.id}`,
},
]
useAddBreadcrumbs(breadcrumbs)
useEffect(() => {
const fetchLab = async () => {
if (id) {
const fetchedLab = await LabRepository.find(id)
setLab(fetchedLab)
setIsEditable(fetchedLab.status === 'requested')
}
}
fetchLab()
}, [id])
useEffect(() => {
const fetchPatient = async () => {
if (lab) {
const fetchedPatient = await PatientRepository.find(lab.patientId)
setPatient(fetchedPatient)
}
}
fetchPatient()
}, [lab])
const onResultChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
const result = event.currentTarget.value
const newLab = lab as Lab
setLab({ ...newLab, result })
}
const onNotesChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
const notes = event.currentTarget.value
const newLab = lab as Lab
setLab({ ...newLab, notes })
}
const onUpdate = async () => {
await LabRepository.saveOrUpdate(lab as Lab)
history.push('/labs')
}
const onComplete = async () => {
const newLab = lab as Lab
if (!newLab.result) {
setIsResultInvalid(true)
setResultFeedback(t('labs.requests.error.resultRequiredToComplete'))
setErrorMessage(t('labs.requests.error.unableToComplete'))
return
}
await LabRepository.saveOrUpdate({
...newLab,
completedOn: new Date().toISOString(),
status: 'completed',
})
history.push('/labs')
}
const onCancel = async () => {
const newLab = lab as Lab
await LabRepository.saveOrUpdate({
...newLab,
canceledOn: new Date().toISOString(),
status: 'canceled',
})
history.push('/labs')
}
const getButtons = () => {
const buttons: React.ReactNode[] = []
if (lab?.status === 'completed' || lab?.status === 'canceled') {
return buttons
}
if (permissions.includes(Permissions.CompleteLab)) {
buttons.push(
<Button onClick={onComplete} color="primary">
{t('labs.requests.complete')}
</Button>,
)
}
if (permissions.includes(Permissions.CancelLab)) {
buttons.push(
<Button onClick={onCancel} color="danger">
{t('labs.requests.cancel')}
</Button>,
)
}
return buttons
}
setButtons(getButtons())
if (lab && patient) {
const getBadgeColor = () => {
if (lab.status === 'completed') {
return 'primary'
}
if (lab.status === 'canceled') {
return 'danger'
}
return 'warning'
}
const getCanceledOnOrCompletedOnDate = () => {
if (lab.status === 'completed') {
return (
<Column>
<div className="form-group">
<h4>{t('labs.lab.completedOn')}</h4>
<h5>{format(new Date(lab.completedOn), 'yyyy-MM-dd hh:mm a')}</h5>
</div>
</Column>
)
}
if (lab.status === 'canceled') {
return (
<Column>
<div className="form-group">
<h4>{t('labs.lab.canceledOn')}</h4>
<h5>{format(new Date(lab.completedOn), 'yyyy-MM-dd hh:mm a')}</h5>
</div>
</Column>
)
}
return <></>
}
return (
<>
{isResultInvalid && (
<Alert color="danger" title={t('states.error')} message={errorMessage} />
)}
<Row>
<Column>
<div className="form-group">
<h4>{t('labs.lab.status')}</h4>
<Badge color={getBadgeColor()}>
<h5>{lab.status}</h5>
</Badge>
</div>
</Column>
<Column>
<div className="form-group">
<h4>{t('labs.lab.for')}</h4>
<h5>{patient.fullName}</h5>
</div>
</Column>
<Column>
<div className="form-group">
<h4>{t('labs.lab.type')}</h4>
<h5>{lab.type}</h5>
</div>
</Column>
<Column>
<div className="form-group">
<h4>{t('labs.lab.requestedOn')}</h4>
<h5>{format(new Date(lab.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')}
isEditable={isEditable}
isInvalid={isResultInvalid}
feedback={resultFeedback}
onChange={onResultChange}
/>
<TextFieldWithLabelFormGroup
name="notes"
label={t('labs.lab.notes')}
value={lab.notes}
isEditable={isEditable}
onChange={onNotesChange}
/>
{isEditable && (
<div className="row float-right">
<div className="btn-group btn-group-lg mt-3">
<Button className="mr-2" color="success" onClick={onUpdate}>
{t('actions.update')}
</Button>
</div>
</div>
)}
</form>
</>
)
}
return <h1>Loading...</h1>
}
export default ViewLab