1
- import { filterFiles } from './mapHelpers'
2
- import axios from 'axios'
1
+ import { filterFiles , returnLoadParams , queryDB } from './mapHelpers'
3
2
const d3 = require ( 'd3' ) ;
4
3
5
- //META MAP VARS -- MAKE THIS BETTER LATER
6
- const assemblyLoc = 'https://raw.githubusercontent.com/cngonzalez/nycet-flatfiles/master/locational/nyad_geo.json'
7
- const electionLoc = 'https://raw.githubusercontent.com/cngonzalez/nycet-flatfiles/master/locational/nyed_geo.json'
8
- const assemblyDataLoc = 'https://raw.githubusercontent.com/cngonzalez/nycet-flatfiles/master/ad_margins.tsv'
9
- const electionDataLoc = 'https://raw.githubusercontent.com/cngonzalez/nycet-flatfiles/master/ed_margins.tsv'
10
-
11
- const DISTRICT_LEVEL_LABELS = {
12
- //top-level: NYC show all AD, SD, CD
13
- //eventually add diff levels
14
- AD : [ assemblyLoc , assemblyDataLoc , 'AssemDist' , 'districtnumber' ] ,
15
- //mid-level: show EDs for a specific level
16
- ED : [ electionLoc , electionDataLoc , 'ElectDist' , 'ed' ] }
17
-
18
-
19
4
//ACTION CREATORS
20
5
export const loadMapData = ( props ) =>
21
6
//use this thunk syntax, because d3.queue happens async
22
7
dispatch => {
23
-
24
8
dispatch ( announceLoading )
25
9
let selected = props . parentDistId
26
10
let districtType = ( selected === 0 ) ? props . parentDistType : 'ED'
27
- let [ geoSource ,
28
- dataSource ,
29
- mapRegionType ,
30
- dataRegionType ] = DISTRICT_LEVEL_LABELS [ districtType ] ;
31
-
32
- return (
33
- d3 . queue ( )
34
- . defer ( d3 . json , geoSource )
35
- . defer ( d3 . tsv , dataSource )
36
- . await ( ( error , geoFile , dataFile ) => {
37
- let [ filteredGeo ,
38
- filteredData ] = filterFiles ( geoFile , dataFile , mapRegionType , dataRegionType , selected ) ;
39
-
40
- let dataMap = d3 . map ( )
41
- filteredData . forEach ( ( d ) => {
42
- d . margin = ( ( d . winning_party === 'Republican ' ) ? - d . margin : + d . margin )
43
- dataMap . set ( d [ dataRegionType ] , d . margin ) } )
44
- dispatch ( storeMapData (
45
- { geoJson : filteredGeo ,
46
- geoData : dataMap } , 'LOAD_MAP_DATA' ) )
11
+ console . log ( districtType )
12
+ dispatch ( changeDistrict ( districtType ) )
13
+ let { mapRegionType,
14
+ geoSource , table } = returnLoadParams ( districtType )
15
+
16
+ //changes econd parent dist type to election state eventaully
17
+ queryDB ( props . parentDistType , table , props . parentDistType , selected ) . then ( dataPull => {
18
+ d3 . queue ( )
19
+ . defer ( d3 . json , geoSource )
20
+ . await ( ( error , geoFile ) => {
21
+ let [ filteredGeo ,
22
+ filteredData ] = filterFiles ( geoFile , dataPull . data , mapRegionType , selected ) ;
23
+
24
+ let dataMap = d3 . map ( )
25
+ filteredData . forEach ( ( d ) => {
26
+ d . most_rec_pl_margin = ( ( d . winning_pol_lean === 'right ' ) ? - d . most_rec_pl_margin : + d . most_rec_pl_margin )
27
+ dataMap . set ( d . district , d . most_rec_pl_margin ) } )
28
+ dispatch ( storeMapData (
29
+ { geoJson : filteredGeo ,
30
+ geoData : dataMap } , 'LOAD_MAP_DATA' ) )
47
31
} )
48
- )
32
+ } )
49
33
}
50
34
51
35
@@ -63,3 +47,7 @@ export const setMapDimensions = (width, height) => (
63
47
export const announceLoading = ( ) => (
64
48
{ type : 'LOAD_DATA' }
65
49
)
50
+
51
+ export const changeDistrict = ( distType ) => (
52
+ { type : 'CHANGE_DISTRICT_TYPE' ,
53
+ payload : distType } )
0 commit comments