From 103381c3a5665d77831253ffba9a7945f27fabec Mon Sep 17 00:00:00 2001 From: Florian Sommariva Date: Wed, 5 Jul 2023 17:32:33 +0200 Subject: [PATCH] Use commonDictionaries for usePopupResults --- .../components/Map/hooks/usePopupResult.ts | 23 +++++++++--- frontend/src/modules/outdoorSite/connector.ts | 6 +-- .../src/modules/touristicContent/connector.ts | 10 ++--- .../src/modules/touristicEvent/connector.ts | 37 +++---------------- 4 files changed, 31 insertions(+), 45 deletions(-) diff --git a/frontend/src/components/Map/hooks/usePopupResult.ts b/frontend/src/components/Map/hooks/usePopupResult.ts index f1a7addb3..1337b2f05 100644 --- a/frontend/src/components/Map/hooks/usePopupResult.ts +++ b/frontend/src/components/Map/hooks/usePopupResult.ts @@ -5,8 +5,11 @@ import { PopupResult } from 'modules/trekResult/interface'; import { getTouristicContentPopupResult } from 'modules/touristicContent/connector'; import { useRouter } from 'next/router'; import { getDefaultLanguage } from 'modules/header/utills'; -import { getOutdoorSitePopupResult } from '../../../modules/outdoorSite/connector'; +import { CommonDictionaries } from 'modules/dictionaries/interface'; +import { getCommonDictionaries } from 'modules/dictionaries/connector'; +import { ONE_DAY } from 'services/constants/staleTime'; import { getTouristicEventPopupResult } from '../../../modules/touristicEvent/connector'; +import { getOutdoorSitePopupResult } from '../../../modules/outdoorSite/connector'; export const usePopupResult = ( id: string, @@ -15,11 +18,21 @@ export const usePopupResult = ( ) => { const language = useRouter().locale ?? getDefaultLanguage(); + const { data: commonDictionaries } = useQuery( + ['commonDictionaries', language], + () => getCommonDictionaries(language), + { + staleTime: ONE_DAY, + }, + ); + const fetchData = () => { if (type === 'TREK') return getTrekPopupResult(id, language); - if (type === 'TOURISTIC_CONTENT') return getTouristicContentPopupResult(id, language); - if (type === 'OUTDOOR_SITE') return getOutdoorSitePopupResult(id, language); - if (type === 'TOURISTIC_EVENT') return getTouristicEventPopupResult(id, language); + if (type === 'TOURISTIC_CONTENT') + return getTouristicContentPopupResult(id, language, commonDictionaries); + if (type === 'OUTDOOR_SITE') return getOutdoorSitePopupResult(id, language, commonDictionaries); + if (type === 'TOURISTIC_EVENT') + return getTouristicEventPopupResult(id, language, commonDictionaries); throw new Error('Incorrect type'); }; @@ -27,7 +40,7 @@ export const usePopupResult = ( const { data: trekPopupResult, isLoading } = useQuery( ['popupResult', id, language], fetchData, - { enabled: shouldFetch }, + { enabled: shouldFetch && commonDictionaries !== undefined }, ); return { diff --git a/frontend/src/modules/outdoorSite/connector.ts b/frontend/src/modules/outdoorSite/connector.ts index 18513e769..98a58c55a 100644 --- a/frontend/src/modules/outdoorSite/connector.ts +++ b/frontend/src/modules/outdoorSite/connector.ts @@ -5,7 +5,6 @@ import { getInfrastructure } from 'modules/infrastructure/connector'; import { adaptGeometry } from 'modules/utils/geometry'; import { GeometryObject } from 'modules/interface'; import { CommonDictionaries } from 'modules/dictionaries/interface'; -import { getCities } from '../city/connector'; import { getOutdoorCoursesResult } from '../outdoorCourse/connector'; import { getOutdoorPractices } from '../outdoorPractice/connector'; import { getOutdoorRating } from '../outdoorRating/connector'; @@ -139,12 +138,13 @@ export const getOutdoorSiteDetails = async ( export const getOutdoorSitePopupResult = async ( id: string, language: string, + commonDictionaries?: CommonDictionaries, ): Promise => { const rawOutdoorSitePopupResult = await fetchOutdoorSiteDetails({ language }, id); - const cityDictionnary = await getCities(language); + const { cities = {} } = commonDictionaries ?? {}; - return adaptOutdoorSitePopupResults({ rawOutdoorSitePopupResult, cityDictionnary }); + return adaptOutdoorSitePopupResults({ rawOutdoorSitePopupResult, cityDictionnary: cities }); }; export const getOutdoorSiteGeometryResult = async ( diff --git a/frontend/src/modules/touristicContent/connector.ts b/frontend/src/modules/touristicContent/connector.ts index eef7d16be..ec576b6ab 100644 --- a/frontend/src/modules/touristicContent/connector.ts +++ b/frontend/src/modules/touristicContent/connector.ts @@ -1,4 +1,3 @@ -import { getCities } from 'modules/city/connector'; import { GeometryObject } from 'modules/interface'; import { getTouristicContentCategories, @@ -76,13 +75,12 @@ export const getTouristicContentDetails = async ( export const getTouristicContentPopupResult = async ( id: string, language: string, + commonDictionaries?: CommonDictionaries, ): Promise => { - const [rawTouristicContentPopupResult, cityDictionnary] = await Promise.all([ - fetchTouristicContentPopupResult({ language }, id), - getCities(language), - ]); + const rawTouristicContentPopupResult = await fetchTouristicContentPopupResult({ language }, id); + const { cities = {} } = commonDictionaries ?? {}; - return adaptTouristicContentPopupResults(rawTouristicContentPopupResult, cityDictionnary); + return adaptTouristicContentPopupResults(rawTouristicContentPopupResult, cities); }; export const getTouristicContentGeometryResult = async ( diff --git a/frontend/src/modules/touristicEvent/connector.ts b/frontend/src/modules/touristicEvent/connector.ts index 02fa48269..01e83a57b 100644 --- a/frontend/src/modules/touristicEvent/connector.ts +++ b/frontend/src/modules/touristicEvent/connector.ts @@ -1,38 +1,12 @@ import { GeometryObject } from 'modules/interface'; import { adaptGeometry } from 'modules/utils/geometry'; import { CommonDictionaries } from 'modules/dictionaries/interface'; -import { getCities } from '../city/connector'; -import { getThemes } from '../filters/theme/connector'; import { getTouristicContentsNearTarget } from '../touristicContent/connector'; import { getTouristicEventTypes } from '../touristicEventType/connector'; import { PopupResult } from '../trekResult/interface'; -import { - adaptTouristicEventDetails, - adaptTouristicEventPopupResults, - adaptTouristicEvents, -} from './adapter'; -import { fetchTouristicEventDetails, fetchTouristicEventResult, fetchTouristicEvents } from './api'; -import { TouristicEvent, TouristicEventDetails } from './interface'; - -export const getTouristicEvents = async ( - language: string, - query = {}, -): Promise => { - const [rawTouristicEventResult, themeDictionnary, cityDictionnary, touristicEventType] = - await Promise.all([ - fetchTouristicEvents({ ...query, language }), - getThemes(language), - getCities(language), - getTouristicEventTypes(language), - ]); - - return adaptTouristicEvents({ - rawTouristicEvents: rawTouristicEventResult.results, - themeDictionnary, - cityDictionnary, - touristicEventType, - }); -}; +import { adaptTouristicEventDetails, adaptTouristicEventPopupResults } from './adapter'; +import { fetchTouristicEventDetails, fetchTouristicEventResult } from './api'; +import { TouristicEventDetails } from './interface'; export const getTouristicEventDetails = async ( id: string, @@ -64,12 +38,13 @@ export const getTouristicEventDetails = async ( export const getTouristicEventPopupResult = async ( id: string, language: string, + commonDictionaries?: CommonDictionaries, ): Promise => { const rawTouristicEventPopupResult = await fetchTouristicEventDetails({ language }, id); - const [cityDictionnary] = await Promise.all([getCities(language)]); + const { cities = {} } = commonDictionaries ?? {}; - return adaptTouristicEventPopupResults({ rawTouristicEventPopupResult, cityDictionnary }); + return adaptTouristicEventPopupResults({ rawTouristicEventPopupResult, cityDictionnary: cities }); }; export const getTouristicEventGeometryResult = async (