diff --git a/frontend/src/components/pages/search/hooks/useCounter.ts b/frontend/src/components/pages/search/hooks/useCounter.ts index 0b6d0ed74..34f015618 100644 --- a/frontend/src/components/pages/search/hooks/useCounter.ts +++ b/frontend/src/components/pages/search/hooks/useCounter.ts @@ -1,5 +1,7 @@ import { useQuery } from '@tanstack/react-query'; import { ONE_DAY } from 'services/constants/staleTime'; +import { getCommonDictionaries } from 'modules/dictionaries/connector'; +import { CommonDictionaries } from 'modules/dictionaries/interface'; import { getSearchResults } from '../../../../modules/results/connector'; import { getGlobalConfig } from '../../../../modules/utils/api.config'; @@ -15,6 +17,14 @@ interface CountResult { } const useCounter = ({ language }: Args): CountResult => { + const { data: commonDictionaries } = useQuery( + ['commonDictionaries', language], + () => getCommonDictionaries(language), + { + staleTime: ONE_DAY, + }, + ); + const result = useQuery( ['counter'], ({ @@ -34,6 +44,7 @@ const useCounter = ({ language }: Args): CountResult => { }, pageParam, language, + commonDictionaries, ), { refetchOnReconnect: false, diff --git a/frontend/src/components/pages/search/hooks/useTrekResults.ts b/frontend/src/components/pages/search/hooks/useTrekResults.ts index 36524decf..7aaeb186e 100644 --- a/frontend/src/components/pages/search/hooks/useTrekResults.ts +++ b/frontend/src/components/pages/search/hooks/useTrekResults.ts @@ -1,13 +1,16 @@ import { useRouter } from 'next/router'; -import { useInfiniteQuery } from '@tanstack/react-query'; +import { useInfiniteQuery, useQuery } from '@tanstack/react-query'; import { useEffect, useRef, useState } from 'react'; import { getSearchResults } from 'modules/results/connector'; import { SearchResults } from 'modules/results/interface'; import { DateFilter, FilterState } from 'modules/filters/interface'; -import { getGlobalConfig } from '../../../../modules/utils/api.config'; +import { CommonDictionaries } from 'modules/dictionaries/interface'; +import { getCommonDictionaries } from 'modules/dictionaries/connector'; +import { ONE_DAY } from 'services/constants/staleTime'; import { formatInfiniteQuery, parseBboxFilter, parseFilters, parseTextFilter } from '../utils'; +import { getGlobalConfig } from '../../../../modules/utils/api.config'; const formatFiltersUrl = (filtersState: FilterState[]): string[] => filtersState.reduce( @@ -65,6 +68,14 @@ export const useTrekResults = ( const router = useRouter(); + const { data: commonDictionaries } = useQuery( + ['commonDictionaries', language], + () => getCommonDictionaries(language), + { + staleTime: ONE_DAY, + }, + ); + const { data, isLoading, @@ -96,6 +107,7 @@ export const useTrekResults = ( { filtersState: parsedFiltersState, textFilterState, bboxState, dateFilter }, pageParam, language, + commonDictionaries, ); }, { diff --git a/frontend/src/modules/outdoorSite/connector.ts b/frontend/src/modules/outdoorSite/connector.ts index 98a58c55a..472f62ca3 100644 --- a/frontend/src/modules/outdoorSite/connector.ts +++ b/frontend/src/modules/outdoorSite/connector.ts @@ -94,7 +94,17 @@ export const getOutdoorSiteDetails = async ( infrastructure, sensitiveAreas, ] = await Promise.all([ - getTrekResults(language, { near_outdoorsite: Number(id) }), + getTrekResults( + language, + { near_outdoorsite: Number(id) }, + { + cities, + themes, + sources, + informationDesk, + labels, + }, + ), getOutdoorPractices(language), getOutdoorRating(language), getOutdoorRatingScale(language), diff --git a/frontend/src/modules/results/connector.ts b/frontend/src/modules/results/connector.ts index cc3f76afa..4d229973b 100644 --- a/frontend/src/modules/results/connector.ts +++ b/frontend/src/modules/results/connector.ts @@ -1,14 +1,12 @@ import { getTouristicContentCategories } from 'modules/touristicContentCategory/connector'; import { getActivities } from 'modules/activities/connector'; import { getDifficulties } from 'modules/filters/difficulties'; -import { getThemes } from 'modules/filters/theme/connector'; import { CATEGORY_ID, EVENT_ID, OUTDOOR_ID, PRACTICE_ID } from 'modules/filters/constant'; import { QueryFilterState } from 'components/pages/search/utils'; import { fetchTouristicContentResult } from 'modules/touristicContent/api'; import { getGlobalConfig } from 'modules/utils/api.config'; import { DateFilter } from 'modules/filters/interface'; import { TouristicContentResult } from 'modules/touristicContent/interface'; -import { getCities } from 'modules/city/connector'; import { adaptTouristicContentResult } from 'modules/touristicContent/adapter'; import { CommonDictionaries } from 'modules/dictionaries/interface'; import { getOutdoorPractices } from '../outdoorPractice/connector'; @@ -62,8 +60,10 @@ export const getSearchResults = async ( touristicEvents: number | null; }, language: string, + commonDictionaries?: CommonDictionaries, ): Promise => { const { filtersState, textFilterState, bboxState, dateFilter } = filters; + const { themes = {}, cities = {} } = commonDictionaries ?? {}; try { const practiceFilter = filtersState.find(({ id }) => id === PRACTICE_ID); @@ -236,24 +236,20 @@ export const getSearchResults = async ( const [ rawTrekResults, difficulties, - themes, activities, rawTouristicContents, rawOutdoorSites, rawTouristicEvents, touristicContentCategories, - cityDictionnary, outdoorPracticeDictionnary, ] = await Promise.all([ shouldFetchTreks ? getTreksResultsPromise : emptyResultPromise, getDifficulties(language), // Todo: Find a way to store this hashmap to avoid calling this every time - getThemes(language), // Todo: Find a way to store this hashmap to avoid calling this every time getActivities(language), // Todo: Find a way to store this hashmap to avoid calling this every time shouldFetchTouristicContents ? getToursticContentsPromise : emptyResultPromise, shouldFetchOutdoorSites ? getOutdoorSitesPromise : emptyResultPromise, shouldFetchTouristicEvents ? getTouristicEventsPromise : emptyResultPromise, getTouristicContentCategories(language), // Todo: Find a way to store this hashmap to avoid calling this every time - getCities(language), getOutdoorPractices(language), ]); @@ -264,27 +260,27 @@ export const getSearchResults = async ( difficulties, themes, activities, - cityDictionnary, + cityDictionnary: cities, }); const adaptedTouristicContentsList: TouristicContentResult[] = adaptTouristicContentResult({ rawTouristicContent: rawTouristicContents.results, touristicContentCategories, themeDictionnary: themes, - cityDictionnary, + cityDictionnary: cities, }); const adaptedOutdoorSitesList: OutdoorSiteResult[] = adaptoutdoorSitesResult({ rawOutdoorSites: rawOutdoorSites.results, themeDictionnary: themes, outdoorPracticeDictionnary, - cityDictionnary, + cityDictionnary: cities, }); const adaptedTouristicEventsList: TouristicEventResult[] = adaptTouristicEventsResult({ rawTouristicEvents: rawTouristicEvents.results, themeDictionnary: themes, - cityDictionnary, + cityDictionnary: cities, touristicEventType, }); @@ -366,13 +362,16 @@ export const getTrekResultsById = async ( } }; -export const getTrekResults = async (language: string, query = {}): Promise => { - const [rawTrekResults, difficulties, themes, activities, cityDictionnary] = await Promise.all([ +export const getTrekResults = async ( + language: string, + query = {}, + commonDictionaries: CommonDictionaries, +): Promise => { + const { cities, themes } = commonDictionaries; + const [rawTrekResults, difficulties, activities] = await Promise.all([ fetchTrekResults({ language, ...query }), getDifficulties(language), - getThemes(language), getActivities(language), - getCities(language), ]); return adaptTrekResultList({ @@ -380,6 +379,6 @@ export const getTrekResults = async (language: string, query = {}): Promise { endDate: context.query.endDate ?? '', }; + const commonDictionaries = await getCommonDictionaries(locale); + await queryClient.prefetchQuery(['commonDictionaries', locale], () => commonDictionaries); + await queryClient.prefetchInfiniteQuery( [ 'trekResults', @@ -55,6 +59,7 @@ export const getServerSideProps: GetServerSideProps = async context => { touristicEvents: getGlobalConfig().enableTouristicEvents ? page : null, }, locale, + commonDictionaries, ), ); } catch (error) {