Skip to content

Commit

Permalink
Use commonDictionaries for usePopupResults
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrucs committed Jul 24, 2023
1 parent 0990362 commit 103381c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 45 deletions.
23 changes: 18 additions & 5 deletions frontend/src/components/Map/hooks/usePopupResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -15,19 +18,29 @@ export const usePopupResult = (
) => {
const language = useRouter().locale ?? getDefaultLanguage();

const { data: commonDictionaries } = useQuery<CommonDictionaries, Error>(
['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');
};

const { data: trekPopupResult, isLoading } = useQuery<PopupResult, Error>(
['popupResult', id, language],
fetchData,
{ enabled: shouldFetch },
{ enabled: shouldFetch && commonDictionaries !== undefined },
);

return {
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/modules/outdoorSite/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -139,12 +138,13 @@ export const getOutdoorSiteDetails = async (
export const getOutdoorSitePopupResult = async (
id: string,
language: string,
commonDictionaries?: CommonDictionaries,
): Promise<PopupResult> => {
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 (
Expand Down
10 changes: 4 additions & 6 deletions frontend/src/modules/touristicContent/connector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getCities } from 'modules/city/connector';
import { GeometryObject } from 'modules/interface';
import {
getTouristicContentCategories,
Expand Down Expand Up @@ -76,13 +75,12 @@ export const getTouristicContentDetails = async (
export const getTouristicContentPopupResult = async (
id: string,
language: string,
commonDictionaries?: CommonDictionaries,
): Promise<PopupResult> => {
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 (
Expand Down
37 changes: 6 additions & 31 deletions frontend/src/modules/touristicEvent/connector.ts
Original file line number Diff line number Diff line change
@@ -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<TouristicEvent[]> => {
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,
Expand Down Expand Up @@ -64,12 +38,13 @@ export const getTouristicEventDetails = async (
export const getTouristicEventPopupResult = async (
id: string,
language: string,
commonDictionaries?: CommonDictionaries,
): Promise<PopupResult> => {
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 (
Expand Down

0 comments on commit 103381c

Please sign in to comment.