mirror of
https://github.com/a-nyx/maputnik-with-pmtiles.git
synced 2025-01-30 22:35:31 +01:00
Also validate on constructor
This commit is contained in:
parent
a51442921a
commit
f0371b41b1
1 changed files with 36 additions and 26 deletions
|
@ -3,6 +3,34 @@ import PropTypes from 'prop-types'
|
||||||
import StringInput from './StringInput'
|
import StringInput from './StringInput'
|
||||||
import SmallError from '../util/SmallError'
|
import SmallError from '../util/SmallError'
|
||||||
|
|
||||||
|
|
||||||
|
function validate (url) {
|
||||||
|
let error;
|
||||||
|
const getProtocol = (url) => {
|
||||||
|
try {
|
||||||
|
const urlObj = new URL(url);
|
||||||
|
return urlObj.protocol;
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const protocol = getProtocol(url);
|
||||||
|
if (
|
||||||
|
protocol &&
|
||||||
|
protocol === "http:" &&
|
||||||
|
window.location.protocol === "https:"
|
||||||
|
) {
|
||||||
|
error = (
|
||||||
|
<SmallError>
|
||||||
|
CORs policy won't allow fetching resources served over http from https, use a <code>https://</code> domain
|
||||||
|
</SmallError>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
class UrlInput extends React.Component {
|
class UrlInput extends React.Component {
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
"data-wd-key": PropTypes.string,
|
"data-wd-key": PropTypes.string,
|
||||||
|
@ -14,35 +42,17 @@ class UrlInput extends React.Component {
|
||||||
required: PropTypes.bool,
|
required: PropTypes.bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
state = {
|
constructor (props) {
|
||||||
error: null,
|
super(props);
|
||||||
|
this.state = {
|
||||||
|
error: validate(props.value)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
onInput = (url) => {
|
onInput = (url) => {
|
||||||
let error;
|
this.setState({
|
||||||
const getProtocol = (url) => {
|
error: validate(url)
|
||||||
try {
|
});
|
||||||
const urlObj = new URL(url);
|
|
||||||
return urlObj.protocol;
|
|
||||||
}
|
|
||||||
catch (err) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
const protocol = getProtocol(url);
|
|
||||||
if (
|
|
||||||
protocol &&
|
|
||||||
protocol === "http:" &&
|
|
||||||
window.location.protocol === "https:"
|
|
||||||
) {
|
|
||||||
error = (
|
|
||||||
<SmallError>
|
|
||||||
CORs policy won't allow fetching resources served over http from https, use a <code>https://</code> domain
|
|
||||||
</SmallError>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.setState({error});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
|
|
Loading…
Reference in a new issue