From 583442c48fbf46a3fa3ecdafc64a671e3d02ea13 Mon Sep 17 00:00:00 2001 From: Brooke Date: Thu, 25 Aug 2022 10:04:01 -0700 Subject: [PATCH] TCA-354 #comment This PR handles a 2nd flavor of learn URL for FCC exernal links #time 30m --- .../src/components/layouts/tc-integration.tsx | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/client/src/components/layouts/tc-integration.tsx b/client/src/components/layouts/tc-integration.tsx index 69515d7a512859..d7211792668391 100755 --- a/client/src/components/layouts/tc-integration.tsx +++ b/client/src/components/layouts/tc-integration.tsx @@ -118,12 +118,13 @@ class TcIntegrationLayout extends Component { // 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; @@ -134,14 +135,31 @@ class TcIntegrationLayout extends Component { 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