@@ -20,8 +20,7 @@ import FormLabel from "@material-ui/core/FormLabel"
20
20
import Button from "@material-ui/core/Button"
21
21
import { observer } from "mobx-react-lite"
22
22
import { useHistory } from "react-router-dom"
23
- import { format } from "date-fns"
24
- import { autorun } from "mobx"
23
+ import { autorun , toJS } from "mobx"
25
24
26
25
const useStyles = makeStyles ( theme => ( {
27
26
paper : {
@@ -76,44 +75,18 @@ const ParticipantInfo = observer(() => {
76
75
const rootStore = useContext ( rootStoreContext )
77
76
// particiant store derived from root store
78
77
const participantStore = rootStore . ParticipantStore
79
- // init participant state
80
- const [ participant , setParticipant ] = React . useState ( {
81
- id : null ,
82
- firstName : "" ,
83
- lastName : "" ,
84
- lastFourSSN : 0 ,
85
- dateOfBirth : "" ,
86
- startDate : "" ,
87
- ppId : "" ,
88
- race : "" ,
89
- gender : "" ,
90
- hasInsurance : false ,
91
- insuranceType : "" ,
92
- insurer : "" ,
93
- } )
94
- // init visit state
95
- const [ visit , setVisit ] = React . useState ( {
96
- id : null ,
97
- participant : null ,
98
- program : "" ,
99
- service : "" ,
100
- notes : "" ,
101
- urgency : "" ,
102
- } )
103
- // list of all insurerers
104
78
const [ insurers , setInsurers ] = React . useState ( [ ] )
105
79
// list of all programs
106
80
const [ programList , setProgramList ] = React . useState ( [ ] )
107
81
// list of all services
108
82
const [ serviceList , setServiceList ] = React . useState ( [ ] )
109
- // ???
110
83
const [ open , setOpen ] = React . useState ( "" )
111
84
// set up history for routing pushes
112
85
const history = useHistory ( )
113
86
// get existing participant if applicable else its undefined
114
- const existingParticipant = participantStore . getParticipant ( )
87
+ const existingParticipant = toJS ( participantStore . participant )
115
88
// get existing visit if applicable else its undefined
116
- const existingVisit = participantStore . getVisit ( )
89
+ const existingVisit = toJS ( participantStore . visit )
117
90
// useEffect is a hook that gets called after every render/re-render. Empty array second argument prevents it from running again.
118
91
useEffect ( ( ) => {
119
92
// self invoked async function making api calls for insurers and programs
@@ -126,47 +99,21 @@ const ParticipantInfo = observer(() => {
126
99
)
127
100
} ) ( )
128
101
// if existing participant exists then auto fill the fields
129
- if ( existingParticipant ) {
130
- setParticipant ( {
131
- id : existingParticipant . id ,
132
- firstName : existingParticipant . first_name ,
133
- lastName : existingParticipant . last_name ,
134
- lastFourSSN : existingParticipant . last_four_ssn ,
135
- dateOfBirth : existingParticipant . date_of_birth ,
136
- startDate : existingParticipant . start_date ,
137
- ppId : existingParticipant . pp_id ,
138
- race : existingParticipant . race ,
139
- gender : existingParticipant . gender ,
140
- hasInsurance : existingParticipant . is_insured ,
141
- insuranceType : existingParticipant . insuranceType
142
- ? existingParticipant . insuranceType
143
- : "" ,
144
- insurer : existingParticipant . insurer ,
145
- } )
146
- if ( existingVisit ) {
147
- setVisit ( {
148
- id : existingVisit . id ,
149
- participant : existingVisit . participant ,
150
- program : existingVisit . program ,
151
- service : existingVisit . service ,
152
- notes : existingVisit . notes ,
153
- urgency : existingVisit . urgency ,
154
- } )
155
- // preload services
156
- if ( existingVisit . program && existingVisit . service ) {
157
- let serviceList = participantStore
158
- . getProgramList ( )
159
- . find ( val => val . id === existingVisit . program )
160
- setServiceList ( serviceList . services )
161
- }
162
- }
102
+ if (
103
+ existingParticipant . id &&
104
+ existingVisit . id &&
105
+ existingVisit . program &&
106
+ existingVisit . service
107
+ ) {
108
+ // preload services
109
+ let serviceList = participantStore
110
+ . getProgramList ( )
111
+ . find ( val => val . id === existingVisit . program )
112
+ setServiceList ( serviceList . services )
163
113
}
164
114
// eslint-disable-next-line react-hooks/exhaustive-deps
165
115
} , [ ] )
166
116
167
- const createStartDate = ( ) => {
168
- return format ( new Date ( ) , "yyyy-MM-dd" )
169
- }
170
117
// set store stuff here and update Mobx on submit
171
118
const handleClose = ( ) => {
172
119
setOpen ( false )
@@ -176,36 +123,12 @@ const ParticipantInfo = observer(() => {
176
123
}
177
124
const handleSubmit = e => {
178
125
e . preventDefault ( )
179
- participantStore . setParticipant ( {
180
- id : existingParticipant ? participant . id : null ,
181
- first_name : participant . firstName ,
182
- last_name : participant . lastName ,
183
- last_four_ssn : participant . lastFourSSN ,
184
- date_of_birth : participant . dateOfBirth ,
185
- start_date : existingParticipant
186
- ? participant . startDate
187
- : createStartDate ( ) ,
188
- pp_id : participant . ppId ,
189
- race : participant . race ,
190
- gender : participant . gender ,
191
- is_insured : participant . hasInsurance ,
192
- insuranceType : participant . insuranceType ,
193
- insurer : participant . insurer ,
194
- } )
195
- // if we have a participant and navigated from queuetable or is a new participant set visit
196
- if (
197
- ( ! existingParticipant && ! existingVisit ) ||
198
- ( existingParticipant && existingVisit )
199
- ) {
200
- // set visit in Mobx
201
- participantStore . setVisit ( visit )
202
- }
203
126
// if existing participant and vist we are coming from QueueTable, so update particiapnt and visit
204
- if ( existingParticipant && existingVisit ) {
127
+ if ( existingParticipant . id && existingVisit . id ) {
205
128
participantStore . updateParticipant ( )
206
129
participantStore . updateVisit ( )
207
130
// if existing participant and no vist we are coming from search, so update particiapnt only
208
- } else if ( existingParticipant && ! existingVisit ) {
131
+ } else if ( existingParticipant . id && ! existingVisit . id ) {
209
132
participantStore . updateParticipant ( )
210
133
// otherwise we are adding a new participant because both participant and visit will be undefined
211
134
} else {
@@ -250,12 +173,9 @@ const ParticipantInfo = observer(() => {
250
173
< Input
251
174
id = "user_first-name"
252
175
name = "user_first-name"
253
- value = { participant . firstName }
176
+ value = { existingParticipant . first_name }
254
177
onChange = { e =>
255
- setParticipant ( {
256
- ...participant ,
257
- firstName : e . target . value ,
258
- } )
178
+ participantStore . setFirstName ( e . target . value )
259
179
}
260
180
required
261
181
/>
@@ -267,12 +187,9 @@ const ParticipantInfo = observer(() => {
267
187
< Input
268
188
id = "user_last-name"
269
189
name = "user_last-name"
270
- value = { participant . lastName }
190
+ value = { existingParticipant . last_name }
271
191
onChange = { e =>
272
- setParticipant ( {
273
- ...participant ,
274
- lastName : e . target . value ,
275
- } )
192
+ participantStore . setLastName ( e . target . value )
276
193
}
277
194
required
278
195
/>
@@ -288,12 +205,9 @@ const ParticipantInfo = observer(() => {
288
205
< TextField
289
206
id = "user_birth-date"
290
207
name = "user_birth-date"
291
- value = { participant . dateOfBirth }
208
+ value = { existingParticipant . date_of_birth }
292
209
onChange = { e =>
293
- setParticipant ( {
294
- ...participant ,
295
- dateOfBirth : e . target . value ,
296
- } )
210
+ participantStore . setDateOfBirth ( e . target . value )
297
211
}
298
212
required
299
213
style = { { marginTop : 40 } }
@@ -311,14 +225,13 @@ const ParticipantInfo = observer(() => {
311
225
< Input
312
226
id = "uuid"
313
227
name = "uuid"
314
- value = { participant . ppId }
315
- onChange = { e =>
316
- setParticipant ( {
317
- ...participant ,
318
- ppId : e . target . value ,
319
- lastFourSSN : + e . target . value . substr ( 2 ) ,
320
- } )
321
- }
228
+ value = { existingParticipant . pp_id }
229
+ onChange = { e => {
230
+ participantStore . setPPId ( e . target . value )
231
+ participantStore . setLastFourSSN (
232
+ + e . target . value . substr ( 2 )
233
+ )
234
+ } }
322
235
required
323
236
/>
324
237
</ FormControl >
@@ -339,13 +252,8 @@ const ParticipantInfo = observer(() => {
339
252
onClose = { handleClose . race }
340
253
onOpen = { handleOpen . race }
341
254
required
342
- value = { participant . race }
343
- onChange = { e =>
344
- setParticipant ( {
345
- ...participant ,
346
- race : e . target . value ,
347
- } )
348
- }
255
+ value = { existingParticipant . race }
256
+ onChange = { e => participantStore . setRace ( e . target . value ) }
349
257
inputProps = { {
350
258
name : "race" ,
351
259
id : "demo-controlled-open-select" ,
@@ -380,12 +288,9 @@ const ParticipantInfo = observer(() => {
380
288
onClose = { handleClose . gender }
381
289
onOpen = { handleOpen . gender }
382
290
required
383
- value = { participant . gender }
291
+ value = { existingParticipant . gender }
384
292
onChange = { e =>
385
- setParticipant ( {
386
- ...participant ,
387
- gender : e . target . value ,
388
- } )
293
+ participantStore . setGender ( e . target . value )
389
294
}
390
295
inputProps = { {
391
296
name : "gender" ,
@@ -419,13 +324,13 @@ const ParticipantInfo = observer(() => {
419
324
aria-label = "insurance"
420
325
name = "hasInsurance"
421
326
className = { classes . group }
422
- value = { participant . hasInsurance === true ? "yes" : "no" }
327
+ value = {
328
+ existingParticipant . is_insured === true ? "yes" : "no"
329
+ }
423
330
onChange = { e =>
424
- setParticipant ( {
425
- ...participant ,
426
- hasInsurance :
427
- e . target . value === "yes" ? true : false ,
428
- } )
331
+ participantStore . setIsInsured (
332
+ e . target . value === "yes" ? true : false
333
+ )
429
334
}
430
335
style = { { display : "inline" } }
431
336
>
@@ -452,12 +357,9 @@ const ParticipantInfo = observer(() => {
452
357
open = { open . insuranceType }
453
358
onClose = { handleClose . insuranceType }
454
359
onOpen = { handleOpen . insuranceType }
455
- value = { participant . insurer }
360
+ value = { existingParticipant . insurer }
456
361
onChange = { e =>
457
- setParticipant ( {
458
- ...participant ,
459
- insurer : e . target . value ,
460
- } )
362
+ participantStore . setInsurer ( e . target . value )
461
363
}
462
364
inputProps = { {
463
365
name : "insuranceType" ,
@@ -484,8 +386,8 @@ const ParticipantInfo = observer(() => {
484
386
</ div >
485
387
</ Grid >
486
388
487
- { ( ! existingParticipant && ! existingVisit ) ||
488
- ( existingParticipant && existingVisit ) ? (
389
+ { ( ! existingParticipant . id && ! existingVisit . id ) ||
390
+ ( existingParticipant . id && existingVisit . id ) ? (
489
391
< div >
490
392
< Typography
491
393
style = { { textAlign : "left" } }
@@ -510,12 +412,9 @@ const ParticipantInfo = observer(() => {
510
412
onClose = { handleClose . program }
511
413
onOpen = { handleOpen . program }
512
414
required
513
- value = { visit . program }
415
+ value = { existingVisit . program }
514
416
onChange = { e => {
515
- setVisit ( {
516
- ...visit ,
517
- program : e . target . value ,
518
- } )
417
+ participantStore . setVisitProgram ( e . target . value )
519
418
findAndSaveServiceListings ( e )
520
419
} }
521
420
inputProps = { {
@@ -546,18 +445,15 @@ const ParticipantInfo = observer(() => {
546
445
< InputLabel htmlFor = "demo-controlled-open-select" >
547
446
Select Service
548
447
</ InputLabel >
549
- { visit . program && serviceList . length > 0 ? (
448
+ { existingVisit . program && serviceList . length > 0 ? (
550
449
< Select
551
450
open = { open . service }
552
451
onClose = { handleClose . service }
553
452
onOpen = { handleOpen . service }
554
453
required
555
- value = { visit . service }
454
+ value = { existingVisit . service }
556
455
onChange = { e =>
557
- setVisit ( {
558
- ...visit ,
559
- service : e . target . value ,
560
- } )
456
+ participantStore . setVisitService ( e . target . value )
561
457
}
562
458
inputProps = { {
563
459
name : "service" ,
@@ -592,12 +488,9 @@ const ParticipantInfo = observer(() => {
592
488
open = { open . priorityLevel }
593
489
onClose = { handleClose . priorityLevel }
594
490
onOpen = { handleOpen . priorityLevel }
595
- value = { visit . urgency }
491
+ value = { existingVisit . urgency }
596
492
onChange = { e =>
597
- setVisit ( {
598
- ...visit ,
599
- urgency : e . target . value ,
600
- } )
493
+ participantStore . setVisitUrgency ( e . target . value )
601
494
}
602
495
inputProps = { {
603
496
name : "priorityLevel" ,
@@ -618,12 +511,9 @@ const ParticipantInfo = observer(() => {
618
511
style = { { margin : 8 , marginTop : 40 } }
619
512
placeholder = "Add a note"
620
513
onChange = { e =>
621
- setVisit ( {
622
- ...visit ,
623
- notes : e . target . value ,
624
- } )
514
+ participantStore . setVisitNotes ( e . target . value )
625
515
}
626
- value = { visit . notes }
516
+ value = { existingVisit . notes }
627
517
fullWidth
628
518
margin = "normal"
629
519
InputLabelProps = { {
0 commit comments