11import React , { Component } from 'react'
22import { Link } from 'react-router-dom'
3+ import { isEqual } from 'lodash'
34import { getItemCSV , getSubsetCSV , getSubsetDetails } from '../api.js'
45import Header from '../common/Header.jsx'
5- import LEI_COUNTS from '../constants/leiCounts.js'
66import MSAMD_COUNTS from '../constants/msamdCounts.js'
77import STATE_COUNTS from '../constants/stateCounts.js'
88import { makeSearchFromState , makeStateFromSearch } from '../query.js'
@@ -11,6 +11,7 @@ import Aggregations from './Aggregations.jsx'
1111import './Geography.css'
1212import InstitutionSelect from './InstitutionSelect'
1313import ItemSelect from './ItemSelect.jsx'
14+ import { fetchLeis , filterLeis } from './leiUtils'
1415import {
1516 createItemOptions ,
1617 createVariableOptions ,
@@ -36,6 +37,8 @@ class Geography extends Component {
3637 this . setStateAndRoute = this . setStateAndRoute . bind ( this )
3738 this . updateSearch = this . updateSearch . bind ( this )
3839 this . scrollToTable = this . scrollToTable . bind ( this )
40+ this . fetchLeis = fetchLeis . bind ( this )
41+ this . filterLeis = filterLeis . bind ( this )
3942
4043 this . itemOptions = createItemOptions ( props )
4144 this . variableOptions = createVariableOptions ( )
@@ -55,7 +58,12 @@ class Geography extends Component {
5558 orderedVariables : [ ] ,
5659 details : { } ,
5760 loadingDetails : false ,
58- error : null
61+ error : null ,
62+ leiDetails : {
63+ loading : true ,
64+ counts : { } ,
65+ leis : { }
66+ }
5967 }
6068
6169 const newState = makeStateFromSearch ( this . props . location . search , defaultState , this . requestSubset , this . updateSearch )
@@ -64,6 +72,22 @@ class Geography extends Component {
6472 return newState
6573 }
6674
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+
6791 updateSearch ( ) {
6892 this . props . history . replace ( { search : makeSearchFromState ( this . state ) } )
6993 }
@@ -115,9 +139,21 @@ class Geography extends Component {
115139 }
116140
117141 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
121157 if ( category === 'states' ) return this . checkIfLargeCount ( items , STATE_COUNTS )
122158 if ( category === 'msamds' ) return this . checkIfLargeCount ( items , MSAMD_COUNTS )
123159 return false
@@ -133,7 +169,6 @@ class Geography extends Component {
133169 this . setStateAndRoute ( {
134170 category : catObj . value ,
135171 items : [ ] ,
136- leis : [ ] ,
137172 details : { } ,
138173 isLargeFile : this . checkIfLargeFile ( catObj . value , [ ] ) ,
139174 } )
@@ -147,18 +182,17 @@ class Geography extends Component {
147182 return this . setStateAndRoute ( {
148183 items,
149184 details : { } ,
150- isLargeFile : this . checkIfLargeFile ( this . state . category , items )
151185 } )
152186 }
153187
154188 onInstitutionChange ( selectedLEIs = [ ] ) {
155189 let leis = selectedLEIs . map ( l => l . value )
156190 if ( leis . includes ( 'all' ) ) leis = [ ]
157191
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+ } )
162196 } )
163197 }
164198
@@ -219,7 +253,7 @@ class Geography extends Component {
219253 const total = formatWithCommas ( this . makeTotal ( details ) )
220254 return (
221255 < >
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 } />
223257 < div className = "CSVButtonContainer" >
224258 { this . renderTotal ( total ) }
225259 </ div >
@@ -228,7 +262,8 @@ class Geography extends Component {
228262 }
229263
230264 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
232267
233268 const nationwide = isNationwide ( category )
234269 const enabled = nationwide || items . length
@@ -269,8 +304,7 @@ class Geography extends Component {
269304 < InstitutionSelect
270305 items = { leis }
271306 onChange = { this . onInstitutionChange }
272- options = { this . itemOptions }
273- nationwide = { nationwide }
307+ leiDetails = { leiDetails }
274308 />
275309 < VariableSelect
276310 options = { this . variableOptions }
@@ -286,7 +320,7 @@ class Geography extends Component {
286320 < ActionsWarningsErrors
287321 downloadEnabled = { enabled }
288322 downloadCallback = { checksExist ? this . requestSubsetCSV : this . requestItemCSV }
289- showSummaryButton = { ! details . aggregations || ! this . makeTotal ( details ) }
323+ showSummaryButton = { ! details . aggregations }
290324 summaryEnabled = { enabled && checksExist }
291325 loadingDetails = { loadingDetails }
292326 requestSubset = { this . requestSubset }
0 commit comments