Skip to content

Commit

Permalink
WIP: unloading place popup with data from olmap
Browse files Browse the repository at this point in the history
  • Loading branch information
tuukka committed Dec 15, 2021
1 parent 795b1e1 commit 98896c9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
19 changes: 9 additions & 10 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,10 @@ const App: React.FC = () => {
};
});
// Fetch new data
const olmapData = await fetchOlmapData(state.popupCoordinates.id);
const olmapData =
state.popupCoordinates.type === "olmap"
? await fetchOlmapData(undefined, state.popupCoordinates.id)
: await fetchOlmapData(state.popupCoordinates.id);
setState((prevState: State): State => {
if (prevState.popupCoordinates !== state.popupCoordinates) {
return prevState;
Expand Down Expand Up @@ -867,15 +870,11 @@ const App: React.FC = () => {
event.lngLat.lng,
]);

// If an OLMap element was clicked, show details in the popup.
if (feature?.properties["@id"]?.startsWith("olmap")) {
return {
...prevState,
editingNote: feature.properties["@id"].split("/").reverse()[0],
};
}
// If an entrance was clicked, show details in the popup.
if (feature?.properties.entrance) {
// If an entrance or an OLMap element was clicked, show details in the popup.
if (
feature?.properties.entrance ||
feature?.properties["@id"]?.startsWith("olmap")
) {
const element = geoJsonToElement(feature);
return {
...prevState,
Expand Down
41 changes: 32 additions & 9 deletions src/olmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,21 @@ const processOlmapData = (data: OlmapResponse): OlmapResponse => {
deliveryTypePriorities[a.deliveries || "null"]
);
}
if (!data.image_notes) {
return {
image_notes: [data as unknown as OlmapNote],
id: data.id,
associated_entrances: [],
};
}
return data;
};

export const fetchOlmapData = async (
osmId: number
): Promise<NetworkState<OlmapResponse> | undefined> => {
if (osmId === -1) {
return undefined;
}
const fetchOlmapUrl = async (
url: string
): Promise<NetworkState<OlmapResponse>> => {
try {
const response = await fetch(
`https://api.olmap.org/rest/osm_features/${osmId}/`
);
const response = await fetch(url);
try {
const data = await response.json();
if (!response.ok) {
Expand Down Expand Up @@ -161,6 +163,27 @@ export const fetchOlmapData = async (
}
};

export const fetchOlmapData = async (
osmId?: number,
olmapId?: number
): Promise<NetworkState<OlmapResponse> | undefined> => {
try {
if (olmapId) {
return await fetchOlmapUrl(
`https://api.olmap.org/rest/osm_image_notes/${olmapId}/`
);
}
if (!osmId || osmId === -1) {
return undefined;
}
return await fetchOlmapUrl(
`https://api.olmap.org/rest/osm_features/${osmId}/`
);
} finally {
// no-op
}
};

export const venueDataToGeoJSON = (
venueData: NetworkState<OlmapResponse>,
osmData: Array<ElementWithCoordinates>
Expand Down

0 comments on commit 98896c9

Please sign in to comment.