1
1
import React , { useState } from 'react'
2
2
import { useTranslation } from 'react-i18next'
3
- import { Button , Checkbox } from '@hospitalrun/components'
3
+ import { Alert , Button , Checkbox } from '@hospitalrun/components'
4
4
import { startOfDay , subYears } from 'date-fns'
5
5
import SelectWithLabelFormGroup from '../../components/input/SelectWithLableFormGroup'
6
6
import TextFieldWithLabelFormGroup from '../../components/input/TextFieldWithLabelFormGroup'
@@ -19,7 +19,7 @@ const NewPatientForm = (props: Props) => {
19
19
const [ isEditable ] = useState ( true )
20
20
const { onCancel, onSave } = props
21
21
const [ approximateAge , setApproximateAge ] = useState ( 0 )
22
- const [ errorMessage , setError ] = useState ( '' )
22
+ const [ errorMessage , setErrorMessage ] = useState ( '' )
23
23
const [ patient , setPatient ] = useState ( {
24
24
givenName : '' ,
25
25
familyName : '' ,
@@ -38,27 +38,28 @@ const NewPatientForm = (props: Props) => {
38
38
} )
39
39
40
40
const onSaveButtonClick = async ( ) => {
41
- if ( ! patient . givenName && ! patient . familyName ) {
42
- return setError ( "No patient name entered!" )
43
- }
44
- const newPatient = {
45
- prefix : patient . prefix ,
46
- familyName : patient . familyName ,
47
- givenName : patient . givenName ,
48
- suffix : patient . suffix ,
49
- sex : patient . sex ,
50
- dateOfBirth : patient . dateOfBirth ,
51
- isApproximateDateOfBirth : patient . isApproximateDateOfBirth ,
52
- type : patient . type ,
53
- occupation : patient . occupation ,
54
- preferredLanguage : patient . preferredLanguage ,
55
- phoneNumber : patient . phoneNumber ,
56
- email : patient . email ,
57
- address : patient . address ,
58
- fullName : getPatientName ( patient . givenName , patient . familyName , patient . suffix ) ,
59
- } as Patient
41
+ if ( ! patient . givenName ) {
42
+ setErrorMessage ( t ( 'errors.patientNameRequired' ) )
43
+ } else {
44
+ const newPatient = {
45
+ prefix : patient . prefix ,
46
+ familyName : patient . familyName ,
47
+ givenName : patient . givenName ,
48
+ suffix : patient . suffix ,
49
+ sex : patient . sex ,
50
+ dateOfBirth : patient . dateOfBirth ,
51
+ isApproximateDateOfBirth : patient . isApproximateDateOfBirth ,
52
+ type : patient . type ,
53
+ occupation : patient . occupation ,
54
+ preferredLanguage : patient . preferredLanguage ,
55
+ phoneNumber : patient . phoneNumber ,
56
+ email : patient . email ,
57
+ address : patient . address ,
58
+ fullName : getPatientName ( patient . givenName , patient . familyName , patient . suffix ) ,
59
+ } as Patient
60
60
61
- onSave ( newPatient )
61
+ onSave ( newPatient )
62
+ }
62
63
}
63
64
64
65
const onFieldChange = ( key : string , value : string ) => {
@@ -97,6 +98,7 @@ const NewPatientForm = (props: Props) => {
97
98
< div >
98
99
< form >
99
100
< h3 > { t ( 'patient.basicInformation' ) } </ h3 >
101
+ { errorMessage && < Alert className = "alert" color = "danger" message = { t ( errorMessage ) } /> }
100
102
< div className = "row" >
101
103
< div className = "col-md-2" >
102
104
< TextInputWithLabelFormGroup
@@ -278,9 +280,6 @@ const NewPatientForm = (props: Props) => {
278
280
/>
279
281
</ div >
280
282
</ div >
281
- { errorMessage && (
282
- < div className = "alert alert-danger" role = "alert" > { t ( errorMessage ) } </ div >
283
- ) }
284
283
{ isEditable && (
285
284
< div className = "row" >
286
285
< Button onClick = { onSaveButtonClick } > { t ( 'actions.save' ) } </ Button >
0 commit comments