mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-29 07:00:53 -04:00
fix(client): fetch non-prod exam links on deploymentEnv (#63978)
This commit is contained in:
@@ -32,16 +32,22 @@ import {
|
||||
import { examAttempts } from '../../../utils/ajax';
|
||||
import MissingPrerequisites from '../exam/components/missing-prerequisites';
|
||||
import { isChallengeCompletedSelector } from '../redux/selectors';
|
||||
import envData from '../../../../config/env.json';
|
||||
import { Attempts } from './attempts';
|
||||
import ExamTokenControls from './exam-token-controls';
|
||||
|
||||
import './show.css';
|
||||
|
||||
const { deploymentEnv } = envData;
|
||||
|
||||
interface GitProps {
|
||||
tag_name: string;
|
||||
assets: {
|
||||
browser_download_url: string;
|
||||
}[];
|
||||
name: string;
|
||||
draft: boolean;
|
||||
prerelease: boolean;
|
||||
}
|
||||
|
||||
const mapStateToProps = createSelector(
|
||||
@@ -154,23 +160,53 @@ function ShowExamDownload({
|
||||
useEffect(() => {
|
||||
async function checkLatestVersion() {
|
||||
try {
|
||||
const response = await fetch(
|
||||
'https://api.github.com/repos/freeCodeCamp/exam-env/releases/latest'
|
||||
);
|
||||
if (response.ok) {
|
||||
let latest: GitProps;
|
||||
|
||||
if (deploymentEnv !== 'production') {
|
||||
const response = await fetch(
|
||||
'https://api.github.com/repos/freeCodeCamp/exam-env/releases'
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
setLatestVersion(null);
|
||||
return;
|
||||
}
|
||||
|
||||
const data = (await response.json()) as GitProps[];
|
||||
if (!data || data.length === 0) {
|
||||
setLatestVersion(null);
|
||||
return;
|
||||
}
|
||||
latest = getLatest(data);
|
||||
} else {
|
||||
const response = await fetch(
|
||||
'https://api.github.com/repos/freeCodeCamp/exam-env/releases/latest'
|
||||
);
|
||||
if (!response.ok) {
|
||||
setLatestVersion(null);
|
||||
return;
|
||||
}
|
||||
const data = (await response.json()) as GitProps;
|
||||
const { tag_name, assets } = data;
|
||||
setLatestVersion(tag_name);
|
||||
const urls = assets.map(link => link.browser_download_url);
|
||||
setDownloadLink(handleDownloadLink(urls));
|
||||
setDownloadLinks(urls);
|
||||
if (!data) {
|
||||
setLatestVersion(null);
|
||||
return;
|
||||
}
|
||||
latest = data;
|
||||
}
|
||||
|
||||
const { tag_name, assets } = latest;
|
||||
setLatestVersion(tag_name);
|
||||
const urls = assets.map(link => link.browser_download_url);
|
||||
setDownloadLink(handleDownloadLink(urls));
|
||||
setDownloadLinks(urls);
|
||||
} catch {
|
||||
setLatestVersion('...');
|
||||
setLatestVersion(null);
|
||||
}
|
||||
}
|
||||
|
||||
void checkLatestVersion();
|
||||
if (os.os) {
|
||||
void checkLatestVersion();
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [os]);
|
||||
|
||||
@@ -235,7 +271,6 @@ function ShowExamDownload({
|
||||
version: latestVersion || '...'
|
||||
})}
|
||||
</p>
|
||||
{/* TODO: confirm this works on MacOS */}
|
||||
<Button href={'exam-environment://'}>
|
||||
{t('exam.open-exam-application')}
|
||||
</Button>
|
||||
@@ -283,6 +318,30 @@ function ShowExamDownload({
|
||||
);
|
||||
}
|
||||
|
||||
function getLatest(releases: GitProps[]): GitProps {
|
||||
switch (deploymentEnv) {
|
||||
case 'staging':
|
||||
return (
|
||||
releases.find(r => {
|
||||
return !r.draft && r.name.endsWith('/staging');
|
||||
}) || releases[0]
|
||||
);
|
||||
// Currently, this is never the case
|
||||
case 'development':
|
||||
return (
|
||||
releases.find(r => {
|
||||
return !r.draft && r.name.endsWith('/development');
|
||||
}) || releases[0]
|
||||
);
|
||||
default:
|
||||
return (
|
||||
releases.find(r => {
|
||||
return !r.prerelease && !r.draft && r.name.endsWith('/production');
|
||||
}) || releases[0]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default connect(mapStateToProps)(withTranslation()(ShowExamDownload));
|
||||
|
||||
// GraphQL
|
||||
|
||||
Reference in New Issue
Block a user