Skip to content

Commit 4bda510

Browse files
TCA-355 #comment This PR handles clicks w/in the FCC app so that they are redirected to a new window. Also removing prettier bc it sucks. #time 4h
1 parent 3b8df04 commit 4bda510

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class TcIntegrationLayout extends Component<TcIntegrationLayoutProps> {
9797

9898
window.addEventListener('online', this.updateOnlineStatus);
9999
window.addEventListener('offline', this.updateOnlineStatus);
100+
window.addEventListener('click', this.externalLinkHandler);
100101
}
101102

102103
componentDidUpdate(prevProps: TcIntegrationLayoutProps) {
@@ -112,6 +113,39 @@ class TcIntegrationLayout extends Component<TcIntegrationLayoutProps> {
112113
window.removeEventListener('offline', this.updateOnlineStatus);
113114
}
114115

116+
externalLinkHandler = (event: MouseEvent) => {
117+
// if we're not clicking an anchor tag, there's nothing to do
118+
const eventTarget = event.target as HTMLElement;
119+
if (eventTarget?.localName !== 'a') {
120+
return;
121+
}
122+
123+
// if the target of the click isn't external, there's nothing to do
124+
const target = eventTarget as HTMLAnchorElement;
125+
const url = new URL(target.href);
126+
if (url.host === window.location.host) {
127+
return;
128+
}
129+
130+
// stop the click so we can alter it
131+
event.stopPropagation();
132+
event.preventDefault();
133+
134+
// if this is a freecodecamp lesson, change its domain and path
135+
if (url.host === 'learn.freecodecamp.org') {
136+
url.host = window.location.host;
137+
// TODO: it would be nice to not require that the FCC
138+
// app knows about the paths in the platform UI, but
139+
// creating a way to share this info would be complex and
140+
// time consuming, so we can handle it when we get another
141+
// provider.
142+
url.pathname = `learn/freecodecamp/${url.pathname}`;
143+
}
144+
145+
// now open the url in a new tab
146+
window.open(url, '_blank');
147+
};
148+
115149
updateOnlineStatus = () => {
116150
const { onlineStatusChange } = this.props;
117151
const isOnline =

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
"lint:challenges": "cd ./curriculum && npm run lint",
8282
"lint:js": "eslint --max-warnings 0 .",
8383
"lint:ts": "tsc && tsc -p config && tsc -p tools/ui-components && tsc -p utils",
84-
"lint:prettier": "prettier --list-different .",
8584
"seed": "cross-env DEBUG=fcc:* node ./tools/scripts/seed/seedAuthUser",
8685
"seed:certified-user": "cross-env DEBUG=fcc:* node ./tools/scripts/seed/seedAuthUser certUser",
8786
"serve:client": "cd ./client && npm run serve",

0 commit comments

Comments
 (0)