1
1
import React , { Component } from 'react'
2
2
import { Link } from 'react-router-dom'
3
+ import { isEqual } from 'lodash'
3
4
import { getItemCSV , getSubsetCSV , getSubsetDetails } from '../api.js'
4
5
import Header from '../common/Header.jsx'
5
- import LEI_COUNTS from '../constants/leiCounts.js'
6
6
import MSAMD_COUNTS from '../constants/msamdCounts.js'
7
7
import STATE_COUNTS from '../constants/stateCounts.js'
8
8
import { makeSearchFromState , makeStateFromSearch } from '../query.js'
@@ -11,6 +11,7 @@ import Aggregations from './Aggregations.jsx'
11
11
import './Geography.css'
12
12
import InstitutionSelect from './InstitutionSelect'
13
13
import ItemSelect from './ItemSelect.jsx'
14
+ import { fetchLeis , filterLeis } from './leiUtils'
14
15
import {
15
16
createItemOptions ,
16
17
createVariableOptions ,
@@ -36,6 +37,8 @@ class Geography extends Component {
36
37
this . setStateAndRoute = this . setStateAndRoute . bind ( this )
37
38
this . updateSearch = this . updateSearch . bind ( this )
38
39
this . scrollToTable = this . scrollToTable . bind ( this )
40
+ this . fetchLeis = fetchLeis . bind ( this )
41
+ this . filterLeis = filterLeis . bind ( this )
39
42
40
43
this . itemOptions = createItemOptions ( props )
41
44
this . variableOptions = createVariableOptions ( )
@@ -55,7 +58,12 @@ class Geography extends Component {
55
58
orderedVariables : [ ] ,
56
59
details : { } ,
57
60
loadingDetails : false ,
58
- error : null
61
+ error : null ,
62
+ leiDetails : {
63
+ loading : true ,
64
+ counts : { } ,
65
+ leis : { }
66
+ }
59
67
}
60
68
61
69
const newState = makeStateFromSearch ( this . props . location . search , defaultState , this . requestSubset , this . updateSearch )
@@ -64,6 +72,22 @@ class Geography extends Component {
64
72
return newState
65
73
}
66
74
75
+ componentDidMount ( ) {
76
+ this . fetchLeis ( )
77
+ this . filterLeis ( )
78
+ this . setState ( { isLargeFile : this . checkIfLargeFile ( this . state . category , this . state . items ) } )
79
+ }
80
+
81
+ componentDidUpdate ( prevProps , prevState ) {
82
+ const geographyChanged = ! isEqual ( prevState . items , this . state . items )
83
+ const leisReloaded = prevState . leiDetails . loading && ! this . state . leiDetails . loading
84
+
85
+ if ( geographyChanged ) this . fetchLeis ( )
86
+ if ( geographyChanged || leisReloaded ) this . filterLeis ( )
87
+ if ( leisReloaded )
88
+ this . setState ( { isLargeFile : this . checkIfLargeFile ( this . state . category , this . state . items ) } )
89
+ }
90
+
67
91
updateSearch ( ) {
68
92
this . props . history . replace ( { search : makeSearchFromState ( this . state ) } )
69
93
}
@@ -115,9 +139,21 @@ class Geography extends Component {
115
139
}
116
140
117
141
checkIfLargeFile ( category , items ) {
118
- if ( isNationwide ( category ) && ! items . length ) return true
119
- if ( isNationwide ( category ) || category === 'leis' )
120
- return this . checkIfLargeCount ( items , LEI_COUNTS )
142
+ const leisSelected = this . state && this . state . leis . length
143
+ const leisFetched = this . state && ! this . state . leiDetails . loading
144
+
145
+ if ( category === 'leis' ) {
146
+ if ( items . length && leisFetched )
147
+ return this . checkIfLargeCount ( items , this . state . leiDetails . counts )
148
+
149
+ if ( ! items . length )
150
+ return this . checkIfLargeFile ( this . state . category , this . state . items )
151
+ }
152
+
153
+ if ( leisSelected && leisFetched )
154
+ return this . checkIfLargeCount ( this . state . leis , this . state . leiDetails . counts )
155
+
156
+ if ( isNationwide ( category ) ) return true
121
157
if ( category === 'states' ) return this . checkIfLargeCount ( items , STATE_COUNTS )
122
158
if ( category === 'msamds' ) return this . checkIfLargeCount ( items , MSAMD_COUNTS )
123
159
return false
@@ -133,7 +169,6 @@ class Geography extends Component {
133
169
this . setStateAndRoute ( {
134
170
category : catObj . value ,
135
171
items : [ ] ,
136
- leis : [ ] ,
137
172
details : { } ,
138
173
isLargeFile : this . checkIfLargeFile ( catObj . value , [ ] ) ,
139
174
} )
@@ -147,18 +182,17 @@ class Geography extends Component {
147
182
return this . setStateAndRoute ( {
148
183
items,
149
184
details : { } ,
150
- isLargeFile : this . checkIfLargeFile ( this . state . category , items )
151
185
} )
152
186
}
153
187
154
188
onInstitutionChange ( selectedLEIs = [ ] ) {
155
189
let leis = selectedLEIs . map ( l => l . value )
156
190
if ( leis . includes ( 'all' ) ) leis = [ ]
157
191
158
- return this . setStateAndRoute ( {
159
- leis ,
160
- details : { } ,
161
- isLargeFile : this . checkIfLargeFile ( this . state . category , leis )
192
+ return this . setState ( { leis , details : { } } , ( ) => {
193
+ return this . setStateAndRoute ( {
194
+ isLargeFile : this . checkIfLargeFile ( 'leis' , this . state . leis )
195
+ } )
162
196
} )
163
197
}
164
198
@@ -219,7 +253,7 @@ class Geography extends Component {
219
253
const total = formatWithCommas ( this . makeTotal ( details ) )
220
254
return (
221
255
< >
222
- < Aggregations ref = { this . tableRef } details = { details } orderedVariables = { orderedVariables } year = { this . props . match . params . year } />
256
+ < Aggregations ref = { this . tableRef } details = { details } orderedVariables = { orderedVariables } year = { this . props . match . params . year } leis = { this . state . leiDetails } />
223
257
< div className = "CSVButtonContainer" >
224
258
{ this . renderTotal ( total ) }
225
259
</ div >
@@ -228,7 +262,8 @@ class Geography extends Component {
228
262
}
229
263
230
264
render ( ) {
231
- const { category, items, leis, isLargeFile, variables, orderedVariables, details, loadingDetails, error } = this . state
265
+ const { category, details, error, isLargeFile, items, leiDetails, leis,
266
+ loadingDetails, orderedVariables, variables } = this . state
232
267
233
268
const nationwide = isNationwide ( category )
234
269
const enabled = nationwide || items . length
@@ -269,8 +304,7 @@ class Geography extends Component {
269
304
< InstitutionSelect
270
305
items = { leis }
271
306
onChange = { this . onInstitutionChange }
272
- options = { this . itemOptions }
273
- nationwide = { nationwide }
307
+ leiDetails = { leiDetails }
274
308
/>
275
309
< VariableSelect
276
310
options = { this . variableOptions }
@@ -286,7 +320,7 @@ class Geography extends Component {
286
320
< ActionsWarningsErrors
287
321
downloadEnabled = { enabled }
288
322
downloadCallback = { checksExist ? this . requestSubsetCSV : this . requestItemCSV }
289
- showSummaryButton = { ! details . aggregations || ! this . makeTotal ( details ) }
323
+ showSummaryButton = { ! details . aggregations }
290
324
summaryEnabled = { enabled && checksExist }
291
325
loadingDetails = { loadingDetails }
292
326
requestSubset = { this . requestSubset }
0 commit comments