Skip to content

Commit f082b9b

Browse files
Merge pull request #49 from topcoder-platform/TCA-354_fcc-link
TCA-354 2nd flavor of learn URL for FCC external link -> dev
2 parents d152e17 + 583442c commit f082b9b

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

client/src/components/layouts/tc-integration.tsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,13 @@ class TcIntegrationLayout extends Component<TcIntegrationLayoutProps> {
118118

119119
// if we're not clicking an anchor tag, there's nothing to do
120120
const eventTarget = event.target as HTMLElement;
121-
if (eventTarget?.localName !== 'a') {
121+
const anchorTag = eventTarget.closest('a');
122+
if (!anchorTag) {
122123
return;
123124
}
124125

125126
// if the target of the click isn't external, there's nothing to do
126-
const target = eventTarget as HTMLAnchorElement;
127+
const target = anchorTag;
127128
const url = new URL(target.href);
128129
if (url.host === window.location.host) {
129130
return;
@@ -134,14 +135,31 @@ class TcIntegrationLayout extends Component<TcIntegrationLayoutProps> {
134135
event.preventDefault();
135136

136137
// if this is a freecodecamp lesson, change its domain and path
137-
if (url.host === 'learn.freecodecamp.org') {
138-
url.host = new URL(document.referrer).host;
138+
const fccHost = 'freecodecamp.org';
139+
if (url.host.endsWith(fccHost)) {
139140
// TODO: it would be nice to not require that the FCC
140141
// app knows about the paths in the platform UI, but
141142
// creating a way to share this info would be complex and
142143
// time consuming, so we can handle it when we get another
143144
// provider.
144-
url.pathname = `learn/freecodecamp${url.pathname}`;
145+
146+
// set the pathname for the 2 flavors of lesson URL
147+
const platformPathPrefix = 'learn/freecodecamp';
148+
const learnPrefix = '/learn/';
149+
if (url.host === `learn.${fccHost}`) {
150+
url.pathname = `${platformPathPrefix}${url.pathname}`;
151+
} else if (
152+
url.host === `www.${fccHost}` &&
153+
url.pathname.startsWith(learnPrefix)
154+
) {
155+
url.pathname = url.pathname.replace(
156+
learnPrefix,
157+
`/${platformPathPrefix}/`
158+
);
159+
}
160+
161+
// set the host to the iframe's parent domain
162+
url.host = new URL(document.referrer).host;
145163
}
146164

147165
// now open the url in a new tab

0 commit comments

Comments
 (0)