Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep the user's query in the NL search bar when landing on the place page #4796

Merged
merged 7 commits into from
Dec 13, 2024
8 changes: 6 additions & 2 deletions server/webdriver/shared_tests/place_explorer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,14 @@ def test_explorer_redirect_place_explorer(self):
# Assert 200 HTTP code: successful page load.
self.assertEqual(shared.safe_url_open(self.driver.current_url), 200)

# Assert page title is correct.
# Assert page title is correct, and that the query string is set in the url.
WebDriverWait(self.driver, self.TIMEOUT_SEC).until(
EC.title_contains('United States of America'))
self.assertTrue("place/country/USA" in self.driver.current_url)
self.assertTrue("place/country/USA?q=United%20States%20Of%20America" in self.driver.current_url)

# Ensure the query string is set in the NL Search Bar.
search_bar = self.driver.find_element(By.ID, "query-search-input")
self.assertEqual(search_bar.get_attribute("value"), "United States Of America")

def test_ranking_chart_present(self):
"""Test basic ranking chart."""
Expand Down
13 changes: 12 additions & 1 deletion static/js/apps/base/components/header_bar/header_bar_search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,24 @@ interface HeaderBarSearchProps {
gaValueSearchSource?: string;
}

// The search query param name. Used to pre-populate the search bar.
const QUERY_PARAM = "q";

const HeaderBarSearch = ({
inputId = "query-search-input",
searchBarHashMode,
gaValueSearchSource,
}: HeaderBarSearchProps): ReactElement => {
const { queryString, placeholder, queryResult, debugData } = useQueryStore();

// Get the query string from the url params.
const urlParams = new URLSearchParams(window.location.search);
const urlQuery = urlParams.get(QUERY_PARAM) || "";

// If the search bar is in hash mode, use the query string from the url params.
// Otherwise, use the query string from the query store.
const initialValue = searchBarHashMode ? queryString : urlQuery;

return (
<div className="header-search">
<NlSearchBar
Expand Down Expand Up @@ -75,7 +86,7 @@ const HeaderBarSearch = ({
}
}}
placeholder={placeholder}
initialValue={queryString}
initialValue={initialValue}
shouldAutoFocus={false}
/>
<DebugInfo debugData={debugData} queryResult={queryResult}></DebugInfo>
Expand Down
22 changes: 17 additions & 5 deletions static/js/apps/explore/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,18 @@ export function App(props: AppProps): ReactElement {
return hasPlace || fulfillData["entities"];
}

function processFulfillData(fulfillData: any, shouldSetQuery: boolean): void {
/**
* Process the fulfill data from the search API response.
*
* This processes the fulfill data by setting up page metadata, debug data, and user
* messages for rendering the explore page. However, if the fulfill response only
* contains place information, a page overview configuration, but no charts, it will
* redirect to /place/{placeDcid} instead.
*
* @param fulfillData The fulfill data from the search API response
* @param userQuery The user's search query
*/
function processFulfillData(fulfillData: any, userQuery?: string): void {
setDebugData(fulfillData["debug"]);
setStoreDebugData(fulfillData["debug"]);
const userMessage = {
Expand Down Expand Up @@ -238,7 +249,8 @@ export function App(props: AppProps): ReactElement {
isPendingRedirect = shouldSkipPlaceOverview(pageMetadata);
if (isPendingRedirect) {
const placeDcid = pageMetadata.place.dcid;
const url = `/place/${placeDcid}`;
// If the user has a query, append it to the url
const url = `/place/${placeDcid}${userQuery ? `?q=${userQuery}` : ""}`;
window.location.replace(url);
}
// Note: for category links, we only use the main-topic.
Expand All @@ -253,7 +265,7 @@ export function App(props: AppProps): ReactElement {
}
}
if (
shouldSetQuery &&
!userQuery &&
!_.isEmpty(pageMetadata.mainTopics) &&
pageMetadata.place.name
) {
Expand Down Expand Up @@ -347,7 +359,7 @@ export function App(props: AppProps): ReactElement {
includeStopWords
)
.then((resp) => {
processFulfillData(resp, false);
processFulfillData(resp, query);
})
.catch(() => {
setLoadingStatus(LoadingStatus.FAILED);
Expand All @@ -371,7 +383,7 @@ export function App(props: AppProps): ReactElement {
client
)
.then((resp) => {
processFulfillData(resp, true);
processFulfillData(resp);
})
.catch(() => {
setLoadingStatus(LoadingStatus.FAILED);
Expand Down
Loading