11import { useEffect , useMemo , useRef , useState , useCallback } from "react" ;
22import { CarmaMap , DatasheetLayout } from "@carma-mapping/core" ;
33import { useDispatch , useSelector } from "react-redux" ;
4- import { setSelectedFeature } from "../../store/slices/featureCollection" ;
4+ import {
5+ setSelectedFeature ,
6+ setFeatureLoading ,
7+ } from "../../store/slices/featureCollection" ;
58import {
69 getActiveBackgroundLayer ,
710 getBackgroundLayerOpacities ,
@@ -27,6 +30,8 @@ import type maplibregl from "maplibre-gl";
2730import BelisDatasheetView from "../ui/BelisDatasheetView" ;
2831import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" ;
2932import { faMap } from "@fortawesome/free-solid-svg-icons" ;
33+ import { getJWT } from "../../store/slices/auth" ;
34+ import { FeatureType , fetchFeatureById } from "../../helper/apiMethods" ;
3035
3136const LIST_WIDTH = 300 ;
3237
@@ -35,9 +40,11 @@ const MINI_MAP_DEBUGGING = false;
3540
3641const BelisMapLibWrapper = ( { mapSizes } ) => {
3742 const dispatch : AppDispatch = useDispatch ( ) ;
43+ const jwt = useSelector ( getJWT ) ;
3844 const { map } = useLibreContext ( ) ;
39- const { selectedFeature, rawFeature } = useMapSelection ( ) ;
45+ const { selectedFeature, rawFeature, selectedFeatureId } = useMapSelection ( ) ;
4046 const { closeDatasheet } = useDatasheet ( ) ;
47+ const [ fetchedFeatureData , setFetchedFeatureData ] = useState < any > ( null ) ;
4148
4249 const activeBackgroundLayer = useSelector ( getActiveBackgroundLayer ) ;
4350 const backgroundLayerOpacities = useSelector ( getBackgroundLayerOpacities ) ;
@@ -119,6 +126,44 @@ const BelisMapLibWrapper = ({ mapSizes }) => {
119126
120127 const mapWidth = mapSizes . width - LIST_WIDTH ;
121128
129+ useEffect ( ( ) => {
130+ const fetchData = async ( ) => {
131+ if ( ! jwt || ! selectedFeatureId ?. id ) {
132+ setFetchedFeatureData ( null ) ;
133+ return ;
134+ }
135+
136+ // Get sourceLayer from selectedFeatureId or rawFeature
137+ const sourceLayer = selectedFeatureId . sourceLayer ;
138+
139+ console . log ( "xxx BelisMa Selection:" , {
140+ id : selectedFeatureId . id ,
141+ sourceLayer,
142+ } ) ;
143+
144+ if ( sourceLayer && selectedFeatureId . id ) {
145+ dispatch ( setFeatureLoading ( true ) ) ;
146+ try {
147+ const fullData = await fetchFeatureById (
148+ jwt ,
149+ selectedFeatureId . id as number ,
150+ sourceLayer as FeatureType
151+ ) ;
152+ console . log ( "xxx Fetched full data:" , fullData ) ;
153+ // Pass full data - forms will extract what they need internally
154+ setFetchedFeatureData ( fullData ) ;
155+ } catch ( error ) {
156+ console . error ( "xxx Failed to fetch feature:" , error ) ;
157+ setFetchedFeatureData ( null ) ;
158+ } finally {
159+ dispatch ( setFeatureLoading ( false ) ) ;
160+ }
161+ }
162+ } ;
163+
164+ fetchData ( ) ;
165+ } , [ selectedFeatureId , jwt ] ) ;
166+
122167 return (
123168 < div
124169 className = "relative flex"
@@ -197,6 +242,8 @@ const BelisMapLibWrapper = ({ mapSizes }) => {
197242 < BelisDatasheetView
198243 feature = { selectedFeature }
199244 rawFeature = { rawFeature }
245+ fetchedData = { fetchedFeatureData }
246+ featureType = { selectedFeatureId ?. sourceLayer }
200247 />
201248 </ div >
202249 }
0 commit comments