Skip to content

Commit

Permalink
fix: error when trying to display map if no zip or lat/lon (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland authored Feb 7, 2024
1 parent 23e2dca commit aa0aa93
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 41 deletions.
32 changes: 18 additions & 14 deletions js/src/forum/components/ZipCodeMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,67 +21,71 @@ export default class ZipCodeMap extends Component {

this.data = null;

if (this.ipInfo.zipCode()) {
if (this.ipInfo.latitude() && this.ipInfo.longitude()) {
this.searchLatLon();
} else if (this.ipInfo.zipCode()) {
this.searchZip();
} else {
this.searchLatLon();
this.data = { unknown: true };
}
}

view() {
if (this.loading) {
return <LoadingIndicator size="medium" />;
} else if (this.data && this.data.unknown) {
return <div className="helpText">{app.translator.trans('fof-geoip.forum.map_modal.not_enough_data')}</div>;
} else if (!this.data) {
return <div />;
}

return <div id="geoip-map" oncreate={this.configMap.bind(this)} />;
}

searchZip() {
searchLatLon() {
if (this.loading) return;

this.loading = true;

return addResources().then(
app
.request({
url: `https://nominatim.openstreetmap.org/search`,
url: `https://nominatim.openstreetmap.org/reverse`,
method: 'GET',
params: {
q: this.ipInfo.zipCode(),
countrycodes: this.ipInfo.countryCode(),
limit: 1,
lat: this.ipInfo.latitude(),
lon: this.ipInfo.longitude(),
format: 'json',
},
})
.then((data) => {
this.data = data[0];
this.data = data;
this.loading = false;

m.redraw();
})
);
}

searchLatLon() {
searchZip() {
if (this.loading) return;

this.loading = true;

return addResources().then(
app
.request({
url: `https://nominatim.openstreetmap.org/reverse`,
url: `https://nominatim.openstreetmap.org/search`,
method: 'GET',
params: {
lat: this.ipInfo.latitude(),
lon: this.ipInfo.longitude(),
q: this.ipInfo.zipCode(),
countrycodes: this.ipInfo.countryCode(),
limit: 1,
format: 'json',
},
})
.then((data) => {
this.data = data;
this.data = data[0];
this.loading = false;

m.redraw();
Expand All @@ -100,6 +104,6 @@ export default class ZipCodeMap extends Component {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).addTo(this.map);

L.marker([bounding[0], bounding[2]]).addTo(this.map).bindPopup(displayName).openPopup();
L.marker([bounding[0], bounding[2]]).addTo(this.map).openPopup();
}
}
28 changes: 13 additions & 15 deletions js/src/forum/extenders/extendPostMeta.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function extendPostMeta() {
const ipAddr = post.data.attributes.ipAddress;

if (ipInformation && ipAddr) {
const { description, threat, image, zip, country } = getIPData(ipInformation);
const { description, threat, image } = getIPData(ipInformation);

items.add(
'ipInfo',
Expand All @@ -59,20 +59,18 @@ export default function extendPostMeta() {
100
);

if (zip && country) {
items.add(
'mapButton',
<Tooltip text={app.translator.trans('fof-geoip.forum.map_button_label')}>
<Button
icon="fas fa-map-marker-alt"
className="Button Button--icon Button--link"
onclick={() => app.modal.show(MapModal, { ipInfo: ipInformation, ipAddr: ipAddr })}
aria-label={app.translator.trans('fof-geoip.forum.map_button_label')}
/>
</Tooltip>,
90
);
}
items.add(
'mapButton',
<Tooltip text={app.translator.trans('fof-geoip.forum.map_button_label')}>
<Button
icon="fas fa-map-marker-alt"
className="Button Button--icon Button--link"
onclick={() => app.modal.show(MapModal, { ipInfo: ipInformation, ipAddr: ipAddr })}
aria-label={app.translator.trans('fof-geoip.forum.map_button_label')}
/>
</Tooltip>,
90
);
}

return items;
Expand Down
27 changes: 15 additions & 12 deletions js/src/forum/models/IPInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,46 @@ export default class IPInfo extends Model {
}

countryCode() {
return Model.attribute<string>('countryCode').call(this);
return Model.attribute<string | null>('countryCode').call(this);
}

zipCode() {
return Model.attribute<string>('zipCode').call(this);
return Model.attribute<string | null>('zipCode').call(this);
}

latitude() {
return Model.attribute<number>('latitude').call(this);
return Model.attribute<number | null>('latitude').call(this);
}

longitude() {
return Model.attribute<number>('longitude').call(this);
return Model.attribute<number | null>('longitude').call(this);
}

isp() {
return Model.attribute<string>('isp').call(this);
return Model.attribute<string | null>('isp').call(this);
}

organization() {
return Model.attribute<string>('organization').call(this);
return Model.attribute<string | null>('organization').call(this);
}

as() {
return Model.attribute<string>('as').call(this);
return Model.attribute<string | null>('as').call(this);
}

mobile() {
return Model.attribute<boolean>('mobile').call(this);
return Model.attribute<boolean | null>('mobile').call(this);
}

threatLevel() {
return Model.attribute<string>('threatLevel').call(this);
return Model.attribute<string | null>('threatLevel').call(this);
}

threatTypes() {
const raw = Model.attribute<string>('threatTypes').call(this);
const raw = Model.attribute<string | null>('threatTypes').call(this);
if (!raw) {
return [];
}
try {
return JSON.parse(raw);
} catch (error) {
Expand All @@ -51,11 +54,11 @@ export default class IPInfo extends Model {
}

error() {
return Model.attribute<string>('error').call(this);
return Model.attribute<string | null>('error').call(this);
}

dataProvider() {
return Model.attribute<string>('dataProvider').call(this);
return Model.attribute<string | null>('dataProvider').call(this);
}

createdAt() {
Expand Down
1 change: 1 addition & 0 deletions resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ fof-geoip:
threat_level: "Threat Level"
threat_types: "Threat Types"
error: "Error"
not_enough_data: "Not enough data to draw a map"

0 comments on commit aa0aa93

Please sign in to comment.