forked from HospitalRun/hospitalrun-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReportIncident.tsx
188 lines (176 loc) · 6.05 KB
/
ReportIncident.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
import { Button, Row, Column, Typeahead, Label } from '@hospitalrun/components'
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 DateTimePickerWithLabelFormGroup from '../../shared/components/input/DateTimePickerWithLabelFormGroup'
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 Incident from '../../shared/model/Incident'
import Patient from '../../shared/model/Patient'
import { RootState } from '../../shared/store'
import useReportIncident from '../hooks/useReportIncident'
import { IncidentError } from '../util/validate-incident'
const ReportIncident = () => {
const [mutate] = useReportIncident()
const history = useHistory()
const { t } = useTranslator()
const updateTitle = useUpdateTitle()
const { user } = useSelector((state: RootState) => state.user)
useEffect(() => {
updateTitle(t('incidents.reports.new'))
})
const breadcrumbs = [
{
i18nKey: 'incidents.reports.new',
location: `/incidents/new`,
},
]
useAddBreadcrumbs(breadcrumbs)
const [incident, setIncident] = useState({
reportedBy: user?.fullName, // user is read from redux store state.user and the fullName is used while showing details
reportByUserID: user?.id,
date: new Date().toISOString(),
department: '',
category: '',
categoryItem: '',
description: '',
patient: '',
})
const [error, setError] = useState<IncidentError | undefined>(undefined)
const onDateChange = (newDate: Date) => {
setIncident((prevState) => ({
...prevState,
date: newDate.toISOString(),
}))
}
const onTextInputChange = (text: string, name: string) => {
setIncident((prevState) => ({
...prevState,
[name]: text,
}))
}
const onSave = async () => {
try {
const data = await mutate(incident as Incident)
history.push(`/incidents/${data?.id}`)
} catch (e) {
setError(e)
}
}
const onCancel = () => {
history.push('/incidents')
}
const onPatientChange = (patient: Patient) => {
if (patient) {
setIncident((prevIncident) => ({
...prevIncident,
patient: patient.id,
}))
} else {
setIncident((prevIncident) => ({
...prevIncident,
patient: '',
}))
}
}
return (
<form aria-label="Report Incident form">
<Row>
<Column md={6}>
<DateTimePickerWithLabelFormGroup
name="dateOfIncident"
label={t('incidents.reports.dateOfIncident')}
isEditable
isRequired
onChange={onDateChange}
value={new Date(incident.date)}
isInvalid={!!error?.date}
feedback={t(error?.date as string)}
/>
</Column>
<Column md={6}>
<TextInputWithLabelFormGroup
label={t('incidents.reports.department')}
name="department"
isRequired
isEditable
value={incident.department}
onChange={(event) => onTextInputChange(event.currentTarget.value, 'department')}
isInvalid={!!error?.department}
feedback={t(error?.department as string)}
/>
</Column>
</Row>
<Row>
<Column md={6}>
<TextInputWithLabelFormGroup
name="category"
label={t('incidents.reports.category')}
isEditable
isRequired
value={incident.category}
onChange={(event) => onTextInputChange(event.currentTarget.value, 'category')}
isInvalid={!!error?.category}
feedback={t(error?.category as string)}
/>
</Column>
<Column md={6}>
<TextInputWithLabelFormGroup
label={t('incidents.reports.categoryItem')}
name="categoryItem"
isRequired
isEditable
value={incident.categoryItem}
onChange={(event) => onTextInputChange(event.currentTarget.value, 'categoryItem')}
isInvalid={!!error?.categoryItem}
feedback={t(error?.categoryItem as string)}
/>
</Column>
</Row>
<Row>
<Column md={12}>
<TextFieldWithLabelFormGroup
label={t('incidents.reports.description')}
name="description"
isRequired
isEditable
value={incident.description}
onChange={(event) => onTextInputChange(event.currentTarget.value, 'description')}
isInvalid={!!error?.description}
feedback={t(error?.description as string)}
/>
</Column>
</Row>
<Row>
<Column md={6}>
<div className="form-group patient-typeahead">
<Label htmlFor="patientTypeahead" text={t('incidents.reports.patient')} />
<Typeahead
id="patientTypeahead"
placeholder={t('incidents.reports.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>}
/>
</div>
</Column>
</Row>
<div className="row float-right">
<div className="btn-group btn-group-lg mt-3 mr-3">
<Button className="mr-2" color="success" onClick={onSave}>
{t('incidents.reports.new')}
</Button>
<Button color="danger" onClick={onCancel}>
{t('actions.cancel')}
</Button>
</div>
</div>
</form>
)
}
export default ReportIncident