mirror of
https://github.com/maputnik/editor.git
synced 2026-08-27 15:37:27 +00:00
Fix to add error notice when uploading invalid JSON (issue #54)
This commit is contained in:
@@ -46,7 +46,20 @@ class OpenModal extends React.Component {
|
|||||||
onStyleOpen: React.PropTypes.func.isRequired,
|
onStyleOpen: React.PropTypes.func.isRequired,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
clearError() {
|
||||||
|
this.setState({
|
||||||
|
error: null
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
onStyleSelect(styleUrl) {
|
onStyleSelect(styleUrl) {
|
||||||
|
this.clearError();
|
||||||
|
|
||||||
request({
|
request({
|
||||||
url: styleUrl,
|
url: styleUrl,
|
||||||
withCredentials: false,
|
withCredentials: false,
|
||||||
@@ -64,9 +77,21 @@ class OpenModal extends React.Component {
|
|||||||
onUpload(_, files) {
|
onUpload(_, files) {
|
||||||
const [e, file] = files[0];
|
const [e, file] = files[0];
|
||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
|
|
||||||
|
this.clearError();
|
||||||
|
|
||||||
reader.readAsText(file, "UTF-8");
|
reader.readAsText(file, "UTF-8");
|
||||||
reader.onload = e => {
|
reader.onload = e => {
|
||||||
let mapStyle = JSON.parse(e.target.result)
|
let mapStyle;
|
||||||
|
try {
|
||||||
|
mapStyle = JSON.parse(e.target.result)
|
||||||
|
}
|
||||||
|
catch(err) {
|
||||||
|
this.setState({
|
||||||
|
error: err.toString()
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
mapStyle = style.ensureStyleValidity(mapStyle)
|
mapStyle = style.ensureStyleValidity(mapStyle)
|
||||||
this.props.onStyleOpen(mapStyle);
|
this.props.onStyleOpen(mapStyle);
|
||||||
}
|
}
|
||||||
@@ -84,11 +109,22 @@ class OpenModal extends React.Component {
|
|||||||
/>
|
/>
|
||||||
})
|
})
|
||||||
|
|
||||||
|
let errorElement;
|
||||||
|
if(this.state.error) {
|
||||||
|
errorElement = (
|
||||||
|
<div className="maputnik-modal-error">
|
||||||
|
{this.state.error}
|
||||||
|
<a href="#" onClick={() => this.clearError()} className="maputnik-modal-error-close">×</a>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return <Modal
|
return <Modal
|
||||||
isOpen={this.props.isOpen}
|
isOpen={this.props.isOpen}
|
||||||
onOpenToggle={this.props.onOpenToggle}
|
onOpenToggle={this.props.onOpenToggle}
|
||||||
title={'Open Style'}
|
title={'Open Style'}
|
||||||
>
|
>
|
||||||
|
{errorElement}
|
||||||
<section className="maputnik-modal-section">
|
<section className="maputnik-modal-section">
|
||||||
<h2>Upload Style</h2>
|
<h2>Upload Style</h2>
|
||||||
<p>Upload a JSON style from your computer.</p>
|
<p>Upload a JSON style from your computer.</p>
|
||||||
|
|||||||
@@ -199,3 +199,18 @@
|
|||||||
color: $color-lowgray;
|
color: $color-lowgray;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.maputnik-modal-error {
|
||||||
|
border: solid 2px #EF5350;
|
||||||
|
color: #EF5350;
|
||||||
|
padding: 8px;
|
||||||
|
padding-right: 32px;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
.maputnik-modal-error-close {
|
||||||
|
position: absolute;
|
||||||
|
right: 8px;
|
||||||
|
text-decoration: none;
|
||||||
|
color: #ef5350;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user