-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlternativeAudience.tsx
142 lines (123 loc) · 5.11 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { Fragment } from 'react';
import { BodyLong } from '@navikt/ds-react';
import {
AlternativeAudience as AlternativeAudienceType,
Audience,
AudienceOptions,
getAudience,
} 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 getAudienceLabel = translator('audience', language);
const getProviderAudienceLabel = translator('providerAudience', language);
const getStringPart = translator('stringParts', language);
const getRelatedString = translator('related', language);
const getProviderTypes = (audience: AudienceOptions) => {
if (audience._selected !== Audience.PROVIDER) {
return [];
}
return audience[audience._selected].provider_audience;
};
const buildAudienceAffirmation = (addPeriod: boolean) => {
const { audience: currentAudience } = data;
const currentAudienceKey = getAudience(currentAudience);
if (!currentAudience || !currentAudienceKey || currentAudienceKey === 'person') {
return '';
}
const currentAudienceLabel = getAudienceLabel(currentAudienceKey);
const providerTypes = getProviderTypes(currentAudience) || [];
const providerTypesString = joinWithConjunction(
providerTypes.map((type) => getProviderAudienceLabel(type)),
language
);
const forString = `${getStringPart('for').charAt(0).toUpperCase()}${getStringPart(
'for'
).slice(1)}`;
return `${forString} ${providerTypesString || currentAudienceLabel}${addPeriod ? '.' : ''} `;
};
if (!alternativeAudience) {
return (
<div className={style.alternativeAudience}>
<BodyLong size="small" className={style.text}>
{buildAudienceAffirmation(false)}
</BodyLong>
</div>
);
}
const productName =
showProductName === false ? getStringPart('this') : displayName.toLowerCase();
const audienceLinks = buildAudienceLinks(alternativeAudience, language);
return (
<div className={style.alternativeAudience}>
<BodyLong size="small" className={style.text}>
{buildAudienceAffirmation(true)}
{getRelatedString('relatedAudience').replace('{name}', productName)}{' '}
{audienceLinks.map((link, index) => (
<Fragment key={index}>
<LenkeInline href={link.url} analyticsLabel={'Aktuell målgruppe'}>
{link.title}
</LenkeInline>
{getConjunction({
index,
length: audienceLinks.length,
language,
})}
</Fragment>
))}
</BodyLong>
</div>
);
};