diff --git a/frontend/src/components/Map/hooks/usePopupResult.ts b/frontend/src/components/Map/hooks/usePopupResult.ts index 3a46d97b9..99385a4fd 100644 --- a/frontend/src/components/Map/hooks/usePopupResult.ts +++ b/frontend/src/components/Map/hooks/usePopupResult.ts @@ -5,9 +5,7 @@ import { PopupResult } from 'modules/trekResult/interface'; import { getTouristicContentPopupResult } from 'modules/touristicContent/connector'; import { useRouter } from 'next/router'; import { getDefaultLanguage } from 'modules/header/utills'; -import { CommonDictionaries } from 'modules/dictionaries/interface'; -import { getCommonDictionaries } from 'modules/dictionaries/connector'; -import { ONE_DAY } from 'services/constants/staleTime'; +import { queryCommonDictionaries } from 'modules/dictionaries/api'; import { getTouristicEventPopupResult } from '../../../modules/touristicEvent/connector'; import { getOutdoorSitePopupResult } from '../../../modules/outdoorSite/connector'; @@ -17,14 +15,7 @@ export const usePopupResult = ( type: 'TREK' | 'TOURISTIC_CONTENT' | 'OUTDOOR_SITE' | 'TOURISTIC_EVENT', ) => { const language = useRouter().locale ?? getDefaultLanguage(); - - const { data: commonDictionaries } = useQuery( - ['commonDictionaries', language], - () => getCommonDictionaries(language), - { - staleTime: ONE_DAY / 2, - }, - ); + const commonDictionaries = queryCommonDictionaries(language); const fetchData = () => { if (type === 'TREK') return getTrekPopupResult(id, language); diff --git a/frontend/src/components/pages/details/useDetails.tsx b/frontend/src/components/pages/details/useDetails.tsx index 50aa05e94..f01032c1a 100644 --- a/frontend/src/components/pages/details/useDetails.tsx +++ b/frontend/src/components/pages/details/useDetails.tsx @@ -10,8 +10,7 @@ import { useRouter } from 'next/router'; import { routes } from 'services/routes'; import { useMediaPredicate } from 'react-media-hook'; import useSectionsReferences from 'hooks/useSectionsReferences'; -import { getCommonDictionaries } from 'modules/dictionaries/connector'; -import { CommonDictionaries } from 'modules/dictionaries/interface'; +import { queryCommonDictionaries } from 'modules/dictionaries/api'; import { getDetailsConfig } from './config'; import { DetailsSectionOutdoorCourseNames, @@ -46,18 +45,7 @@ export const useDetails = ( const path = isUrlString(slug) ? decodeURI(slug) : ''; const router = useRouter(); - const { data: commonDictionaries } = useQuery( - ['commonDictionaries', language], - () => getCommonDictionaries(language), - { - onError: async error => { - if (isRessourceMissing(error)) { - await router.push(routes.HOME); - } - }, - staleTime: ONE_DAY / 2, - }, - ); + const commonDictionaries = queryCommonDictionaries(language); const { data: details, diff --git a/frontend/src/components/pages/home/useHome.ts b/frontend/src/components/pages/home/useHome.ts index 859d6423e..868e20547 100644 --- a/frontend/src/components/pages/home/useHome.ts +++ b/frontend/src/components/pages/home/useHome.ts @@ -5,9 +5,7 @@ import { HomePageConfig } from 'modules/home/interface'; import { adaptSuggestions, getHomePageConfig } from 'modules/home/utils'; import { useRouter } from 'next/router'; import { useQuery } from '@tanstack/react-query'; -import { CommonDictionaries } from 'modules/dictionaries/interface'; -import { getCommonDictionaries } from 'modules/dictionaries/connector'; -import { ONE_DAY } from 'services/constants/staleTime'; +import { queryCommonDictionaries } from 'modules/dictionaries/api'; interface UseHome { config: HomePageConfig; suggestions: ActivitySuggestion[]; @@ -22,13 +20,7 @@ export const useHome = (): UseHome => { 'ids' in suggestion ? suggestion.ids : [suggestion.type], ); - const { data: commonDictionaries } = useQuery( - ['commonDictionaries', language], - () => getCommonDictionaries(language), - { - staleTime: ONE_DAY / 2, - }, - ); + const commonDictionaries = queryCommonDictionaries(language); const { data = [] } = useQuery( ['activitySuggestions', `Suggestion-${activitySuggestionIds.join('-')}`, language], diff --git a/frontend/src/components/pages/search/hooks/useTrekResults.ts b/frontend/src/components/pages/search/hooks/useTrekResults.ts index 39573a20a..41c872c46 100644 --- a/frontend/src/components/pages/search/hooks/useTrekResults.ts +++ b/frontend/src/components/pages/search/hooks/useTrekResults.ts @@ -1,14 +1,12 @@ import { useRouter } from 'next/router'; -import { useInfiniteQuery, useQuery } from '@tanstack/react-query'; +import { useInfiniteQuery } 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 { CommonDictionaries } from 'modules/dictionaries/interface'; -import { getCommonDictionaries } from 'modules/dictionaries/connector'; -import { ONE_DAY } from 'services/constants/staleTime'; +import { queryCommonDictionaries } from 'modules/dictionaries/api'; import { formatInfiniteQuery, parseBboxFilter, parseFilters, parseTextFilter } from '../utils'; import { getGlobalConfig } from '../../../../modules/utils/api.config'; @@ -68,13 +66,7 @@ export const useTrekResults = ( const router = useRouter(); - const { data: commonDictionaries } = useQuery( - ['commonDictionaries', language], - () => getCommonDictionaries(language), - { - staleTime: ONE_DAY / 2, - }, - ); + const commonDictionaries = queryCommonDictionaries(language); const { data, diff --git a/frontend/src/components/pages/site/useOutdoorCourse.tsx b/frontend/src/components/pages/site/useOutdoorCourse.tsx index 70d9fdff5..1bea33901 100644 --- a/frontend/src/components/pages/site/useOutdoorCourse.tsx +++ b/frontend/src/components/pages/site/useOutdoorCourse.tsx @@ -3,11 +3,10 @@ import { isUrlString } from 'modules/utils/string'; import { useState } from 'react'; import { useQuery } from '@tanstack/react-query'; import { useRouter } from 'next/router'; -import { CommonDictionaries } from 'modules/dictionaries/interface'; -import { getCommonDictionaries } from 'modules/dictionaries/connector'; import { isRessourceMissing } from 'services/routeUtils'; import { routes } from 'services/routes'; import { ONE_DAY } from 'services/constants/staleTime'; +import { queryCommonDictionaries } from 'modules/dictionaries/api'; import { DetailsSections } from '../details/useDetails'; import { getDetailsConfig } from '../details/config'; import { OutdoorCourseDetails } from '../../../modules/outdoorCourse/interface'; @@ -22,18 +21,7 @@ export const useOutdoorCourse = ( const router = useRouter(); - const { data: commonDictionaries } = useQuery( - ['commonDictionaries', language], - () => getCommonDictionaries(language), - { - onError: async error => { - if (isRessourceMissing(error)) { - await router.push(routes.HOME); - } - }, - staleTime: ONE_DAY / 2, - }, - ); + const commonDictionaries = queryCommonDictionaries(language); const { data, refetch, isLoading } = useQuery( ['outdoorCourseDetails', id, language], diff --git a/frontend/src/components/pages/site/useOutdoorSite.tsx b/frontend/src/components/pages/site/useOutdoorSite.tsx index 272991e99..945d9ba50 100644 --- a/frontend/src/components/pages/site/useOutdoorSite.tsx +++ b/frontend/src/components/pages/site/useOutdoorSite.tsx @@ -3,11 +3,10 @@ import { isUrlString } from 'modules/utils/string'; import { useState } from 'react'; import { useQuery } from '@tanstack/react-query'; import { ONE_DAY } from 'services/constants/staleTime'; -import { CommonDictionaries } from 'modules/dictionaries/interface'; -import { getCommonDictionaries } from 'modules/dictionaries/connector'; import { isRessourceMissing } from 'services/routeUtils'; import { useRouter } from 'next/router'; import { routes } from 'services/routes'; +import { queryCommonDictionaries } from 'modules/dictionaries/api'; import { getOutdoorSiteDetails } from '../../../modules/outdoorSite/connector'; import { OutdoorSiteDetails } from '../../../modules/outdoorSite/interface'; import { getDetailsConfig } from '../details/config'; @@ -18,18 +17,7 @@ export const useOutdoorSite = (outdoorSiteUrl: string | string[] | undefined, la const path = isUrlString(outdoorSiteUrl) ? decodeURI(outdoorSiteUrl) : ''; const router = useRouter(); - const { data: commonDictionaries } = useQuery( - ['commonDictionaries', language], - () => getCommonDictionaries(language), - { - onError: async error => { - if (isRessourceMissing(error)) { - await router.push(routes.HOME); - } - }, - staleTime: ONE_DAY / 2, - }, - ); + const commonDictionaries = queryCommonDictionaries(language); const { data, refetch, isLoading } = useQuery( ['outdoorSiteDetails', id, language], diff --git a/frontend/src/components/pages/touristicContent/useTouristicContent.tsx b/frontend/src/components/pages/touristicContent/useTouristicContent.tsx index a4a9da548..fabec9d06 100644 --- a/frontend/src/components/pages/touristicContent/useTouristicContent.tsx +++ b/frontend/src/components/pages/touristicContent/useTouristicContent.tsx @@ -8,8 +8,7 @@ import { useRouter } from 'next/router'; import { ONE_DAY } from 'services/constants/staleTime'; import { routes } from 'services/routes'; import useSectionsReferences from 'hooks/useSectionsReferences'; -import { getCommonDictionaries } from 'modules/dictionaries/connector'; -import { CommonDictionaries } from 'modules/dictionaries/interface'; +import { queryCommonDictionaries } from 'modules/dictionaries/api'; import { DetailsSections } from '../details/useDetails'; import { getDetailsConfig } from '../details/config'; @@ -23,18 +22,7 @@ export const useTouristicContent = ( const path = isTouristicContentUrlString ? decodeURI(touristicContentUrl) : ''; const router = useRouter(); - const { data: commonDictionaries } = useQuery( - ['commonDictionaries', language], - () => getCommonDictionaries(language), - { - onError: async error => { - if (isRessourceMissing(error)) { - await router.push(routes.HOME); - } - }, - staleTime: ONE_DAY / 2, - }, - ); + const commonDictionaries = queryCommonDictionaries(language); const { data, refetch, isLoading } = useQuery( ['touristicContentDetails', id, language], diff --git a/frontend/src/components/pages/touristicEvent/useTouristicEvent.tsx b/frontend/src/components/pages/touristicEvent/useTouristicEvent.tsx index 30e2d8574..cdd98943c 100644 --- a/frontend/src/components/pages/touristicEvent/useTouristicEvent.tsx +++ b/frontend/src/components/pages/touristicEvent/useTouristicEvent.tsx @@ -6,8 +6,7 @@ import { useRouter } from 'next/router'; import { ONE_DAY } from 'services/constants/staleTime'; import { routes } from 'services/routes'; import useSectionsReferences from 'hooks/useSectionsReferences'; -import { getCommonDictionaries } from 'modules/dictionaries/connector'; -import { CommonDictionaries } from 'modules/dictionaries/interface'; +import { queryCommonDictionaries } from 'modules/dictionaries/api'; import { getTouristicEventDetails } from '../../../modules/touristicEvent/connector'; import { TouristicEventDetails } from '../../../modules/touristicEvent/interface'; import { getDetailsConfig } from '../details/config'; @@ -21,18 +20,7 @@ export const useTouristicEvent = ( const path = isUrlString(touristicEventUrl) ? decodeURI(touristicEventUrl) : ''; const router = useRouter(); - const { data: commonDictionaries } = useQuery( - ['commonDictionaries', language], - () => getCommonDictionaries(language), - { - onError: async error => { - if (isRessourceMissing(error)) { - await router.push(routes.HOME); - } - }, - staleTime: ONE_DAY / 2, - }, - ); + const commonDictionaries = queryCommonDictionaries(language); const { data, refetch, isLoading } = useQuery( ['outdoorCourseDetails', id, language], diff --git a/frontend/src/modules/dictionaries/api.ts b/frontend/src/modules/dictionaries/api.ts new file mode 100644 index 000000000..1ba9a5cac --- /dev/null +++ b/frontend/src/modules/dictionaries/api.ts @@ -0,0 +1,15 @@ +import { useQuery } from '@tanstack/react-query'; +import { ONE_DAY } from 'services/constants/staleTime'; +import { CommonDictionaries } from './interface'; +import { getCommonDictionaries } from './connector'; + +export const queryCommonDictionaries = (language: string) => { + const { data } = useQuery( + ['commonDictionaries', language], + () => getCommonDictionaries(language), + { + staleTime: ONE_DAY / 2, + }, + ); + return data; +};