@@ -2,7 +2,7 @@ import React from "react";
2
2
import styled from "@emotion/styled" ;
3
3
import fastXml from 'fast-xml-parser' ;
4
4
import { spacing , colors , fontSizes , radii } from "../constants" ;
5
- import { ObservableCell } from "../components" ;
5
+ import { ObservableCell , SessionStore } from "../components" ;
6
6
7
7
//Upload page specific css/html
8
8
export const UploadButton = styled ( "div" ) `
@@ -69,11 +69,10 @@ export class GenerateTable extends React.Component {
69
69
const earnings = parsedXml [ 'osss:OnlineSocialSecurityStatementData' ] [ 'osss:EarningsRecord' ] [ 'osss:Earnings' ] ;
70
70
header = < tr > < TableHeader > Year</ TableHeader > < TableHeader > Amount</ TableHeader > </ tr > ;
71
71
tableRows = earnings . map ( ( earning , i ) => {
72
- this . props . updateCounter ( earning [ 'osss:FicaEarnings' ] )
73
72
return (
74
73
< TableRow key = { i } >
75
74
< td > < label > { earning [ '@_startYear' ] } </ label > </ td >
76
- < td > < input defaultValue = { earning [ 'osss:FicaEarnings' ] } > </ input > </ td >
75
+ < td > < input id = { earning [ '@_startYear' ] } defaultValue = { earning [ 'osss:FicaEarnings' ] } onChange = { this . props . handleInputEarnings } > </ input > </ td >
77
76
</ TableRow >
78
77
)
79
78
} ) ;
@@ -100,54 +99,77 @@ export default class FileUpload extends React.Component {
100
99
101
100
this . handleSubmit = this . handleSubmit . bind ( this ) ;
102
101
this . handleLoadTable = this . handleLoadTable . bind ( this ) ;
103
- this . handleYearsChange = this . handleYearsChange . bind ( this ) ;
102
+ this . handleInputEarnings = this . handleInputEarnings . bind ( this ) ;
103
+ this . assertLoad = this . assertLoad . bind ( this ) ;
104
104
this . customObserver = this . customObserver . bind ( this ) ;
105
- this . updateCounter = this . updateCounter . bind ( this ) ;
106
105
this . fileInput = React . createRef ( ) ;
107
106
108
107
this . state = {
109
- earningsRecord : '' ,
110
- displayTable : false ,
111
- yearsSE : 0 ,
112
- counter : [ ] ,
108
+ elementLoaded : false ,
109
+ earningsRecord : undefined ,
110
+ displayTable : false
113
111
} ;
114
112
}
115
113
116
- componentDidUpdate ( nextProps , nextState ) {
117
- this . testEarning . value = this . state . yearsSE ;
118
- if ( nextState . earningsRecord !== this . state . earningsRecord ) {
119
- this . setState ( {
120
- counter : [ ]
121
- } )
122
- }
123
-
114
+ componentDidUpdate ( prevProps , prevState ) {
115
+ if ( this . state . elementLoaded ) {
116
+ this . parseXML . value = this . state . earningsRecord
117
+ }
124
118
}
125
119
126
- customObserver ( ) {
120
+ componentDidMount ( ) {
121
+ if ( SessionStore . get ( 'earnings' ) ) {
122
+ var earningsValue = JSON . parse ( SessionStore . get ( 'earnings' ) )
123
+ this . setState ( {
124
+ earningsRecord : earningsValue
125
+ } )
126
+
127
+ }
128
+ }
129
+
130
+ assertLoad ( ) {
131
+ this . setState ( {
132
+ elementLoaded : true
133
+ } )
134
+ }
135
+
136
+ customObserver ( test ) {
127
137
return { fulfilled : ( value ) => {
128
- this . testEarning = value
138
+ this . parseXML = value
139
+ this . assertLoad ( )
129
140
} }
130
141
}
131
142
132
- updateCounter ( earning ) {
133
- if ( earning > 1000 & ! this . state . counter . includes ( earning ) ) {
134
- this . state . counter . push ( earning )
135
- this . setState ( {
136
- yearsSE : this . state . counter . length
137
- } )
143
+ handleInputEarnings ( input ) {
144
+ var modifiedEarnings = this . state . earningsRecord
145
+ var earnings = modifiedEarnings [ 'osss:OnlineSocialSecurityStatementData' ] [ 'osss:EarningsRecord' ] [ 'osss:Earnings' ]
146
+ var modifiedyear = input . target . id
147
+
148
+ var findValue = earnings . find ( ( element ) => {
149
+ if ( element [ '@_startYear' ] === modifiedyear ) {
150
+ element [ 'osss:FicaEarnings' ] = input . target . value
151
+ return true
152
+ }
153
+ } )
154
+
155
+ if ( findValue ) {
156
+ modifiedEarnings [ 'osss:OnlineSocialSecurityStatementData' ] [ 'osss:EarningsRecord' ] [ 'osss:Earnings' ] = earnings
157
+
158
+ var earningsJSON = JSON . stringify ( modifiedEarnings )
159
+ SessionStore . push ( 'earnings' , earningsJSON )
160
+
161
+ this . setState ( {
162
+ earningsRecord : modifiedEarnings
163
+ } )
138
164
}
139
- //console.log(this.state.counter)
140
- }
141
165
142
- handleYearsChange ( e ) {
143
- this . setState ( {
144
- yearsSE : e . target . value
145
- } )
146
- }
166
+ }
147
167
148
168
handleLoadTable ( reader ) {
149
169
if ( fastXml . validate ( reader . target . result ) === true ) {
150
170
var parsedText = fastXml . parse ( reader . target . result , { ignoreAttributes : false } )
171
+ var earningsJSON = JSON . stringify ( parsedText )
172
+ SessionStore . push ( 'earnings' , earningsJSON )
151
173
this . setState ( {
152
174
earningsRecord : parsedText
153
175
} )
@@ -164,7 +186,6 @@ export default class FileUpload extends React.Component {
164
186
const formData = new FormData ( ) ;
165
187
formData . append ( name , file )
166
188
167
-
168
189
var reader = new FileReader ( )
169
190
reader . readAsText ( file ) ;
170
191
@@ -178,10 +199,10 @@ export default class FileUpload extends React.Component {
178
199
< UploadLabel htmlFor = "inputfile" className = "btn" > Upload Earnings Record</ UploadLabel >
179
200
< UploadInput type = 'file' id = 'inputfile' ref = { this . fileInput } onChange = { this . handleSubmit } > </ UploadInput >
180
201
</ UploadButton >
181
- < GenerateTable parsedXml = { this . state . earningsRecord } updateCounter = { this . updateCounter } counter = { this . state . counter } />
182
- < div > < ObservableCell cellname = ' mutable yearsSE' customObserver = { this . customObserver } /> </ div >
202
+ < GenerateTable parsedXml = { this . state . earningsRecord } handleInputEarnings = { this . handleInputEarnings } />
203
+ < div > < ObservableCell cellname = " mutable parsedXmlFileText" customObserver = { this . customObserver } /> </ div >
183
204
< div style = { { display : 'none' } } > < ObservableCell cellname = 'calculationDisplay' /> </ div >
184
205
</ div >
185
206
)
186
207
}
187
- }
208
+ }
0 commit comments