diff --git a/client/src/pages/learn/daily-coding-challenge.tsx b/client/src/client-only-routes/show-daily-coding-challenge.tsx
similarity index 84%
rename from client/src/pages/learn/daily-coding-challenge.tsx
rename to client/src/client-only-routes/show-daily-coding-challenge.tsx
index 30d1a7b7fd0..d00454eda27 100644
--- a/client/src/pages/learn/daily-coding-challenge.tsx
+++ b/client/src/client-only-routes/show-daily-coding-challenge.tsx
@@ -1,23 +1,21 @@
import React, { useEffect, useState } from 'react';
+import { useParams } from '@gatsbyjs/reach-router';
import store from 'store';
-import ShowClassic from '../../templates/Challenges/classic/show';
-import { Loader } from '../../components/helpers';
+import ShowClassic from '../templates/Challenges/classic/show';
+import { Loader } from '../components/helpers';
import {
DailyCodingChallengeLanguages,
DailyCodingChallengeNode,
DailyCodingChallengePageContext
-} from '../../redux/prop-types';
-import DailyCodingChallengeNotFound from '../../components/daily-coding-challenge/not-found';
-import FourOhFour from '../../components/FourOhFour';
-import {
- apiLocation,
- showDailyCodingChallenges
-} from '../../../config/env.json';
-import { isValidDateParam } from '../../components/daily-coding-challenge/helpers';
+} from '../redux/prop-types';
+import DailyCodingChallengeNotFound from '../components/daily-coding-challenge/not-found';
+import FourOhFour from '../components/FourOhFour';
+import { apiLocation, showDailyCodingChallenges } from '../../config/env.json';
+import { isValidDateString } from '../components/daily-coding-challenge/helpers';
import {
validateDailyCodingChallengeSchema,
type DailyCodingChallengeFromDb
-} from '../../utils/daily-coding-challenge-validator';
+} from '../utils/daily-coding-challenge-validator';
interface DailyCodingChallengeLanguageData {
data: {
@@ -31,7 +29,7 @@ interface DailyCodingChallengeDataFormatted {
python: DailyCodingChallengeLanguageData;
}
-// These are not included in the data from the DB (Daily Challenge API) - so we add them in
+// This is not included in the data from the DB (Daily Challenge API) - so we add it in
function formatDescription(str: string) {
return `\n${str}\n`;
}
@@ -149,7 +147,9 @@ function formatChallengeData({
return props;
}
-function DailyCodingChallenge(): JSX.Element {
+function ShowDailyCodingChallenge(): JSX.Element {
+ const { date } = useParams<{ date?: string }>();
+
const initLanguage =
(store.get(
'dailyCodingChallengeLanguage'
@@ -162,9 +162,6 @@ function DailyCodingChallenge(): JSX.Element {
const [dailyCodingChallengeLanguage, setDailyCodingChallengeLanguage] =
useState(initLanguage);
- const dateParam =
- new URLSearchParams(window.location.search).get('date') || '';
-
const fetchChallenge = async (date: string) => {
try {
const response = await fetch(
@@ -201,15 +198,15 @@ function DailyCodingChallenge(): JSX.Element {
};
useEffect(() => {
- // If dateParam is invalid, stop loading/fetching and show the not found page
- if (!isValidDateParam(dateParam)) {
+ // If date is invalid, stop loading/fetching and show the not found page
+ if (!date || !isValidDateString(date)) {
setIsLoading(false);
setChallengeFound(false);
return;
}
- void fetchChallenge(dateParam);
- }, [dateParam]);
+ void fetchChallenge(date);
+ }, [date]);
if (!showDailyCodingChallenges) {
return ;
@@ -230,6 +227,6 @@ function DailyCodingChallenge(): JSX.Element {
);
}
-DailyCodingChallenge.displayName = 'DailyCodingChallenge';
+ShowDailyCodingChallenge.displayName = 'ShowDailyCodingChallenge';
-export default DailyCodingChallenge;
+export default ShowDailyCodingChallenge;
diff --git a/client/src/components/Progress/progress.tsx b/client/src/components/Progress/progress.tsx
index a05980918c9..68874e7a7c5 100644
--- a/client/src/components/Progress/progress.tsx
+++ b/client/src/components/Progress/progress.tsx
@@ -16,7 +16,7 @@ import { updateAllChallengesInfo } from '../../redux/actions';
import { CertificateNode, ChallengeNode } from '../../redux/prop-types';
import { getIsDailyCodingChallenge } from '../../../../shared/config/challenge-types';
import {
- isValidDateParam,
+ isValidDateString,
formatDisplayDate
} from '../daily-coding-challenge/helpers';
import ProgressInner from './progress-inner';
@@ -81,7 +81,7 @@ function Progress({
const dateParam =
new URLSearchParams(window.location.search).get('date') || '';
- if (isValidDateParam(dateParam)) {
+ if (isValidDateString(dateParam)) {
blockTitle += `: ${formatDisplayDate(dateParam)}`;
}
}
diff --git a/client/src/components/daily-coding-challenge/calendar-day.tsx b/client/src/components/daily-coding-challenge/calendar-day.tsx
index f76eae7b43d..91d5968a1b5 100644
--- a/client/src/components/daily-coding-challenge/calendar-day.tsx
+++ b/client/src/components/daily-coding-challenge/calendar-day.tsx
@@ -41,7 +41,7 @@ function DailyCodingChallengeCalendarDay({
// isAvailable -> render link to challenge
return (
{t('buttons.go-to-today')}
diff --git a/client/src/components/daily-coding-challenge/helpers.ts b/client/src/components/daily-coding-challenge/helpers.ts
index 7ee7c6a5c3a..b01b67c9637 100644
--- a/client/src/components/daily-coding-challenge/helpers.ts
+++ b/client/src/components/daily-coding-challenge/helpers.ts
@@ -20,7 +20,7 @@ export function getTodayUsCentral(dateObj: Date = new Date()) {
// Validate that dateString is in the format yyyy-MM-dd
// Leading zero's are accepted for single digit month/day
-export function isValidDateParam(dateString: string) {
+export function isValidDateString(dateString: string) {
const parsedDate = parse(dateString, 'yyyy-MM-dd', new Date());
return isValid(parsedDate);
}
diff --git a/client/src/components/daily-coding-challenge/not-found.tsx b/client/src/components/daily-coding-challenge/not-found.tsx
index 4a76cca1256..293c99453a2 100644
--- a/client/src/components/daily-coding-challenge/not-found.tsx
+++ b/client/src/components/daily-coding-challenge/not-found.tsx
@@ -41,7 +41,7 @@ function DailyCodingChallengeNotFound(): JSX.Element {