Skip to content

Commit

Permalink
2926: Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
LeandraH committed Jan 7, 2025
1 parent 5b6bf61 commit 17cbbd2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 33 deletions.
14 changes: 7 additions & 7 deletions native/src/routes/SearchModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ const SearchCounter = styled.Text`
`

export type SearchModalProps = {
allPossibleResults: SearchResult[]
allPossibleFallbackResults: SearchResult[]
allPossibleContentLanguageResults: SearchResult[]
allPossibleFallbackLanguageResults: SearchResult[]
languageCode: string
cityCode: string
closeModal: (query: string) => void
initialSearchText: string
}

const SearchModal = ({
allPossibleResults,
allPossibleFallbackResults,
allPossibleContentLanguageResults,
allPossibleFallbackLanguageResults,
languageCode,
cityCode,
closeModal,
Expand All @@ -48,10 +48,10 @@ const SearchModal = ({
const resourceCache = useResourceCache({ cityCode, languageCode })
const { t } = useTranslation('search')

const mainResults = useSearch(allPossibleResults, query)
const fallbackResults = useSearch(allPossibleFallbackResults, query)
const contentLanguageResults = useSearch(allPossibleContentLanguageResults, query)
const fallbackLanguageResults = useSearch(allPossibleFallbackLanguageResults, query)

const searchResults = mainResults?.length === 0 ? fallbackResults : mainResults
const searchResults = contentLanguageResults?.length === 0 ? fallbackLanguageResults : contentLanguageResults

if (!searchResults) {
return null
Expand Down
17 changes: 5 additions & 12 deletions native/src/routes/SearchModalContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { ReactElement, useMemo } from 'react'
import React, { ReactElement } from 'react'

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

Expand All @@ -16,21 +15,15 @@ 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])

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 = useMemoizeResults(data?.categories, data?.events, data?.pois)
const allPossibleContentLanguageResults = formatPossibleSearchResults(data?.categories, data?.events, data?.pois)

const allPossibleFallbackResults = useMemoizeResults(
const allPossibleFallbackLanguageResults = formatPossibleSearchResults(
fallbackData?.categories,
fallbackData?.events,
fallbackData?.pois,
Expand All @@ -42,8 +35,8 @@ const SearchModalContainer = ({ navigation, route }: SearchModalContainerProps):
<SearchModal
cityCode={cityCode}
closeModal={navigation.goBack}
allPossibleResults={allPossibleResults}
allPossibleFallbackResults={allPossibleFallbackResults}
allPossibleContentLanguageResults={allPossibleContentLanguageResults}
allPossibleFallbackLanguageResults={allPossibleFallbackLanguageResults}

Check notice on line 39 in native/src/routes/SearchModalContainer.tsx

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ No longer an issue: Complex Method

SearchModalContainer is no longer above the threshold for cyclomatic complexity
languageCode={languageCode}
initialSearchText={initialSearchText}
/>
Expand Down
6 changes: 3 additions & 3 deletions native/src/routes/__tests__/SearchModal.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ describe('SearchModal', () => {
const eventModels = new EventModelBuilder('testseed', 5, cityCode, languageCode).build()
const poiModels = new PoiModelBuilder(3).build()

const allPossibleResults = [
const allPossibleContentLanguageResults = [
...categoriesMapModel.toArray().filter(category => !category.isRoot()),
...eventModels,
...poiModels,
]

const props: SearchModalProps = {
allPossibleResults,
allPossibleFallbackResults: [],
allPossibleContentLanguageResults,
allPossibleFallbackLanguageResults: [],
languageCode,
cityCode,
closeModal: dummy,
Expand Down
7 changes: 1 addition & 6 deletions web/src/hooks/useAllPossibleSearchResults.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { useMemo } from 'react'

import {
createCategoriesEndpoint,
createEventsEndpoint,
Expand Down Expand Up @@ -32,10 +30,7 @@ const useAllPossibleSearchResults = ({
const events = useLoadFromEndpoint(createEventsEndpoint, cmsApiBaseUrl, params)
const pois = useLoadFromEndpoint(createPOIsEndpoint, cmsApiBaseUrl, params)

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

return {
data: allPossibleResults,
Expand Down
10 changes: 5 additions & 5 deletions web/src/routes/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,24 @@ const SearchPage = ({ city, cityCode, languageCode, pathname }: CityRouteProps):
const navigate = useNavigate()

const {
data: allPossibleResults,
data: allPossibleContentLanguageResults,
loading,
error,
} = useAllPossibleSearchResults({
city: cityCode,
language: languageCode,
cmsApiBaseUrl,
})
const mainResults = useSearch(allPossibleResults, query)
const contentLanguageResults = useSearch(allPossibleContentLanguageResults, query)

const { data: allPossibleFallbackResults } = useAllPossibleSearchResults({
const { data: allPossibleFallbackLanguageResults } = useAllPossibleSearchResults({
city: cityCode,
language: config.sourceLanguage,
cmsApiBaseUrl,
})
const fallbackResults = useSearch(allPossibleFallbackResults, query)
const fallbackLanguageResults = useSearch(allPossibleFallbackLanguageResults, query)

const results = mainResults?.length === 0 ? fallbackResults : mainResults
const results = contentLanguageResults?.length === 0 ? fallbackLanguageResults : contentLanguageResults

if (!city) {
return null
Expand Down

0 comments on commit 17cbbd2

Please sign in to comment.