From a4fbe55012118014b360c40bf1fdb8ed653b058b Mon Sep 17 00:00:00 2001 From: orangemug Date: Fri, 17 Nov 2017 11:43:56 +0000 Subject: [PATCH] Added type to sources list and now filtering in modal autocomplete. --- src/components/App.jsx | 9 ++++-- src/components/modals/AddModal.jsx | 52 +++++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/src/components/App.jsx b/src/components/App.jsx index 7ca7f2b..279f66d 100644 --- a/src/components/App.jsx +++ b/src/components/App.jsx @@ -188,7 +188,10 @@ export default class App extends React.Component { const sourceList = {}; for(let [key, val] of Object.entries(this.state.mapStyle.sources)) { - sourceList[key] = sourceList[key] || []; + sourceList[key] = { + type: val.type, + layers: [] + }; if(val.type === "vector") { const url = val.url; @@ -199,10 +202,10 @@ export default class App extends React.Component { .then((json) => { // Create new objects before setState const sourceList = {...this.state.sources}; - sourceList[key] = []; + sourceList[key] = {...sourceList[key]}; for(let layer of json.vector_layers) { - sourceList[key].push(layer.id) + sourceList[key].layers.push(layer.id) } this.setState({ diff --git a/src/components/modals/AddModal.jsx b/src/components/modals/AddModal.jsx index 24a4638..ab4a6de 100644 --- a/src/components/modals/AddModal.jsx +++ b/src/components/modals/AddModal.jsx @@ -56,18 +56,54 @@ class AddModal extends React.Component { } } - componentWillReceiveProps(nextProps) { - const sourceIds = Object.keys(nextProps.sources) - if(!this.state.source && sourceIds.length > 0) { + componentWillUpdate(nextProps, nextState) { + // Check if source is valid for new type + const availableSources = this.getSources(nextState.type); + if( + this.state.source !== "" + && availableSources.indexOf(this.state.source) < 0 + ) { this.setState({ - source: sourceIds[0], - 'source-layer': this.state['source-layer'] || (nextProps.sources[sourceIds[0]] || [])[0] - }) + source: "" + }); } } + getLayersForSource(source) { + const sourceObj = this.props.sources[source] || {}; + return sourceObj.layers || []; + } + + getSources(type) { + const sources = []; + + const types = { + vector: [ + "fill", + "line", + "symbol", + "circle", + "fill-extrusion" + ], + raster: [ + "raster" + ] + } + + for(let [key, val] of Object.entries(this.props.sources)) { + if(types[val.type].indexOf(type) > -1) { + sources.push(key); + } + } + + return sources; + } + render() { + const sources = this.getSources(this.state.type); + const layers = this.getLayersForSource(this.state.source); + return {this.state.type !== 'background' && this.setState({ source: v })} /> } {this.state.type !== 'background' && this.state.type !== 'raster' && this.setState({ 'source-layer': v })} />