1
1
import React from 'react' ;
2
2
import styled from "@emotion/styled" ;
3
- import fastXml from 'fast-xml-parser' ;
3
+ import fastXml from "fast-xml-parser" ;
4
+ import pdfJS from "pdfjs-dist" ;
4
5
import { spacing , colors , fontSizes , radii } from "../constants" ;
5
6
import { ObservableCell , SessionStore } from "../components" ;
6
7
@@ -98,7 +99,7 @@ export class GenerateTable extends React.Component {
98
99
)
99
100
100
101
} )
101
-
102
+
102
103
} else {
103
104
header = < tr > </ tr > ;
104
105
tableRows = < tr > </ tr > ;
@@ -120,8 +121,11 @@ export default class FileUpload extends React.Component {
120
121
constructor ( props , context ) {
121
122
super ( props , context ) ;
122
123
124
+ pdfJS . GlobalWorkerOptions . workerSrc = 'https://cdn.jsdelivr.net/npm/[email protected] /build/pdf.worker.js' ;
125
+
123
126
this . handleUpload = this . handleUpload . bind ( this ) ;
124
- this . handleLoadTable = this . handleLoadTable . bind ( this ) ;
127
+ this . handleXMLFile = this . handleXMLFile . bind ( this ) ;
128
+ this . handlePDFFile = this . handlePDFFile . bind ( this ) ;
125
129
this . handleInputEarnings = this . handleInputEarnings . bind ( this ) ;
126
130
this . handleManualEarnings = this . handleManualEarnings . bind ( this ) ;
127
131
this . handleSave = this . handleSave . bind ( this ) ;
@@ -159,7 +163,7 @@ export default class FileUpload extends React.Component {
159
163
160
164
if ( ( this . state . userBirthDate ) && ( this . state . userRetireDate ) && ( ! this . state . manualTable . length ) ) {
161
165
var tempTable = [ ]
162
-
166
+
163
167
for ( var i = this . state . userBirthDate ; i <= this . state . userRetireDate ; i ++ ) {
164
168
var record = { }
165
169
record [ 'year' ] = i
@@ -181,14 +185,14 @@ export default class FileUpload extends React.Component {
181
185
earningsRecord : earningsValue
182
186
} )
183
187
}
184
-
188
+
185
189
if ( SessionStore . get ( 'tableArray' ) ) {
186
190
var tableArray = JSON . parse ( SessionStore . get ( 'tableArray' ) )
187
191
this . setState ( {
188
- manualTable : tableArray
192
+ manualTable : tableArray
189
193
} )
190
194
}
191
-
195
+
192
196
}
193
197
194
198
@@ -231,7 +235,9 @@ export default class FileUpload extends React.Component {
231
235
if ( element [ '@_startYear' ] === modifiedyear ) {
232
236
element [ 'osss:FicaEarnings' ] = input . target . value
233
237
return true
234
- }
238
+ } else {
239
+ return false
240
+ }
235
241
} )
236
242
237
243
if ( findValue ) {
@@ -248,7 +254,7 @@ export default class FileUpload extends React.Component {
248
254
}
249
255
250
256
//Parse XML file
251
- handleLoadTable ( reader ) {
257
+ handleXMLFile ( reader ) {
252
258
if ( fastXml . validate ( reader . target . result ) === true ) {
253
259
var parsedText = fastXml . parse ( reader . target . result , { ignoreAttributes : false } )
254
260
var earningsJSON = JSON . stringify ( parsedText )
@@ -259,24 +265,83 @@ export default class FileUpload extends React.Component {
259
265
}
260
266
}
261
267
268
+ //Parse PDF file
269
+ handlePDFFile ( reader ) {
270
+ //Returns first page of document
271
+ var combinedValues = [ ]
272
+ pdfJS . getDocument ( reader . target . result ) . promise
273
+ . then (
274
+ ssaDoc => ssaDoc . getPage ( 3 ) ) . then ( earningsPage => earningsPage . getTextContent ( ) )
275
+ . then ( ( doc ) => {
276
+ doc . items . forEach ( ( item ) => {
277
+
278
+ var filter = Number ( item . str . replace ( "," , "" ) . replace ( " " , "" ) )
279
+ if ( ! ( Number . isNaN ( filter ) ) ) {
280
+ combinedValues . push ( filter )
281
+ }
282
+ } )
283
+ } )
284
+ . then ( ( ) => {
285
+
286
+ var tempRecord = this . state . defaultRecord
287
+ do {
288
+ var newvalue = combinedValues . shift ( )
289
+ if ( newvalue > 1900 ) {
290
+ var currentRecord = tempRecord [ 'osss:OnlineSocialSecurityStatementData' ] [ 'osss:EarningsRecord' ] [ 'osss:Earnings' ]
291
+ var newrecord = {
292
+ '@_startYear' : newvalue ,
293
+ '@_endYear' : newvalue ,
294
+ 'osss:FicaEarnings' : combinedValues . shift ( ) ,
295
+ 'osss:MedicafreEarnings' : combinedValues . shift ( )
296
+ }
297
+
298
+ currentRecord . push ( newrecord )
299
+ }
300
+ } while ( combinedValues . length > 0 ) } )
301
+ . then ( ( ) => {
302
+ var earningsJSON = JSON . stringify ( this . state . defaultRecord )
303
+ SessionStore . push ( 'earnings' , earningsJSON )
304
+ this . setState ( {
305
+ earningsRecord : this . state . defaultRecord
306
+ } )
307
+ } )
308
+ }
309
+
262
310
handleUpload ( formResponse ) {
263
311
this . setState ( {
264
312
displayTable : true
265
313
} ) ;
266
314
formResponse . preventDefault ( ) ;
267
315
const file = this . fileInput . current . files [ 0 ]
268
- const name = this . fileInput . current . files [ 0 ] . name
269
- const formData = new FormData ( ) ;
270
- formData . append ( name , file )
271
-
316
+ var name = this . fileInput . current . files [ 0 ] . name
317
+ name = name . split ( '.' )
318
+ const extension = name [ name . length - 1 ]
272
319
var reader = new FileReader ( )
273
- reader . readAsText ( file ) ;
274
320
275
- reader . onload = ( reader ) => this . handleLoadTable ( reader )
321
+ switch ( extension ) {
322
+ case 'xml' :
323
+ reader . onload = ( reader ) => this . handleXMLFile ( reader )
324
+ reader . readAsText ( file ) ;
325
+ break ;
326
+
327
+ case 'pdf' :
328
+ reader . onload = ( reader ) => this . handlePDFFile ( reader )
329
+ reader . readAsArrayBuffer ( file )
330
+ break ;
331
+
332
+ default :
333
+ alert ( "I'm sorry, that file was not recognized." )
334
+ break ;
335
+ }
336
+
337
+
338
+
339
+
276
340
}
277
341
278
342
//Stores users input for manually entered table to allow for persistence across page changes
279
343
handleManualEarnings ( input ) {
344
+ // eslint-disable-next-line
280
345
const [ type , year , key ] = input . target . id . split ( '_' )
281
346
282
347
var tempManualTable = this . state . manualTable
@@ -292,17 +357,17 @@ export default class FileUpload extends React.Component {
292
357
293
358
//Saves manually entered record to this.state.earningsRecord object, becomes noticable to Observable API
294
359
handleSave ( ) {
295
- var tempRecord = this . state . earningsRecord ?
360
+ var tempRecord = this . state . earningsRecord ?
296
361
this . state . earningsRecord [ 'osss:OnlineSocialSecurityStatementData' ] [ 'osss:EarningsRecord' ] [ 'osss:Earnings' ] . length === this . state . manualTable . length
297
362
? this . state . earningsRecord : this . state . defaultRecord
298
363
:
299
364
this . state . defaultRecord
300
365
301
- this . state . manualTable . map ( ( record , i ) => {
366
+ this . state . manualTable . forEach ( ( record , i ) => {
302
367
var currentRecord = tempRecord [ 'osss:OnlineSocialSecurityStatementData' ] [ 'osss:EarningsRecord' ] [ 'osss:Earnings' ]
303
368
var newrecord = {
304
369
'@_startYear' : record [ 'year' ] ,
305
- '@_endYear' : record [ 'year' ] ,
370
+ '@_endYear' : record [ 'year' ] ,
306
371
'osss:FicaEarnings' : record [ 'value' ]
307
372
}
308
373
@@ -311,7 +376,7 @@ export default class FileUpload extends React.Component {
311
376
} else {
312
377
currentRecord [ i ] = newrecord
313
378
}
314
-
379
+
315
380
} )
316
381
317
382
var arrayJSON = JSON . stringify ( this . state . manualTable )
@@ -340,10 +405,10 @@ export default class FileUpload extends React.Component {
340
405
< UploadLabel htmlFor = "inputfile" className = "btn" > { this . state . buttonText } </ UploadLabel >
341
406
< UploadInput type = { this . state . buttonType } id = 'inputfile' ref = { this . fileInput } onChange = { this . state . buttonFunction } > </ UploadInput >
342
407
</ UploadButton >
343
- < GenerateTable
344
- parsedXml = { this . state . earningsRecord }
345
- handleInputEarnings = { this . handleInputEarnings }
346
- manual = { this . props . manual }
408
+ < GenerateTable
409
+ parsedXml = { this . state . earningsRecord }
410
+ handleInputEarnings = { this . handleInputEarnings }
411
+ manual = { this . props . manual }
347
412
manualTable = { this . state . manualTable }
348
413
handleManualEarnings = { this . handleManualEarnings }
349
414
handleSave = { this . handleSave }
@@ -356,4 +421,4 @@ export default class FileUpload extends React.Component {
356
421
</ div >
357
422
)
358
423
}
359
- }
424
+ }
0 commit comments