Skip to content

Commit

Permalink
Do not display custom section if an used variable is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrucs committed Oct 24, 2023
1 parent ce6c443 commit 06b813a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 10 deletions.
14 changes: 12 additions & 2 deletions frontend/src/components/pages/details/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ import { DetailsCardSection } from './components/DetailsCardSection';
import { useDetails } from './useDetails';
import { ErrorFallback } from '../search/components/ErrorFallback';
import { DetailsTopIcons } from './components/DetailsTopIcons';
import { generateTouristicContentUrl, HtmlText } from './utils';
import {
generateTouristicContentUrl,
HtmlText,
templatesVariablesAreDefinedAndUsed,
} from './utils';
import { DetailsSource } from './components/DetailsSource';

import { DetailsInformationDesk } from './components/DetailsInformationDesk';
Expand Down Expand Up @@ -545,7 +549,13 @@ export const DetailsUIWithoutContext: React.FC<Props> = ({ slug, parentId, langu
}

// Custom HTML templates
if (section.template) {
if (
templatesVariablesAreDefinedAndUsed({
template: section.template,
id: details.id.toString(),
cityCode: details.cities_raw?.[0],
})
) {
return (
<section
key={section.name}
Expand Down
16 changes: 16 additions & 0 deletions frontend/src/components/pages/details/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,19 @@ export const generateDetailsUrlFromType = (
const titleWithNoSpace = convertStringForSitemap(title);
return `${routes[type]}/${id}-${encodeURI(titleWithNoSpace)}${searchParams}`;
};

export const templatesVariablesAreDefinedAndUsed = ({
template,
...variables
}: {
template?: string;
id: string;
cityCode?: string;
}) => {
if (!template) {
return false;
}
return Object.entries(variables).every(
([key, value]) => !template.includes(`{{ ${key} }}`) || value,
);
};
14 changes: 12 additions & 2 deletions frontend/src/components/pages/site/OutdoorCourseUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { DetailsHeader } from 'components/pages/details/components/DetailsHeader
import { DetailsSection } from 'components/pages/details/components/DetailsSection';
import { DetailsHeaderMobile, marginDetailsChild } from 'components/pages/details/Details';
import { useOnScreenSection } from 'components/pages/details/hooks/useHighlightedSection';
import { generateTouristicContentUrl, HtmlText } from 'components/pages/details/utils';
import {
generateTouristicContentUrl,
HtmlText,
templatesVariablesAreDefinedAndUsed,
} from 'components/pages/details/utils';
import { VisibleSectionProvider } from 'components/pages/details/VisibleSectionContext';
import { useOutdoorCourse } from 'components/pages/site/useOutdoorCourse';
import { useMemo, useRef } from 'react';
Expand Down Expand Up @@ -356,7 +360,13 @@ export const OutdoorCourseUIWithoutContext: React.FC<Props> = ({ outdoorCourseUr
}

// Custom HTML templates
if (section.template) {
if (
templatesVariablesAreDefinedAndUsed({
template: section.template,
id: outdoorCourseContent.id.toString(),
cityCode: outdoorCourseContent.cities_raw?.[0],
})
) {
return (
<section
key={section.name}
Expand Down
14 changes: 12 additions & 2 deletions frontend/src/components/pages/site/OutdoorSiteUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { DetailsSection } from 'components/pages/details/components/DetailsSecti
import { DetailsSource } from 'components/pages/details/components/DetailsSource';
import { DetailsHeaderMobile, marginDetailsChild } from 'components/pages/details/Details';
import { useOnScreenSection } from 'components/pages/details/hooks/useHighlightedSection';
import { generateTouristicContentUrl, HtmlText } from 'components/pages/details/utils';
import {
generateTouristicContentUrl,
HtmlText,
templatesVariablesAreDefinedAndUsed,
} from 'components/pages/details/utils';
import { VisibleSectionProvider } from 'components/pages/details/VisibleSectionContext';
import { DetailsChildrenSection } from 'components/pages/details/components/DetailsChildrenSection';
import { useMemo, useRef } from 'react';
Expand Down Expand Up @@ -479,7 +483,13 @@ const OutdoorSiteUIWithoutContext: React.FC<Props> = ({ outdoorSiteUrl, language
}

// Custom HTML templates
if (section.template) {
if (
templatesVariablesAreDefinedAndUsed({
template: section.template,
id: outdoorSiteContent.id.toString(),
cityCode: outdoorSiteContent.cities_raw?.[0],
})
) {
return (
<section
key={section.name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { DetailsTopIcons } from '../details/components/DetailsTopIcons';
import { DetailsSource } from '../details/components/DetailsSource';
import { DetailsCoverCarousel } from '../details/components/DetailsCoverCarousel';
import { DetailsHeaderMobile, marginDetailsChild } from '../details/Details';
import { HtmlText } from '../details/utils';
import { HtmlText, templatesVariablesAreDefinedAndUsed } from '../details/utils';
import { DetailsHeader } from '../details/components/DetailsHeader';
import { useDetailsSections } from '../details/useDetailsSections';

Expand Down Expand Up @@ -269,7 +269,13 @@ export const TouristicContentUI: React.FC<TouristicContentUIProps> = ({
}

// Custom HTML templates
if (section.template) {
if (
templatesVariablesAreDefinedAndUsed({
template: section.template,
id: touristicContent.id.toString(),
cityCode: touristicContent.cities_raw?.[0],
})
) {
return (
<section
key={section.name}
Expand Down
13 changes: 11 additions & 2 deletions frontend/src/components/pages/touristicEvent/TouristicEventUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { DetailsSection } from 'components/pages/details/components/DetailsSecti
import { DetailsSource } from 'components/pages/details/components/DetailsSource';
import { DetailsHeaderMobile, marginDetailsChild } from 'components/pages/details/Details';
import { useOnScreenSection } from 'components/pages/details/hooks/useHighlightedSection';
import { generateTouristicContentUrl } from 'components/pages/details/utils';
import {
generateTouristicContentUrl,
templatesVariablesAreDefinedAndUsed,
} from 'components/pages/details/utils';
import { VisibleSectionProvider } from 'components/pages/details/VisibleSectionContext';
import { useTouristicEvent } from 'components/pages/touristicEvent/useTouristicEvent';
import { useMemo, useRef } from 'react';
Expand Down Expand Up @@ -335,7 +338,13 @@ export const TouristicEventUIWithoutContext: React.FC<Props> = ({
}

// Custom HTML templates
if (section.template) {
if (
templatesVariablesAreDefinedAndUsed({
template: section.template,
id: touristicEventContent.id.toString(),
cityCode: touristicEventContent.cities_raw?.[0],
})
) {
return (
<section
key={section.name}
Expand Down

0 comments on commit 06b813a

Please sign in to comment.