@@ -2,10 +2,19 @@ import React, { useCallback, useContext, useEffect, useMemo, useState } from 're
2
2
import { ComposedModal , Button , ModalHeader , ModalFooter , ModalBody , TextInput , FormLabel } from '@carbon/react' ;
3
3
import { TrashCan } from '@carbon/react/icons' ;
4
4
import { useTranslation } from 'react-i18next' ;
5
- import { ExtensionSlot , fetchCurrentPatient , showToast , useConfig , usePatient } from '@openmrs/esm-framework' ;
5
+ import {
6
+ ExtensionSlot ,
7
+ fetchCurrentPatient ,
8
+ showToast ,
9
+ useConfig ,
10
+ usePatient ,
11
+ useSession ,
12
+ } from '@openmrs/esm-framework' ;
6
13
import styles from './styles.scss' ;
7
14
import GroupFormWorkflowContext from '../context/GroupFormWorkflowContext' ;
8
15
import { usePostCohort } from '../hooks' ;
16
+ import PatientLocationMismatchModal from '../form-entry-workflow/patient-search-header/PatienMismatchedLocationModal' ;
17
+ import { useHsuIdIdentifier } from '../hooks/location-tag.resource' ;
9
18
10
19
const MemExtension = React . memo ( ExtensionSlot ) ;
11
20
@@ -112,6 +121,10 @@ const AddGroupModal = ({
112
121
const [ patientList , setPatientList ] = useState ( patients || [ ] ) ;
113
122
const { post, result, error } = usePostCohort ( ) ;
114
123
const config = useConfig ( ) ;
124
+ const [ patientLocationMismatchModalOpen , setPatientLocationMismatchModalOpen ] = useState ( false ) ;
125
+ const [ selectedPatientUuid , setSelectedPatientUuid ] = useState ( ) ;
126
+ const { hsuIdentifier } = useHsuIdIdentifier ( selectedPatientUuid ) ;
127
+ const { sessionLocation } = useSession ( ) ;
115
128
116
129
const removePatient = useCallback (
117
130
( patientUuid : string ) =>
@@ -147,27 +160,44 @@ const AddGroupModal = ({
147
160
[ name , patientList . length ] ,
148
161
) ;
149
162
150
- const updatePatientList = useCallback (
151
- ( patientUuid ) => {
152
- function getPatientName ( patient ) {
153
- return [ patient ?. name ?. [ 0 ] ?. given , patient ?. name ?. [ 0 ] ?. family ] . join ( ' ' ) ;
154
- }
155
- if ( ! patientList . find ( ( p ) => p . uuid === patientUuid ) ) {
156
- fetchCurrentPatient ( patientUuid ) . then ( ( result ) => {
157
- const newPatient = { uuid : patientUuid , ...result } ;
158
- setPatientList (
159
- [ ...patientList , newPatient ] . sort ( ( a , b ) =>
160
- getPatientName ( a ) . localeCompare ( getPatientName ( b ) , undefined , {
161
- sensitivity : 'base' ,
162
- } ) ,
163
- ) ,
164
- ) ;
165
- } ) ;
166
- }
167
- setErrors ( ( errors ) => ( { ...errors , patientList : null } ) ) ;
168
- } ,
169
- [ patientList , setPatientList ] ,
170
- ) ;
163
+ const addSelectedPatientToList = useCallback ( ( ) => {
164
+ function getPatientName ( patient ) {
165
+ return [ patient ?. name ?. [ 0 ] ?. given , patient ?. name ?. [ 0 ] ?. family ] . join ( ' ' ) ;
166
+ }
167
+ if ( ! patientList . find ( ( p ) => p . uuid === selectedPatientUuid ) ) {
168
+ fetchCurrentPatient ( selectedPatientUuid ) . then ( ( result ) => {
169
+ const newPatient = { uuid : selectedPatientUuid , ...result } ;
170
+ setPatientList (
171
+ [ ...patientList , newPatient ] . sort ( ( a , b ) =>
172
+ getPatientName ( a ) . localeCompare ( getPatientName ( b ) , undefined , {
173
+ sensitivity : 'base' ,
174
+ } ) ,
175
+ ) ,
176
+ ) ;
177
+ } ) ;
178
+ }
179
+ setErrors ( ( errors ) => ( { ...errors , patientList : null } ) ) ;
180
+ } , [ selectedPatientUuid , patientList , setPatientList ] ) ;
181
+
182
+ const updatePatientList = ( patientUuid ) => {
183
+ setSelectedPatientUuid ( patientUuid ) ;
184
+ } ;
185
+
186
+ useEffect ( ( ) => {
187
+ if ( ! selectedPatientUuid || ! hsuIdentifier ) return ;
188
+
189
+ if ( config . patientLocationMismatchCheck && hsuIdentifier && sessionLocation . uuid != hsuIdentifier . location . uuid ) {
190
+ setPatientLocationMismatchModalOpen ( true ) ;
191
+ } else {
192
+ addSelectedPatientToList ( ) ;
193
+ }
194
+ } , [
195
+ selectedPatientUuid ,
196
+ sessionLocation ,
197
+ hsuIdentifier ,
198
+ addSelectedPatientToList ,
199
+ config . patientLocationMismatchCheck ,
200
+ ] ) ;
171
201
172
202
const handleSubmit = ( ) => {
173
203
if ( validate ( ) ) {
@@ -216,33 +246,47 @@ const AddGroupModal = ({
216
246
}
217
247
} , [ error , t ] ) ;
218
248
249
+ const onPatientLocationMismatchModalCancel = ( ) => {
250
+ setSelectedPatientUuid ( null ) ;
251
+ } ;
252
+
219
253
return (
220
- < div className = { styles . modal } >
221
- < ComposedModal open = { isOpen } onClose = { handleCancel } >
222
- < ModalHeader > { isCreate ? t ( 'createNewGroup' , 'Create New Group' ) : t ( 'editGroup' , 'Edit Group' ) } </ ModalHeader >
223
- < ModalBody >
224
- < NewGroupForm
225
- { ...{
226
- name,
227
- setName,
228
- patientList,
229
- updatePatientList,
230
- errors,
231
- validate,
232
- removePatient,
233
- } }
234
- />
235
- </ ModalBody >
236
- < ModalFooter >
237
- < Button kind = "secondary" onClick = { handleCancel } >
238
- { t ( 'cancel' , 'Cancel' ) }
239
- </ Button >
240
- < Button kind = "primary" onClick = { handleSubmit } >
241
- { isCreate ? t ( 'createGroup' , 'Create Group' ) : t ( 'save' , 'Save' ) }
242
- </ Button >
243
- </ ModalFooter >
244
- </ ComposedModal >
245
- </ div >
254
+ < >
255
+ < div className = { styles . modal } >
256
+ < ComposedModal open = { isOpen } onClose = { handleCancel } >
257
+ < ModalHeader > { isCreate ? t ( 'createNewGroup' , 'Create New Group' ) : t ( 'editGroup' , 'Edit Group' ) } </ ModalHeader >
258
+ < ModalBody >
259
+ < NewGroupForm
260
+ { ...{
261
+ name,
262
+ setName,
263
+ patientList,
264
+ updatePatientList,
265
+ errors,
266
+ validate,
267
+ removePatient,
268
+ } }
269
+ />
270
+ </ ModalBody >
271
+ < ModalFooter >
272
+ < Button kind = "secondary" onClick = { handleCancel } >
273
+ { t ( 'cancel' , 'Cancel' ) }
274
+ </ Button >
275
+ < Button kind = "primary" onClick = { handleSubmit } >
276
+ { isCreate ? t ( 'createGroup' , 'Create Group' ) : t ( 'save' , 'Save' ) }
277
+ </ Button >
278
+ </ ModalFooter >
279
+ </ ComposedModal >
280
+ </ div >
281
+ < PatientLocationMismatchModal
282
+ open = { patientLocationMismatchModalOpen }
283
+ setOpen = { setPatientLocationMismatchModalOpen }
284
+ onConfirm = { addSelectedPatientToList }
285
+ onCancel = { onPatientLocationMismatchModalCancel }
286
+ sessionLocation = { sessionLocation }
287
+ hsuLocation = { hsuIdentifier ?. location }
288
+ />
289
+ </ >
246
290
) ;
247
291
} ;
248
292
0 commit comments