Skip to content

TCA-354 2nd flavor of learn URL for FCC external link -> dev #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions client/src/components/layouts/tc-integration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ class TcIntegrationLayout extends Component<TcIntegrationLayoutProps> {

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

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

// if this is a freecodecamp lesson, change its domain and path
if (url.host === 'learn.freecodecamp.org') {
url.host = new URL(document.referrer).host;
const fccHost = 'freecodecamp.org';
if (url.host.endsWith(fccHost)) {
// TODO: it would be nice to not require that the FCC
// app knows about the paths in the platform UI, but
// creating a way to share this info would be complex and
// time consuming, so we can handle it when we get another
// provider.
url.pathname = `learn/freecodecamp${url.pathname}`;

// set the pathname for the 2 flavors of lesson URL
const platformPathPrefix = 'learn/freecodecamp';
const learnPrefix = '/learn/';
if (url.host === `learn.${fccHost}`) {
url.pathname = `${platformPathPrefix}${url.pathname}`;
} else if (
url.host === `www.${fccHost}` &&
url.pathname.startsWith(learnPrefix)
) {
url.pathname = url.pathname.replace(
learnPrefix,
`/${platformPathPrefix}/`
);
}

// set the host to the iframe's parent domain
url.host = new URL(document.referrer).host;
}

// now open the url in a new tab
Expand Down