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
/
Allergies.tsx
88 lines (80 loc) · 2.63 KB
/
Allergies.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
import React, { useState } from 'react'
import useAddBreadcrumbs from 'breadcrumbs/useAddBreadcrumbs'
import Patient from 'model/Patient'
import { Button, List, ListItem, Alert, Toast } from '@hospitalrun/components'
import { useSelector, useDispatch } from 'react-redux'
import { RootState } from 'store'
import Permissions from 'model/Permissions'
import { useTranslation } from 'react-i18next'
import Allergy from 'model/Allergy'
import { updatePatient } from 'patients/patient-slice'
import { getTimestampId } from 'patients/util/timestamp-id-generator'
import NewAllergyModal from './NewAllergyModal'
interface AllergiesProps {
patient: Patient
}
const Allergies = (props: AllergiesProps) => {
const { t } = useTranslation()
const dispatch = useDispatch()
const { patient } = props
const { permissions } = useSelector((state: RootState) => state.user)
const [showNewAllergyModal, setShowNewAllergyModal] = useState(false)
const breadcrumbs = [
{
i18nKey: 'patient.allergies.label',
location: `/patients/${patient.id}/allergies`,
},
]
useAddBreadcrumbs(breadcrumbs)
const onAddAllergySuccess = () => {
Toast('success', t('states.success'), `${t('patient.allergies.successfullyAdded')}`)
}
const onAddAllergy = (allergy: Allergy) => {
allergy.id = getTimestampId()
const allergies = []
if (patient.allergies) {
allergies.push(...patient.allergies)
}
allergies.push(allergy)
const patientToUpdate = { ...patient, allergies }
dispatch(updatePatient(patientToUpdate, onAddAllergySuccess))
}
return (
<>
<div className="row">
<div className="col-md-12 d-flex justify-content-end">
{permissions.includes(Permissions.AddAllergy) && (
<Button
outlined
color="success"
icon="add"
iconLocation="left"
onClick={() => setShowNewAllergyModal(true)}
>
{t('patient.allergies.new')}
</Button>
)}
</div>
</div>
<br />
{(!patient.allergies || patient.allergies.length === 0) && (
<Alert
color="warning"
title={t('patient.allergies.warning.noAllergies')}
message={t('patient.allergies.addAllergyAbove')}
/>
)}
<List>
{patient.allergies?.map((a: Allergy) => (
<ListItem key={a.id}>{a.name}</ListItem>
))}
</List>
<NewAllergyModal
show={showNewAllergyModal}
onCloseButtonClick={() => setShowNewAllergyModal(false)}
onSave={(allergy) => onAddAllergy(allergy)}
/>
</>
)
}
export default Allergies