Skip to content

Commit

Permalink
Show all place names from url params (#2502)
Browse files Browse the repository at this point in the history
This is not the most cleanest way to do this in topic page. If we want
to support multiple places for all topic pages, might need to
re-structure the data fetch.
  • Loading branch information
shifucun committed Mar 28, 2023
1 parent ceb828a commit 6b5429f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
9 changes: 6 additions & 3 deletions server/routes/topic_page.py
Expand Up @@ -37,12 +37,11 @@
bp = flask.Blueprint('topic_page', __name__, url_prefix='/topic')


def get_sdg_config(place_dcid, topic_config):
def get_sdg_config(place_dcid, more_places, topic_config):
topic_place_config = topic_config
topic_place_config.metadata.place_dcid.append(place_dcid)
# Populuate comparison places (fixed places) for all tiles
comparison_places = [place_dcid]
more_places = request.args.getlist('places')
if more_places:
comparison_places.extend(more_places)
for p in _SDG_COMPARISON_PLACES:
Expand Down Expand Up @@ -95,10 +94,13 @@ def topic_page(topic_id=None, place_dcid=None):
config={},
topics_summary=topics_summary)

more_places = request.args.getlist('places')

# Find the config for the topic & place.
topic_place_config = None
if topic_id == _SDG_TOPIC:
topic_place_config = get_sdg_config(place_dcid, topic_configs[0])
topic_place_config = get_sdg_config(place_dcid, more_places,
topic_configs[0])
else:
for config in topic_configs:
if place_dcid in config.metadata.place_dcid:
Expand All @@ -119,6 +121,7 @@ def topic_page(topic_id=None, place_dcid=None):
place_type=place_type,
place_name=place_name,
place_dcid=place_dcid,
more_places=json.dumps(more_places),
topic_id=topic_id,
topic_name=topic_place_config.metadata.topic_name or "",
config=MessageToJson(topic_place_config),
Expand Down
2 changes: 1 addition & 1 deletion server/templates/topic_page.html
Expand Up @@ -31,7 +31,7 @@

{% block content %}
<div id="body" class="container-fluid"></div>
<div id="metadata" class="d-none" data-place-dcid="{{ place_dcid }}" data-topic-id="{{ topic_id }}">
<div id="metadata" class="d-none" data-place-dcid="{{ place_dcid }}" data-more-places="{{ more_places }}" data-topic-id="{{ topic_id }}">
<div id="place-name" data-pn="{{ place_name }}"></div>
<div id="place-type" data-pt="{{ place_type }}"></div>
<div id="locale" data-lc="{{ locale }}"></div>
Expand Down
6 changes: 5 additions & 1 deletion static/js/apps/topic_page/app.tsx
Expand Up @@ -18,7 +18,6 @@
* Main component for topic pages.
*/

import _ from "lodash";
import React from "react";

import { SubjectPageMainPane } from "../../components/subject_page/main_pane";
Expand All @@ -33,6 +32,10 @@ interface AppPropType {
* The place to show the page for.
*/
place: NamedTypedPlace;
/**
* A list of additional places for the topic page.
*/
morePlaces: string[];
/**
* The topic of the current page.
*/
Expand Down Expand Up @@ -62,6 +65,7 @@ export function App(props: AppPropType): JSX.Element {
<div className="row col-md-9x col-lg-10">
<PageSelector
selectedPlace={props.place}
morePlaces={props.morePlaces}
selectedTopic={props.topic}
topicsSummary={props.topicsSummary}
/>
Expand Down
4 changes: 4 additions & 0 deletions static/js/apps/topic_page/main.ts
Expand Up @@ -40,6 +40,9 @@ function renderPage(): void {
const topic = document.getElementById("metadata").dataset.topicId;
// TODO(beets): remove these if they remain unused.
const dcid = document.getElementById("metadata").dataset.placeDcid;
const morePlaces = JSON.parse(
document.getElementById("metadata").dataset.morePlaces
);
const placeName = document.getElementById("place-name").dataset.pn;
const placeType =
document.getElementById("place-type").dataset.pt || DEFAULT_PAGE_PLACE_TYPE;
Expand Down Expand Up @@ -67,6 +70,7 @@ function renderPage(): void {
ReactDOM.render(
React.createElement(App, {
place,
morePlaces,
topic,
pageConfig,
topicsSummary,
Expand Down
32 changes: 31 additions & 1 deletion static/js/apps/topic_page/page_selector.tsx
Expand Up @@ -29,6 +29,7 @@ import { getPlaceNames } from "../../utils/place_utils";

interface PageSelectorPropType {
selectedPlace: NamedTypedPlace;
morePlaces: string[];
selectedTopic: string;
topicsSummary: TopicsSummary;
}
Expand All @@ -38,19 +39,32 @@ export function PageSelector(props: PageSelectorPropType): JSX.Element {
Record<string, string> | undefined
>({});

const [morePlaces, setMorePlaces] = useState<NamedTypedPlace[] | undefined>(
[]
);

useEffect(() => {
getPlaceOptions(props.selectedTopic, props.topicsSummary, setPlaceOptions);
}, [props]);

useEffect(() => {
getMorePlaces(props.morePlaces, setMorePlaces);
}, [props]);

const topicName =
props.topicsSummary.topicNameMap[props.selectedTopic] ||
props.selectedTopic;

const allNames = [props.selectedPlace.name];
for (const item of morePlaces) {
allNames.push(item.name);
}

return (
<div className="page-selector-container">
<div className="page-selector-section">
<h1>
{topicName} in {props.selectedPlace.name}
{topicName} in {allNames.join(", ")}
</h1>
{/* <div>
<CustomInput
Expand Down Expand Up @@ -127,6 +141,22 @@ function getPlaceOptions(
});
}

function getMorePlaces(
placeDcids: string[],
setMorePlaces: (places: NamedTypedPlace[]) => void
): void {
getPlaceNames(placeDcids).then((placeNames) => {
const res: NamedTypedPlace[] = [];
for (const dcid in placeNames)
res.push({
dcid: dcid,
name: placeNames[dcid],
types: [],
});
setMorePlaces(res);
});
}

function selectPlace(
currentTopic: string,
event: React.ChangeEvent<HTMLInputElement>
Expand Down

0 comments on commit 6b5429f

Please sign in to comment.