Skip to content

Commit 6307281

Browse files
authored
[fix] fixes for style editor preview and error (#2919)
- fix style preview - take into account basemap library switching in add-map-style-modal - trigger new Map components with url, token or library change in order to clear cache of loaded styles. Otherwise 'style.load' events aren't triggered when input changes from correct > wrong > correct config. Signed-off-by: Ihor Dykhta <[email protected]>
1 parent d6aa275 commit 6307281

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

src/components/src/modal-container.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,10 @@ export default function ModalContainerFactory(
466466
onConfirm: this._onAddCustomMapStyle,
467467
confirmButton: {
468468
large: true,
469-
disabled: !mapStyle.inputStyle.url || !mapStyle.inputStyle.label,
469+
disabled:
470+
mapStyle.inputStyle.error ||
471+
!mapStyle.inputStyle.url ||
472+
!mapStyle.inputStyle.label,
470473
children: 'modal.button.addStyle'
471474
}
472475
};

src/components/src/modals/add-map-style-modal.tsx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const PreviewMap = styled.div`
4646
4747
.preview-title.error {
4848
color: ${props => props.theme.errorColor};
49+
max-width: 250px;
4950
}
5051
5152
${media.portable`
@@ -89,6 +90,10 @@ const InlineLink = styled.a`
8990
}
9091
`;
9192

93+
const nop = () => {
94+
return;
95+
};
96+
9297
interface AddMapStyleModalProps {
9398
inputMapStyle: ActionHandler<typeof inputMapStyle>;
9499
inputStyle: InputStyle;
@@ -131,11 +136,20 @@ function AddMapStyleModalFactory() {
131136
return null;
132137
}
133138

134-
mapRef: MapRef | null | undefined;
135-
_map: MapboxMap | undefined;
139+
_map: MapboxMap | undefined | null;
140+
141+
_setMapRef = (mapRef: MapRef) => {
142+
// Handle change of the basemap library
143+
if (this._map && mapRef) {
144+
const map = mapRef.getMap();
145+
if (map && this._map !== map) {
146+
this._map.off('style.load', nop);
147+
this._map.off('error', nop);
148+
this._map = null;
149+
}
150+
}
136151

137-
componentDidUpdate() {
138-
const map = this.mapRef && this.mapRef.getMap();
152+
const map = mapRef && mapRef.getMap();
139153
if (map && this._map !== map) {
140154
this._map = map;
141155

@@ -148,7 +162,7 @@ function AddMapStyleModalFactory() {
148162
this.loadMapStyleError();
149163
});
150164
}
151-
}
165+
};
152166

153167
loadMapStyleJson = style => {
154168
this.props.loadCustomMapStyle({style, error: false});
@@ -280,16 +294,15 @@ function AddMapStyleModalFactory() {
280294
: (inputStyle.style && inputStyle.style.name) || ''}
281295
</div>
282296
<StyledPreviewImage className="preview-image">
297+
{/** Note, we need the Map to render with errored params to get style.error messages */}
283298
{!inputStyle.isValid ? (
284299
<div className="preview-image-spinner" />
285300
) : (
286301
<StyledMapContainer>
287302
<Map
288303
{...mapProps}
289-
ref={el => {
290-
this.mapRef = el;
291-
}}
292-
key={this.state.reRenderKey}
304+
ref={this._setMapRef}
305+
key={`${baseMapLibraryName}-${this.state.reRenderKey}-${inputStyle.url}-${mapboxApiAccessToken}`}
293306
style={{
294307
width: MapW,
295308
height: MapH

src/reducers/src/map-style-updaters.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ export const loadCustomMapStyleUpdater = (
676676
}
677677
: {}),
678678
...(icon ? {icon} : {}),
679-
...(error ? {error} : {})
679+
...(error !== undefined ? {error} : {})
680680
}
681681
});
682682

@@ -697,7 +697,6 @@ export const inputMapStyleUpdater = (
697697
// differentiate between either a url to hosted style json that needs an icon url,
698698
// or an icon already available client-side as a data uri
699699
const isUpdatedIconDataUri = updated.icon?.startsWith('data:image');
700-
const isValid = Boolean(updated.uploadedFile);
701700
const isMapboxStyleUrl =
702701
updated.url?.startsWith('mapbox://') || updated.url?.includes('mapbox.com');
703702

@@ -716,7 +715,7 @@ export const inputMapStyleUpdater = (
716715
...state,
717716
inputStyle: {
718717
...updated,
719-
isValid,
718+
isValid: true,
720719
icon
721720
}
722721
};

test/node/reducers/map-style-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ test('#mapStyleReducer -> EDIT_CUSTOM_MAP_STYLE', t => {
839839

840840
t.deepEqual(
841841
nextState.inputStyle,
842-
{...inputMapStyleToEdit, error: false, isValid: false, style: null, uploadedFile: null},
842+
{...inputMapStyleToEdit, error: false, isValid: true, style: null, uploadedFile: null},
843843
'should set the inputStyle'
844844
);
845845

0 commit comments

Comments
 (0)