Skip to content

Commit

Permalink
chore: getContent callback is now async
Browse files Browse the repository at this point in the history
  • Loading branch information
wmai committed Sep 15, 2023
1 parent 465095f commit 986543a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
20 changes: 13 additions & 7 deletions packages/visualizations-react/stories/Poi/PoiMap.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import React from 'react';
import { BBox } from 'geojson';
import { Layer } from '@opendatasoft/visualizations';
import { ComponentMeta, ComponentStory } from '@storybook/react';

import type { Layer } from '@opendatasoft/visualizations';
import { PopupDisplayTypes } from '@opendatasoft/visualizations';

import sources from './sources';
import { PoiMap } from '../../src';

Expand All @@ -14,10 +16,12 @@ const layer1 : Layer = {
type: "circle",
color: 'black',
popup: {
display: 'tooltip',
getContent: (_, properties) => {
display: PopupDisplayTypes.Tooltip,
getContent: async (_, properties) => {
const {key} = properties as {key: string};
return `<h4>${key}</h4>`;}
return Promise.resolve(`<h4>${key}</h4>`);
},
getLoadingContent: () => 'Loading...',
}
};

Expand All @@ -27,10 +31,12 @@ const layer2 : Layer = {
type: "circle",
color: 'red',
popup: {
display: 'sidebar',
getContent: (_, properties) => {
display: PopupDisplayTypes.Sidebar,
getContent: async (_, properties) => {
const {name, date, description} = properties as {name: string, date: string, description: string};
return `<h4>${name}</h4><p>${description}<p/><small>${date}</small>`;}
return Promise.resolve(`<h4>${name}</h4><p>${description}<p/><small>${date}</small>`);
},
getLoadingContent: () => 'Loading...',
}
};

Expand Down
17 changes: 12 additions & 5 deletions packages/visualizations/src/components/MapPoi/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,18 @@ export default class MapPOI {
return;
}

const { display, getContent } = popupConfiguration;
this.popup
.setLngLat(geometry.coordinates.slice() as LngLatLike)
.setHTML(getContent(featureId, properties))
.addTo(map);
const { display, getContent, getLoadingContent } = popupConfiguration;

if (this.popup.isOpen() === false) {
this.popup.setLngLat(geometry.coordinates.slice() as LngLatLike).addTo(map);
}

this.popup.setHTML(getLoadingContent());

getContent(featureId, properties)
.then((content) => {
this.popup.setHTML(content);
});

const classnameModifier = display === 'sidebar' ? 'addClassName' : 'removeClassName';
this.popup[classnameModifier](`${POPUP_OPTIONS.className}--as-sidebar`);
Expand Down
10 changes: 8 additions & 2 deletions packages/visualizations/src/components/MapPoi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,20 @@ export type Layer = {
};
};

export enum PopupDisplayTypes {
Tooltip = 'tooltip',
Sidebar = 'sidebar',
}

export type PopupLayer = {
/**
* Control where to display the popup
* - `sidebar`: As a side element (on the left)
* - `tooltip`: Above the feature that has been clicked
*/
display: 'sidebar' | 'tooltip';
getContent: (id: GeoJSONFeature['id'], properties?: GeoJsonProperties) => string;
display: PopupDisplayTypes;
getContent: (id?: GeoJSONFeature['id'], properties?: GeoJsonProperties) => Promise<string>;
getLoadingContent: () => string;
};

export type GeoPoint = {
Expand Down

0 comments on commit 986543a

Please sign in to comment.