CartoDB/carto-react

GeoJSON support in useCartoLayerProps

aaranadev opened this issue · 1 comments

Describe the bug
Hi guys! I've detected a bug in useCartoLayerProps derived from Geojson support. We have tried using it in a GeoJSON layer but we have several issues.

The workflow is set to be used in onDataLoad that is used the Layer, but the problem is that GeoJSON Layer hasn't onDataLoad prop.

We have resolved this issue by creating a new CompositeLayer and adding a new prop to the Layer called onDataLoad that is used to set the data in the Layer.

GeoJSON uses the feature useGeojsonFeatures to set the data in the Layer, but this feature uses setGeoJsonLoaded to set the flag geoJsonLoaded to true. This flag is used in useCartoLayerProps to put the data in the Layer, but that function is not exist in useFeaturesCommons which is used in useGeojsonFeatures so the workflow is not worked.

export default function useGeojsonFeatures({
  source,
  viewport,
  spatialFilter,
  uniqueIdProperty,
  debounceTimeout = 250,
}: any) {
  const [
    debounceIdRef,
    isGeoJsonLoaded,
    setGeoJsonLoaded,
    clearDebounce,
    stopAnyCompute,
    setSourceFeaturesReady,
  ] = useFeaturesCommons({ source })
const onDataLoad = useCallback(
    (geojson) => {
      // @ts-ignore
      stopAnyCompute()
      // @ts-ignore
      setSourceFeaturesReady(false)
      // @ts-ignore
      executeTask(sourceId, Methods.LOAD_GEOJSON_FEATURES, { geojson })
        // @ts-ignore
        .then(() => setGeoJsonLoaded(true))
      // .catch(throwError)
    },
    [sourceId, setSourceFeaturesReady, stopAnyCompute, setGeoJsonLoaded],
  )

OnDataLoad works with a Promise.