@@ -3,18 +3,30 @@ import { ref, onMounted, onUnmounted, watch, nextTick } from 'vue'
33import { initializeMap , addDivisionLayers , loadDivisionData } from ' ./map/mapConfig'
44import { highlightMatchedDivisions , fitMapToFeatures } from ' ./map/divisionHighlighting'
55import { fetchDivisionStats , updateSourceWithCounts } from ' ./map/divisionStats'
6- import { debounce } from ' ./map/divisionUtils'
6+ import { debounce , divisionWithHyphen } from ' ./map/divisionUtils'
7+ import { createClient } from ' @supabase/supabase-js'
78
89const mapContainer = ref (null )
910let map = null
1011
12+ // Initialize Supabase client
13+ const supabase = createClient (
14+ import .meta.env.VITE_SUPABASE_URL,
15+ import .meta.env.VITE_SUPABASE_ANON_KEY,
16+ {
17+ auth: {
18+ persistSession: false
19+ }
20+ }
21+ )
22+
1123// Store highlighted divisions in reactive state
1224const highlightedDivisions = ref ([])
1325
1426// Store division stats
1527const divisionStats = ref ({})
1628
17- // Define props for search results
29+ // Define props and emits
1830const props = defineProps ({
1931 searchResults: {
2032 type: Object ,
@@ -26,11 +38,41 @@ const props = defineProps({
2638 }
2739})
2840
41+ const emit = defineEmits ([' update:searchResults' ])
42+
2943// Create debounced version of fitMapToFeatures
3044const debouncedFitMapToFeatures = debounce ((map , divisions , element ) => {
3145 fitMapToFeatures (map, divisions, element)
3246}, 300 )
3347
48+ // Function to handle division clicks
49+ async function handleDivisionClick (division ) {
50+ console .log (' Division clicked:' , division)
51+ try {
52+ const { data , error } = await supabase
53+ .from (' phila_ballots' )
54+ .select (' name, division, id_number, birth_year, zip, ballot_status_reason' )
55+ .eq (' division' , divisionWithHyphen (division))
56+ .limit (100 )
57+
58+ if (error) {
59+ console .error (' Supabase search error:' , error)
60+ return
61+ }
62+
63+ console .log (' Search results:' , data)
64+
65+ // Update search results which will automatically update the panel
66+ emit (' update:searchResults' , {
67+ matches: data,
68+ divisions: [division]
69+ })
70+
71+ } catch (err) {
72+ console .error (' Search error:' , err)
73+ }
74+ }
75+
3476// Watch for search results changes
3577watch (() => props .searchResults , (newResults ) => {
3678 console .log (' Search results updated:' , newResults)
@@ -76,16 +118,34 @@ onMounted(async () => {
76118 updateSourceWithCounts (map, divisionStats .value )
77119
78120 // Add hover interaction
79- map .on (' mousemove' , ' divisions-layer ' , (e ) => {
121+ map .on (' mousemove' , ' divisions-fill ' , (e ) => {
80122 if (e .features .length > 0 ) {
81123 const division = e .features [0 ].properties .DIVISION_NUM
82124 map .setFilter (' divisions-hover' , [' ==' , [' get' , ' DIVISION_NUM' ], division])
83125 }
84126 })
85127
86- map .on (' mouseleave' , ' divisions-layer ' , () => {
128+ map .on (' mouseleave' , ' divisions-fill ' , () => {
87129 map .setFilter (' divisions-hover' , [' ==' , [' get' , ' DIVISION_NUM' ], ' ' ])
88130 })
131+
132+ // Add click interaction
133+ map .on (' click' , ' divisions-fill' , (e ) => {
134+ if (e .features .length > 0 ) {
135+ const division = e .features [0 ].properties .DIVISION_NUM
136+ handleDivisionClick (division)
137+ }
138+ })
139+
140+ // Set cursor to pointer when hovering over divisions
141+ map .on (' mouseenter' , ' divisions-fill' , () => {
142+ map .getCanvas ().style .cursor = ' pointer'
143+ })
144+
145+ map .on (' mouseleave' , ' divisions-fill' , () => {
146+ map .getCanvas ().style .cursor = ' '
147+ })
148+
89149 } catch (err) {
90150 console .error (' Error loading map data:' , err)
91151 }
0 commit comments