-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlternativeAudience.tsx
102 lines (89 loc) · 3.62 KB
/
AlternativeAudience.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { Fragment } from 'react';
import { BodyLong } from '@navikt/ds-react';
import {
AlternativeAudience as AlternativeAudienceType,
Audience,
} from 'types/component-props/_mixins';
import { usePageContentProps } from 'store/pageContext';
import { Language, translator } from 'translations';
import { LenkeInline } from 'components/_common/lenke/LenkeInline';
import { stripXpPathPrefix } from 'utils/urls';
import { ProductPageProps } from 'types/content-props/dynamic-page-props';
import { getConjunction, joinWithConjunction } from 'utils/string';
import style from './AlternativeAudience.module.scss';
type AudienceLink = {
title: string;
url: string;
};
const buildAudienceLinks = (
alternativeAudience: AlternativeAudienceType,
language: Language
): AudienceLink[] => {
const { person, employer, provider } = alternativeAudience;
const getAudienceLabel = translator('audience', language);
const getProviderAudienceLabel = translator('providerAudience', language);
const links: AudienceLink[] = [];
if (person?.targetPage) {
links.push({
title: getAudienceLabel(Audience.PERSON),
url: stripXpPathPrefix(person.targetPage._path),
});
}
if (employer?.targetPage) {
links.push({
title: getAudienceLabel(Audience.EMPLOYER),
url: stripXpPathPrefix(employer.targetPage._path),
});
}
// For providers, iterate the actual provider category
// rather than just showing "samarbeidspartnere" as we do
// with 'person' and 'arbeidsgiver'.
if (provider?.providerList) {
provider.providerList.forEach((singleProvider) => {
if (!singleProvider.targetPage) return;
const providerLabels = singleProvider.providerAudience.map((audience) => {
if (typeof audience === 'string') return ''; // Failsafe for old data.
if (audience.overrideLabel) return audience.overrideLabel;
return getProviderAudienceLabel(audience.name);
});
links.push({
title: joinWithConjunction(providerLabels, language),
url: stripXpPathPrefix(singleProvider.targetPage._path),
});
});
}
return links;
};
export const AlternativeAudience = () => {
const { data, language, displayName = '', page } = usePageContentProps<ProductPageProps>();
const { config } = page;
const { showProductName } = config;
const { alternativeAudience } = data;
const getStringPart = translator('stringParts', language);
const getRelatedString = translator('related', language);
if (!alternativeAudience) {
return null;
}
const productName =
showProductName === false ? getStringPart('this') : displayName.toLowerCase();
const audienceLinks = buildAudienceLinks(alternativeAudience, language);
return (
<div className={style.alternativeAudience}>
<BodyLong size="small" className={style.text}>
{getRelatedString('relatedAudience').replace('{name}', productName)}{' '}
{audienceLinks.map((link, index) => (
<Fragment key={index}>
<LenkeInline href={link.url} analyticsComponent="alternativ-målgruppe">
{link.title}
</LenkeInline>
{getConjunction({
index,
length: audienceLinks.length,
language,
})}
</Fragment>
))}
</BodyLong>
</div>
);
};