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
/
EditPatient.tsx
118 lines (102 loc) · 3.16 KB
/
EditPatient.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
import React, { useEffect, useState } from 'react'
import { useHistory, useParams } from 'react-router-dom'
import { useTranslation } from 'react-i18next'
import { useDispatch, useSelector } from 'react-redux'
import { Spinner, Button, Toast } from '@hospitalrun/components'
import GeneralInformation from '../GeneralInformation'
import useTitle from '../../page-header/useTitle'
import Patient from '../../model/Patient'
import { updatePatient, fetchPatient } from '../patient-slice'
import { RootState } from '../../store'
import { getPatientFullName, getPatientName } from '../util/patient-name-util'
import useAddBreadcrumbs from '../../breadcrumbs/useAddBreadcrumbs'
const getPatientCode = (p: Patient): string => {
if (p) {
return p.code
}
return ''
}
const EditPatient = () => {
const { t } = useTranslation()
const history = useHistory()
const dispatch = useDispatch()
const [patient, setPatient] = useState({} as Patient)
const [errorMessage, setErrorMessage] = useState('')
const { patient: reduxPatient, isLoading } = useSelector((state: RootState) => state.patient)
useTitle(
`${t('patients.editPatient')}: ${getPatientFullName(reduxPatient)} (${getPatientCode(
reduxPatient,
)})`,
)
const breadcrumbs = [
{ i18nKey: 'patients.label', location: '/patients' },
{ text: getPatientFullName(reduxPatient), location: `/patients/${reduxPatient.id}` },
{ i18nKey: 'patients.editPatient', location: `/patients/${reduxPatient.id}/edit` },
]
useAddBreadcrumbs(breadcrumbs, true)
useEffect(() => {
setPatient(reduxPatient)
}, [reduxPatient])
const { id } = useParams()
useEffect(() => {
if (id) {
dispatch(fetchPatient(id))
}
}, [id, dispatch])
const onCancel = () => {
history.push(`/patients/${patient.id}`)
}
const onSuccessfulSave = (updatedPatient: Patient) => {
history.push(`/patients/${updatedPatient.id}`)
Toast(
'success',
t('states.success'),
`${t('patients.successfullyUpdated')} ${patient.fullName}`,
)
}
const onSave = () => {
if (!patient.givenName) {
setErrorMessage(t('patient.errors.patientGivenNameRequired'))
} else {
dispatch(
updatePatient(
{
...patient,
fullName: getPatientName(patient.givenName, patient.familyName, patient.suffix),
},
onSuccessfulSave,
),
)
}
}
const onFieldChange = (key: string, value: string | boolean) => {
setPatient({
...patient,
[key]: value,
})
}
if (isLoading) {
return <Spinner color="blue" loading size={[10, 25]} type="ScaleLoader" />
}
return (
<div>
<GeneralInformation
isEditable
patient={patient}
onFieldChange={onFieldChange}
errorMessage={errorMessage}
/>
<div className="row float-right">
<div className="btn-group btn-group-lg">
<Button className="mr-2" color="success" onClick={onSave}>
{t('actions.save')}
</Button>
<Button color="danger" onClick={onCancel}>
{t('actions.cancel')}
</Button>
</div>
</div>
</div>
)
}
export default EditPatient