Add JSON.parse error-handling when parsing from URL

This commit is contained in:
Tom Russell 2019-10-02 22:13:34 +01:00
parent 40891cd4ce
commit fa785a726c
2 changed files with 23 additions and 4 deletions

View File

@ -34,7 +34,20 @@ const MultiEdit = (props) => {
}
const q = parse(props.location.search);
const data = JSON.parse(q.data as string) // TODO: verify what happens when data is string[]
let data: object;
if (cat === 'like'){
data = { like: true }
} else {
try {
// TODO: verify what happens if data is string[]
data = JSON.parse(q.data as string);
} catch (error) {
console.error(error, q)
data = {}
}
}
const title = sectionTitleFromCat(cat);
return (
<Sidebar>

View File

@ -142,11 +142,17 @@ class MapApp extends React.Component<MapAppProps, MapAppState> {
colourBuilding(building) {
const cat = this.props.match.params.category;
const q = parse(window.location.search);
const data = (cat === 'like') ? { like: true } : JSON.parse(q.data as string); // TODO: verify what happens if data is string[]
if (cat === 'like') {
this.likeBuilding(building.building_id)
} else {
this.updateBuilding(building.building_id, data)
try {
// TODO: verify what happens if data is string[]
const data = JSON.parse(q.data as string);
this.updateBuilding(building.building_id, data)
} catch (error) {
console.error(error, q)
}
}
}
@ -245,4 +251,4 @@ class MapApp extends React.Component<MapAppProps, MapAppState> {
}
}
export default MapApp;
export default MapApp;