From 0aa629164a1ea89af98f974a6fcdfc65a8b7fe06 Mon Sep 17 00:00:00 2001 From: orangemug Date: Tue, 19 May 2020 10:24:59 +0100 Subject: [PATCH 1/2] Fix for updating available sources when updating style. --- src/components/App.jsx | 67 +++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/src/components/App.jsx b/src/components/App.jsx index 02b2212a..6923288c 100644 --- a/src/components/App.jsx +++ b/src/components/App.jsx @@ -440,9 +440,10 @@ export default class App extends React.Component { mapStyle: newStyle, dirtyMapStyle: dirtyMapStyle, errors: mappedErrors, + }, () => { + this.fetchSources(); }) - this.fetchSources(); } onUndo = () => { @@ -566,19 +567,19 @@ export default class App extends React.Component { } fetchSources() { - const sourceList = {...this.state.sources}; + const sourceList = {}; for(let [key, val] of Object.entries(this.state.mapStyle.sources)) { - if(sourceList.hasOwnProperty(key)) { - continue; - } + if( + !this.state.sources.hasOwnProperty(key) && + val.type === "vector" && + val.hasOwnProperty("url") + ) { + sourceList[key] = { + type: val.type, + layers: [] + }; - sourceList[key] = { - type: val.type, - layers: [] - }; - - if(!this.state.sources.hasOwnProperty(key) && val.type === "vector" && val.hasOwnProperty("url")) { let url = val.url; try { url = normalizeSourceURL(url, MapboxGl.accessToken); @@ -595,29 +596,33 @@ export default class App extends React.Component { fetch(url, { mode: 'cors', }) - .then((response) => { - return response.json(); - }) - .then((json) => { - if(!json.hasOwnProperty("vector_layers")) { - return; - } + .then(response => response.json()) + .then(json => { - // Create new objects before setState - const sources = Object.assign({}, this.state.sources); + if(!json.hasOwnProperty("vector_layers")) { + return; + } - for(let layer of json.vector_layers) { - sources[key].layers.push(layer.id) - } + // Create new objects before setState + const sources = Object.assign({}, { + [key]: this.state.sources[key], + }); - console.debug("Updating source: "+key); - this.setState({ - sources: sources - }); - }) - .catch((err) => { - console.error("Failed to process sources for '%s'", url, err); - }) + for(let layer of json.vector_layers) { + sources[key].layers.push(layer.id) + } + + console.debug("Updating source: "+key); + this.setState({ + sources: sources + }); + }) + .catch(err => { + console.error("Failed to process sources for '%s'", url, err); + }); + } + else { + sourceList[key] = this.state.sources[key] || this.state.mapStyle.sources[key]; } } From 5448cdbe4ef2cf892163646ae19e30ebd55ec6dd Mon Sep 17 00:00:00 2001 From: orangemug Date: Thu, 21 May 2020 10:07:22 +0100 Subject: [PATCH 2/2] Don't remember state when toggling --- src/components/layers/LayerList.jsx | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/components/layers/LayerList.jsx b/src/components/layers/LayerList.jsx index f7bf9ef6..c72f92bc 100644 --- a/src/components/layers/LayerList.jsx +++ b/src/components/layers/LayerList.jsx @@ -35,6 +35,8 @@ function findClosestCommonPrefix(layers, idx) { return closestIdx } +let UID = 0; + // List of collapsible layer editors class LayerListContainer extends React.Component { static propTypes = {...layerListPropTypes} @@ -49,6 +51,9 @@ class LayerListContainer extends React.Component { this.state = { collapsedGroups: {}, areAllGroupsExpanded: false, + keys: { + add: UID++, + }, isOpen: { add: false, } @@ -57,6 +62,10 @@ class LayerListContainer extends React.Component { toggleModal(modalName) { this.setState({ + keys: { + ...this.state.keys, + [modalName]: UID++, + }, isOpen: { ...this.state.isOpen, [modalName]: !this.state.isOpen[modalName] @@ -253,6 +262,7 @@ class LayerListContainer extends React.Component { return