-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #553 from geoadmin/feat_load_geojson_data_in_store
Load GeoJSON data on addLayer in a store plugin
- Loading branch information
Showing
6 changed files
with
92 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Listen to the `addLayer` mutation, and if a GeoJSON is added without data/style defined, we load | ||
* it here | ||
*/ | ||
|
||
import GeoAdminGeoJsonLayer from '@/api/layers/GeoAdminGeoJsonLayer.class' | ||
import log from '@/utils/logging' | ||
import axios from 'axios' | ||
|
||
async function load(url) { | ||
try { | ||
return await axios.get(url) | ||
} catch (error) { | ||
log.error(`Error while loading URL ${url}`, error) | ||
throw error | ||
} | ||
} | ||
|
||
/** | ||
* @param {Vuex.Store} store | ||
* @param {GeoAdminGeoJsonLayer} geoJsonLayer | ||
* @returns {Promise<void>} | ||
*/ | ||
async function loadDataAndStyle(store, geoJsonLayer) { | ||
try { | ||
const { data: style } = await load(geoJsonLayer.styleUrl) | ||
const { data } = await load(geoJsonLayer.geoJsonUrl) | ||
// as the layer comes from the store (99.9% chances), we copy it before altering it | ||
// (otherwise, Vuex raises an error) | ||
const layerCopy = geoJsonLayer.clone() | ||
layerCopy.geoJsonData = data | ||
layerCopy.geoJsonStyle = style | ||
layerCopy.isLoading = false | ||
store.dispatch('updateLayer', layerCopy) | ||
} catch (error) { | ||
log.error( | ||
`Error while fetching GeoJSON data/style for layer ${geoJsonLayer?.getID()}`, | ||
error | ||
) | ||
throw error | ||
} | ||
} | ||
|
||
/** | ||
* Load GeoJSON data and style whenever a GeoJSON layer is added (or does nothing if the layer was | ||
* already processed/loaded) | ||
* | ||
* @param {Vuex.Store} store | ||
*/ | ||
export default function loadGeojsonStyleAndData(store) { | ||
store.subscribe((mutation) => { | ||
if ( | ||
mutation.type === 'addLayer' && | ||
mutation.payload.layer instanceof GeoAdminGeoJsonLayer && | ||
mutation.payload.layer.isLoading | ||
) { | ||
log.debug(`Loading data/style for added GeoJSON layer`, mutation.payload.layer) | ||
loadDataAndStyle(store, mutation.payload.layer) | ||
} | ||
}) | ||
} |