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

2926: Fallback language in search function #3024

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
2926: Fix tests
  • Loading branch information
LeandraH committed Dec 2, 2024
commit 0245b3f506892c7447cd9d481f3378c4d2b20698
15 changes: 11 additions & 4 deletions native/src/routes/SearchModalContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { ReactElement } from 'react'
import React, { ReactElement, useMemo } from 'react'

import { SearchRouteType } from 'shared'
import { useFormatPossibleSearchResults } from 'shared/hooks/useSearch'
import { CategoriesMapModel, EventModel, PoiModel } from 'shared/api'
import { formatPossibleSearchResults } from 'shared/hooks/useSearch'
import { config } from 'translations'

import { NavigationProps, RouteProps } from '../constants/NavigationTypes'
@@ -15,15 +16,21 @@ export type SearchModalContainerProps = {
route: RouteProps<SearchRouteType>
}

const useMemoizeResults = (
categories?: CategoriesMapModel | null,
events?: EventModel[] | null,
pois?: PoiModel[] | null,
) => useMemo(() => formatPossibleSearchResults(categories, events, pois), [categories, events, pois])
LeandraH marked this conversation as resolved.
Show resolved Hide resolved

const SearchModalContainer = ({ navigation, route }: SearchModalContainerProps): ReactElement | null => {
const { cityCode, languageCode } = useCityAppContext()
const initialSearchText = route.params.searchText ?? ''
const { data, ...response } = useLoadCityContent({ cityCode, languageCode })
const { data: fallbackData } = useLoadCityContent({ cityCode, languageCode: config.sourceLanguage })

const allPossibleResults = useFormatPossibleSearchResults(data?.categories, data?.events, data?.pois)
const allPossibleResults = useMemoizeResults(data?.categories, data?.events, data?.pois)

const allPossibleFallbackResults = useFormatPossibleSearchResults(
const allPossibleFallbackResults = useMemoizeResults(
fallbackData?.categories,
fallbackData?.events,
fallbackData?.pois,
1 change: 1 addition & 0 deletions native/src/routes/__tests__/SearchModal.spec.tsx
Original file line number Diff line number Diff line change
@@ -55,6 +55,7 @@ describe('SearchModal', () => {

const props: SearchModalProps = {
allPossibleResults,
allPossibleFallbackResults: [],
languageCode,
cityCode,
closeModal: dummy,
17 changes: 10 additions & 7 deletions shared/hooks/useSearch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MiniSearch from 'minisearch'
import { useCallback, useMemo } from 'react'
import { useCallback } from 'react'

import useLoadAsync from '../api/endpoints/hooks/useLoadAsync'
import CategoriesMapModel from '../api/models/CategoriesMapModel'
@@ -9,15 +9,18 @@ import PoiModel from '../api/models/PoiModel'

export type SearchResult = ExtendedPageModel

export const useFormatPossibleSearchResults = (
// TODO: turn it back into a memoized function
// TODO: mock it in the web test
// TODO: maybe test this in shared
export const formatPossibleSearchResults = (
categories?: CategoriesMapModel | null,
events?: EventModel[] | null,
pois?: PoiModel[] | null,
): SearchResult[] =>
useMemo(
() => [...(categories?.toArray().filter(category => !category.isRoot()) || []), ...(events || []), ...(pois || [])],
[categories, events, pois],
)
): SearchResult[] => [
...(categories?.toArray().filter(category => !category.isRoot()) || []),
...(events || []),
...(pois || []),
]

const useSearch = (allPossibleResults: SearchResult[], query: string): SearchResult[] | null => {
const initializeMiniSearch = useCallback(async () => {
3 changes: 0 additions & 3 deletions web/src/hooks/__tests__/useAllPossibleSearchResults.spec.ts
Original file line number Diff line number Diff line change
@@ -27,11 +27,8 @@ describe('useAllPossibleSearchResults', () => {

const city = new CityModelBuilder(1).build()[0]!.code
const language = new LanguageModelBuilder(1).build()[0]!.code

const categories = new CategoriesMapModelBuilder(city, language).build()

const events = new EventModelBuilder('seed', 2, city, language).build()

const locations = new PoiModelBuilder(3).build()

it('should return the correct results', () => {
9 changes: 7 additions & 2 deletions web/src/hooks/useAllPossibleSearchResults.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useMemo } from 'react'

import {
createCategoriesEndpoint,
createEventsEndpoint,
createPOIsEndpoint,
ExtendedPageModel,
useLoadFromEndpoint,
} from 'shared/api'
import { useFormatPossibleSearchResults } from 'shared/hooks/useSearch'
import { formatPossibleSearchResults } from 'shared/hooks/useSearch'

type UseAllPossibleSearchResultsProps = {
city: string
@@ -30,7 +32,10 @@ const useAllPossibleSearchResults = ({
const events = useLoadFromEndpoint(createEventsEndpoint, cmsApiBaseUrl, params)
const pois = useLoadFromEndpoint(createPOIsEndpoint, cmsApiBaseUrl, params)

const allPossibleResults = useFormatPossibleSearchResults(categories.data, events.data, pois.data)
const allPossibleResults = useMemo(
() => formatPossibleSearchResults(categories.data, events.data, pois.data),
[categories.data, events.data, pois.data],
)

return {
data: allPossibleResults,