Skip to content

Commit 10d160b

Browse files
fix(client): daily-coding-challenge routing (freeCodeCamp#65942)
Co-authored-by: moT01 <20648924+moT01@users.noreply.github.com>
1 parent 4974310 commit 10d160b

3 files changed

Lines changed: 19 additions & 27 deletions

File tree

client/src/client-only-routes/show-daily-coding-challenge.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { useEffect, useState } from 'react';
2-
import { useParams } from '@gatsbyjs/reach-router';
32
import store from 'store';
43
import ShowClassic from '../templates/Challenges/classic/show';
54
import { Loader } from '../components/helpers';
@@ -140,9 +139,7 @@ function formatChallengeData({
140139
return props;
141140
}
142141

143-
function ShowDailyCodingChallenge(): JSX.Element {
144-
const { date } = useParams<{ date?: string }>();
145-
142+
function ShowDailyCodingChallenge({ date }: { date: string }): JSX.Element {
146143
const initLanguage =
147144
(store.get(
148145
'dailyCodingChallengeLanguage'

client/src/pages/learn/daily-coding-challenge/[...].tsx

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
11
/* eslint-disable filenames-simple/naming-convention */
2-
import { Router } from '@gatsbyjs/reach-router';
3-
import { withPrefix } from 'gatsby';
42
import React from 'react';
53

64
import ShowDailyCodingChallenge from '../../../client-only-routes/show-daily-coding-challenge';
75
import RedirectToArchive from '../../../components/redirect-daily-challenge-archive';
6+
import { isValidDateString } from '../../../components/daily-coding-challenge/helpers';
87

9-
const inlineStyles = {
10-
minHeight: 0,
11-
height: '100%'
12-
};
8+
interface Props {
9+
params: {
10+
'*': string;
11+
};
12+
}
1313

14-
function DailyCodingChallengeAll(): JSX.Element {
15-
return (
16-
// Router adds an element around the editor, messing with the layout because the editor is a flex item
17-
// These few inline styles fix it.
18-
<Router style={inlineStyles}>
19-
<ShowDailyCodingChallenge
20-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
21-
// @ts-ignore
22-
path={withPrefix('/learn/daily-coding-challenge/:date')}
23-
/>
14+
function DailyCodingChallengeAll(props: Props): JSX.Element {
15+
if (!isValidDateString(props.params['*'])) {
16+
return <RedirectToArchive />;
17+
}
2418

25-
<RedirectToArchive default />
26-
</Router>
27-
);
19+
return <ShowDailyCodingChallenge date={props.params['*']} />;
2820
}
2921

3022
DailyCodingChallengeAll.displayName = 'DailyCodingChallengeAll';

e2e/daily-coding-challenge.spec.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,9 @@ const mockApiAllChallenges = [
6666
const mockDaysInMonth = new Date(year, month, 0).getDate();
6767

6868
test.describe('Daily Coding Challenges', () => {
69-
test('should show not found page for invalid date', async ({ page }) => {
69+
test('should redirect to archive for invalid date', async ({ page }) => {
7070
await page.goto('/learn/daily-coding-challenge/invalid-date');
71-
await expect(
72-
page.getByText(/daily coding challenge not found\./i)
73-
).toBeVisible();
71+
await expect(page).toHaveURL('/learn/daily-coding-challenge/archive');
7472
});
7573

7674
test('should show not found page for date without challenge', async ({
@@ -252,5 +250,10 @@ test.describe('Daily Coding Challenge Archive', () => {
252250
await expect(page.getByTestId('calendar-day-completed')).toHaveCount(1);
253251

254252
await expect(page.getByTestId('calendar-day-not-completed')).toHaveCount(3);
253+
254+
await page.getByTestId('calendar-day-completed').click();
255+
await expect(page).toHaveURL(
256+
`/learn/daily-coding-challenge/${todayUsCentral}`
257+
);
255258
});
256259
});

0 commit comments

Comments
 (0)