Skip to content

Commit 2eb11a7

Browse files
authored
fix(client): persist transcript expanded state (freeCodeCamp#58437)
1 parent 7dcc6f5 commit 2eb11a7

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

client/i18n/locales/english/translations.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@
430430
"questions": "Questions",
431431
"answered-mcq": "You have unanswered questions and/or incorrect answers.",
432432
"explanation": "Explanation",
433-
"transcript": "Read a transcript of this video",
433+
"transcript": "Transcript",
434434
"solution-link": "Solution Link",
435435
"source-code-link": "Source Code Link",
436436
"ms-link": "Microsoft Link",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.challenge-transcript-heading {
22
cursor: pointer;
3+
font-weight: bold;
34
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import store from 'store';
4+
import ChallengeTranscript from './challenge-transcript';
5+
6+
const baseProps = {
7+
transcript: 'Sample transcript text'
8+
};
9+
10+
describe('<ChallengeTranscript />', () => {
11+
beforeEach(() => {
12+
store.clearAll();
13+
});
14+
15+
it('renders the transcript heading', () => {
16+
render(<ChallengeTranscript {...baseProps} />);
17+
expect(screen.getByText('learn.transcript')).toBeVisible();
18+
});
19+
20+
it('renders collapsed by default', () => {
21+
render(<ChallengeTranscript {...baseProps} />);
22+
expect(screen.getByTestId('challenge-transcript')).not.toHaveAttribute(
23+
'open'
24+
);
25+
expect(screen.getByText('Sample transcript text')).not.toBeVisible();
26+
});
27+
28+
it("renders collapsed when localstorage 'fcc-transcript-expanded = false'", () => {
29+
store.set('fcc-transcript-expanded', false);
30+
render(<ChallengeTranscript {...baseProps} />);
31+
expect(screen.getByTestId('challenge-transcript')).not.toHaveAttribute(
32+
'open'
33+
);
34+
expect(screen.getByText('Sample transcript text')).not.toBeVisible();
35+
});
36+
37+
it("renders expanded when 'fcc-transcript-expanded = true'", () => {
38+
store.set('fcc-transcript-expanded', true);
39+
render(<ChallengeTranscript {...baseProps} />);
40+
expect(screen.getByTestId('challenge-transcript')).toHaveAttribute('open');
41+
expect(screen.getByText('Sample transcript text')).toBeVisible();
42+
});
43+
});

client/src/templates/Challenges/components/challenge-transcript.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import { useTranslation } from 'react-i18next';
33
import { Spacer } from '@freecodecamp/ui';
4+
import store from 'store';
45
import PrismFormatted from './prism-formatted';
56

67
import './challenge-transcript.css';
@@ -14,10 +15,25 @@ function ChallengeTranscript({
1415
}: ChallengeTranscriptProps): JSX.Element {
1516
const { t } = useTranslation();
1617

18+
// default to collapsed
19+
const [isOpen, setIsOpen] = useState(
20+
() => (store.get('fcc-transcript-expanded') as boolean | null) ?? false
21+
);
22+
23+
function toggleExpandedState(e: React.MouseEvent<HTMLDetailsElement>) {
24+
e.preventDefault();
25+
store.set('fcc-transcript-expanded', !isOpen);
26+
setIsOpen(!isOpen);
27+
}
28+
1729
return (
1830
<>
19-
<details>
20-
<summary className='challenge-transcript-heading'>
31+
<details data-testid='challenge-transcript' open={isOpen}>
32+
<summary
33+
onClick={toggleExpandedState}
34+
aria-expanded={isOpen}
35+
className='challenge-transcript-heading'
36+
>
2137
{t('learn.transcript')}
2238
</summary>
2339
<Spacer size='m' />

0 commit comments

Comments
 (0)