@@ -15,9 +15,11 @@ import { useRouter } from 'next/router';
15
15
import FormDiv from 'components/FormDiv' ;
16
16
import styled from 'styled-components' ;
17
17
import { useEffect , useState } from 'react' ;
18
- import { useCreateNewFormDataMutation } from 'schema/mutations/application/createNewFormData' ;
19
18
import useEmailNotification from 'lib/helpers/useEmailNotification' ;
20
19
import useRfiCoverageMapKmzUploadedEmail from 'lib/helpers/useRfiCoverageMapKmzUploadedEmail' ;
20
+ import { useUpdateRfiAndCreateTemplateNineDataMutation } from 'schema/mutations/application/updateRfiAndCreateTemplateNineDataMutation' ;
21
+ import { useUpdateRfiAndFormDataMutation } from 'schema/mutations/application/updateRfiAndFormDataMutation' ;
22
+ import { useUpdateFormRfiAndCreateTemplateNineDataMutation } from 'schema/mutations/application/updateFormRfiAndCreateTemplateNineDataMutation' ;
21
23
22
24
const Flex = styled ( 'header' ) `
23
25
display: flex;
@@ -64,14 +66,26 @@ const ApplicantRfiPage = ({
64
66
const { session, rfiDataByRowId, applicationByRowId } = query ;
65
67
const { rfiNumber } = rfiDataByRowId ;
66
68
const [ updateRfi ] = useUpdateWithTrackingRfiMutation ( ) ;
69
+ const [ updateRfiAndCreateTemplateNineData ] =
70
+ useUpdateRfiAndCreateTemplateNineDataMutation ( ) ;
71
+ const [ updateFormRfiAndCreateTemplateNineData ] =
72
+ useUpdateFormRfiAndCreateTemplateNineDataMutation ( ) ;
73
+ const [ updateRfiAndFormData ] = useUpdateRfiAndFormDataMutation ( ) ;
67
74
const router = useRouter ( ) ;
68
75
const formJsonData = applicationByRowId ?. formData ?. jsonData ;
69
76
const applicationId = router . query . id as string ;
70
77
const formSchemaId = applicationByRowId ?. formData ?. formSchemaId ;
71
78
const ccbcNumber = applicationByRowId ?. ccbcNumber ;
72
79
const [ newFormData , setNewFormData ] = useState ( formJsonData ) ;
73
- const [ createNewFormData ] = useCreateNewFormDataMutation ( ) ;
80
+ const [ hasApplicationFormDataUpdated , setHasApplicationFormDataUpdated ] =
81
+ useState ( false ) ;
82
+ const [ templateNineData , setTemplateNineData ] = useState ( null ) ;
74
83
const [ templateData , setTemplateData ] = useState ( null ) ;
84
+ const [ templatesUpdated , setTemplatesUpdated ] = useState ( {
85
+ one : false ,
86
+ two : false ,
87
+ nine : false ,
88
+ } ) ;
75
89
const [ formData , setFormData ] = useState ( rfiDataByRowId . jsonData ) ;
76
90
const { notifyHHCountUpdate } = useEmailNotification ( ) ;
77
91
const { notifyRfiCoverageMapKmzUploaded } =
@@ -89,6 +103,10 @@ const ApplicantRfiPage = ({
89
103
} ,
90
104
} ;
91
105
setNewFormData ( newFormDataWithTemplateOne ) ;
106
+ setTemplatesUpdated ( ( prevTemplatesUpdated ) => {
107
+ return { ...prevTemplatesUpdated , one : true } ;
108
+ } ) ;
109
+ setHasApplicationFormDataUpdated ( true ) ;
92
110
} else if ( templateData ?. templateNumber === 2 && ! templateData . error ) {
93
111
const newFormDataWithTemplateTwo = {
94
112
...newFormData ,
@@ -99,7 +117,21 @@ const ApplicantRfiPage = ({
99
117
} ,
100
118
} ;
101
119
setNewFormData ( newFormDataWithTemplateTwo ) ;
102
- } else if ( templateData ?. error && templateData ?. templateNumber === 1 ) {
120
+ setTemplatesUpdated ( ( prevTemplatesUpdated ) => {
121
+ return { ...prevTemplatesUpdated , two : true } ;
122
+ } ) ;
123
+ setHasApplicationFormDataUpdated ( true ) ;
124
+ } else if ( templateData ?. templateNumber === 9 && ! templateData . error ) {
125
+ setTemplatesUpdated ( ( prevTemplatesUpdated ) => {
126
+ return { ...prevTemplatesUpdated , nine : true } ;
127
+ } ) ;
128
+ setTemplateNineData ( { ...templateData } ) ;
129
+ } else if (
130
+ templateData ?. error &&
131
+ ( templateData ?. templateNumber === 1 ||
132
+ templateData ?. templateNumber === 2 ||
133
+ templateData ?. templateNumber === 9 )
134
+ ) {
103
135
const fileArrayLength =
104
136
newFormData . templateUploads ?. eligibilityAndImpactsCalculator ?. length ;
105
137
fetch ( `/api/email/notifyFailedReadOfTemplateData` , {
@@ -119,89 +151,170 @@ const ApplicantRfiPage = ({
119
151
} ,
120
152
} ) ,
121
153
} ) ;
122
- } else if ( templateData ?. error && templateData ?. templateNumber === 2 ) {
123
- const fileArrayLength =
124
- newFormData . templateUploads ?. detailedBudget ?. length ;
125
- fetch ( `/api/email/notifyFailedReadOfTemplateData` , {
126
- method : 'POST' ,
127
- headers : { 'Content-Type' : 'application/json' } ,
128
- body : JSON . stringify ( {
129
- applicationId,
130
- host : window . location . origin ,
131
- params : {
132
- templateNumber : templateData . templateNumber ,
133
- uuid : newFormData . templateUploads ?. detailedBudget ?. [
134
- fileArrayLength - 1
135
- ] ?. uuid ,
136
- uploadedAt :
137
- newFormData . templateUploads ?. detailedBudget ?. [ fileArrayLength - 1 ]
138
- ?. uploadedAt ,
139
- } ,
140
- } ) ,
141
- } ) ;
142
154
}
143
155
// eslint-disable-next-line react-hooks/exhaustive-deps
144
156
} , [ templateData ] ) ;
145
157
146
158
const handleSubmit = ( e : IChangeEvent < any > ) => {
147
- updateRfi ( {
148
- variables : {
149
- input : {
150
- jsonData : e . formData ,
151
- rfiRowId : rfiDataByRowId . rowId ,
152
- } ,
153
- } ,
154
- onCompleted : ( ) => {
155
- if ( e . formData ?. rfiAdditionalFiles ?. geographicCoverageMap ?. length > 0 ) {
156
- notifyRfiCoverageMapKmzUploaded (
157
- rfiDataByRowId ,
158
- e . formData ,
159
- applicationId ,
159
+ const getTemplateNineUUID = ( ) => {
160
+ // can be wrong source if there are multiple uploads
161
+ return e . formData ?. rfiAdditionalFiles ?. geographicNames ?. [ 0 ] ?. uuid ;
162
+ } ;
163
+
164
+ const checkAndNotifyRfiCoverage = async ( ) => {
165
+ if ( e . formData ?. rfiAdditionalFiles ?. geographicCoverageMap ?. length > 0 ) {
166
+ return notifyRfiCoverageMapKmzUploaded (
167
+ rfiDataByRowId ,
168
+ e . formData ,
169
+ applicationId ,
170
+ ccbcNumber ,
171
+ rfiNumber ,
172
+ applicationByRowId . organizationName
173
+ ) ;
174
+ }
175
+ return Promise . resolve ( ) ;
176
+ } ;
177
+
178
+ const checkAndNotifyHHCount = async ( ) => {
179
+ if ( templatesUpdated ?. one ) {
180
+ return notifyHHCountUpdate (
181
+ newFormData . benefits ,
182
+ formJsonData . benefits ,
183
+ applicationId ,
184
+ {
160
185
ccbcNumber,
186
+ timestamp : new Date ( ) . toLocaleString ( ) ,
187
+ manualUpdate : false ,
161
188
rfiNumber,
162
- applicationByRowId . organizationName
163
- ) ;
164
- }
165
- if ( ! templateData ) {
166
- router . push ( `/applicantportal/dashboard` ) ;
167
- }
168
- } ,
169
- onError : ( err ) => {
170
- // eslint-disable-next-line no-console
171
- console . log ( 'Error updating RFI' , err ) ;
172
- } ,
173
- } ) ;
174
- if ( templateData ) {
175
- createNewFormData ( {
189
+ organizationName : applicationByRowId . organizationName ,
190
+ }
191
+ ) ;
192
+ }
193
+ return Promise . resolve ( ) ;
194
+ } ;
195
+
196
+ if ( ! hasApplicationFormDataUpdated && ! templatesUpdated . nine ) {
197
+ // form data not updated and template nine not updated
198
+ // only update rfi
199
+ updateRfi ( {
176
200
variables : {
177
201
input : {
202
+ jsonData : e . formData ,
203
+ rfiRowId : rfiDataByRowId . rowId ,
204
+ } ,
205
+ } ,
206
+ onCompleted : ( ) => {
207
+ checkAndNotifyRfiCoverage ( ) . then ( ( ) => {
208
+ // wait until email is sent before redirecting
209
+ router . push ( `/applicantportal/dashboard` ) ;
210
+ } ) ;
211
+ } ,
212
+ onError : ( err ) => {
213
+ // eslint-disable-next-line no-console
214
+ console . log ( 'Error updating RFI' , err ) ;
215
+ } ,
216
+ } ) ;
217
+ } else if ( ! hasApplicationFormDataUpdated && templatesUpdated . nine ) {
218
+ // form data not updated but template nine updated, update rfi and create template nine record
219
+ updateRfiAndCreateTemplateNineData ( {
220
+ variables : {
221
+ rfiInput : {
222
+ jsonData : e . formData ,
223
+ rfiRowId : rfiDataByRowId . rowId ,
224
+ } ,
225
+ templateNineInput : {
226
+ applicationFormTemplate9Data : {
227
+ applicationId : Number ( applicationId ) ,
228
+ jsonData : templateNineData . data ,
229
+ source : {
230
+ source : 'RFI' ,
231
+ uuid : getTemplateNineUUID ( ) ,
232
+ } ,
233
+ } ,
234
+ } ,
235
+ } ,
236
+ onError : ( err ) => {
237
+ // eslint-disable-next-line no-console
238
+ console . log (
239
+ 'Error updating RFI and creating template nine data' ,
240
+ err
241
+ ) ;
242
+ } ,
243
+ onCompleted : ( ) => {
244
+ checkAndNotifyRfiCoverage ( ) . then ( ( ) => {
245
+ // wait until email(s) is sent before redirecting
246
+ router . push ( `/applicantportal/dashboard` ) ;
247
+ } ) ;
248
+ } ,
249
+ } ) ;
250
+ } else if ( hasApplicationFormDataUpdated && ! templatesUpdated . nine ) {
251
+ // only update rfi and form data since no template nine data
252
+ updateRfiAndFormData ( {
253
+ variables : {
254
+ formInput : {
178
255
applicationRowId : Number ( applicationId ) ,
179
256
jsonData : newFormData ,
180
257
reasonForChange : `Auto updated from upload for RFI: ${ rfiNumber } ` ,
181
258
formSchemaId,
182
259
} ,
260
+ rfiInput : {
261
+ jsonData : e . formData ,
262
+ rfiRowId : rfiDataByRowId . rowId ,
263
+ } ,
183
264
} ,
184
265
onError : ( err ) => {
185
266
// eslint-disable-next-line no-console
186
267
console . log ( 'Error creating new form data' , err ) ;
187
268
} ,
188
269
onCompleted : ( ) => {
189
- if ( templateData ?. templateNumber === 1 ) {
190
- notifyHHCountUpdate (
191
- newFormData . benefits ,
192
- formJsonData . benefits ,
193
- applicationId ,
194
- {
195
- ccbcNumber,
196
- timestamp : new Date ( ) . toLocaleString ( ) ,
197
- manualUpdate : false ,
198
- rfiNumber,
199
- organizationName : applicationByRowId . organizationName ,
200
- }
201
- ) ;
202
- }
203
270
setTemplateData ( null ) ;
204
- router . push ( `/applicantportal/dashboard` ) ;
271
+ checkAndNotifyRfiCoverage ( ) . then ( ( ) => {
272
+ checkAndNotifyHHCount ( ) . then ( ( ) => {
273
+ // wait until email is sent before redirecting
274
+ router . push ( `/applicantportal/dashboard` ) ;
275
+ } ) ;
276
+ } ) ;
277
+ } ,
278
+ } ) ;
279
+ } else if ( hasApplicationFormDataUpdated && templatesUpdated . nine ) {
280
+ // update rfi, form data, and template nine data (all three)
281
+ updateFormRfiAndCreateTemplateNineData ( {
282
+ variables : {
283
+ formInput : {
284
+ applicationRowId : Number ( applicationId ) ,
285
+ jsonData : newFormData ,
286
+ reasonForChange : `Auto updated from upload for RFI: ${ rfiNumber } ` ,
287
+ formSchemaId,
288
+ } ,
289
+ rfiInput : {
290
+ jsonData : e . formData ,
291
+ rfiRowId : rfiDataByRowId . rowId ,
292
+ } ,
293
+ templateNineInput : {
294
+ applicationFormTemplate9Data : {
295
+ applicationId : Number ( applicationId ) ,
296
+ jsonData : templateNineData . data ,
297
+ source : {
298
+ source : 'RFI' ,
299
+ uuid : getTemplateNineUUID ( ) ,
300
+ } ,
301
+ } ,
302
+ } ,
303
+ } ,
304
+ onError : ( err ) => {
305
+ // eslint-disable-next-line no-console
306
+ console . log (
307
+ 'Error updating RFI, form data, and template nine data' ,
308
+ err
309
+ ) ;
310
+ } ,
311
+ onCompleted : ( ) => {
312
+ checkAndNotifyHHCount ( ) . then ( ( ) => {
313
+ checkAndNotifyRfiCoverage ( ) . then ( ( ) => {
314
+ // wait until email(s) is sent before redirecting
315
+ router . push ( `/applicantportal/dashboard` ) ;
316
+ } ) ;
317
+ } ) ;
205
318
} ,
206
319
} ) ;
207
320
}
0 commit comments