Skip to content

Commit 8034515

Browse files
fix(client): add better navigation for navigating back to blocks (freeCodeCamp#64027)
Co-authored-by: Huyen Nguyen <[email protected]>
1 parent 4d8b384 commit 8034515

File tree

5 files changed

+215
-71
lines changed

5 files changed

+215
-71
lines changed

client/src/templates/Introduction/components/block-header.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe('<BlockHeader />', () => {
3434
{...defaultProps}
3535
accordion={true}
3636
blockUrl='/learn/test-block'
37+
onLinkClick={() => {}}
3738
/>
3839
);
3940

client/src/templates/Introduction/components/block-header.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import CheckMark from './check-mark';
1010
import BlockLabel from './block-label';
1111
import BlockIntros from './block-intros';
1212

13-
interface BlockHeaderProps {
13+
interface BaseBlockHeaderProps {
1414
blockDashed: string;
1515
blockTitle: string;
1616
blockLabel: BlockLabelType | null;
@@ -22,9 +22,19 @@ interface BlockHeaderProps {
2222
percentageCompleted: number;
2323
blockIntroArr?: string[];
2424
accordion?: boolean;
25-
blockUrl?: string;
2625
}
2726

27+
interface BlockHeaderButtonProps extends BaseBlockHeaderProps {
28+
blockUrl?: never;
29+
onLinkClick?: never;
30+
}
31+
32+
interface BlockHeaderLinkProps extends BaseBlockHeaderProps {
33+
blockUrl: string;
34+
onLinkClick: () => void;
35+
}
36+
37+
type BlockHeaderProps = BlockHeaderButtonProps | BlockHeaderLinkProps;
2838
function BlockHeader({
2939
blockDashed,
3040
blockTitle,
@@ -37,7 +47,8 @@ function BlockHeader({
3747
percentageCompleted,
3848
blockIntroArr,
3949
accordion,
40-
blockUrl
50+
blockUrl,
51+
onLinkClick
4152
}: BlockHeaderProps): JSX.Element {
4253
const InnerBlockHeader = () => (
4354
<>
@@ -68,7 +79,7 @@ function BlockHeader({
6879
<>
6980
<h3 className='block-grid-title'>
7081
{accordion && blockUrl ? (
71-
<Link className='block-header' to={blockUrl}>
82+
<Link className='block-header' to={blockUrl} onClick={onLinkClick}>
7283
<InnerBlockHeader />
7384
</Link>
7485
) : (

client/src/templates/Introduction/components/block.tsx

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export class Block extends Component<BlockProps> {
8282
super(props);
8383

8484
this.handleBlockClick = this.handleBlockClick.bind(this);
85-
this.handleBlockHover = this.handleBlockHover.bind(this);
85+
this.handleChallengeClick = this.handleChallengeClick.bind(this);
8686
}
8787

8888
handleBlockClick = (): void => {
@@ -91,13 +91,8 @@ export class Block extends Component<BlockProps> {
9191
toggleBlock(block);
9292
};
9393

94-
/*
95-
* This function handles the block hover event.
96-
* It also updates the URL hash to reflect the current block.
97-
*/
98-
handleBlockHover = (): void => {
94+
handleChallengeClick = (): void => {
9995
const { block } = this.props;
100-
// Convert block to dashed format
10196
const dashedBlock = block
10297
.toLowerCase()
10398
.replace(/\s+/g, '-')
@@ -181,11 +176,7 @@ export class Block extends Component<BlockProps> {
181176
*/
182177
const LegacyChallengeListBlock = (
183178
<Element name={block}>
184-
<div
185-
className={`block ${isExpanded ? 'open' : ''}`}
186-
onMouseOver={this.handleBlockHover}
187-
onFocus={this.handleBlockHover}
188-
>
179+
<div className={`block ${isExpanded ? 'open' : ''}`}>
189180
<div className='block-header'>
190181
<h3 className='big-block-title'>{blockTitle}</h3>
191182
{blockLabel && <BlockLabel blockLabel={blockLabel} />}
@@ -224,7 +215,12 @@ export class Block extends Component<BlockProps> {
224215
</span>
225216
</div>
226217
</button>
227-
{isExpanded && <ChallengesList challenges={extendedChallenges} />}
218+
{isExpanded && (
219+
<ChallengesList
220+
challenges={extendedChallenges}
221+
onChallengeClick={this.handleChallengeClick}
222+
/>
223+
)}
228224
</div>
229225
</Element>
230226
);
@@ -236,11 +232,7 @@ export class Block extends Component<BlockProps> {
236232
*/
237233
const ProjectListBlock = (
238234
<Element name={block}>
239-
<div
240-
className='block'
241-
onMouseOver={this.handleBlockHover}
242-
onFocus={this.handleBlockHover}
243-
>
235+
<div className='block'>
244236
<div className='block-header'>
245237
<h3 className='big-block-title'>{blockTitle}</h3>
246238
{blockLabel && <BlockLabel blockLabel={blockLabel} />}
@@ -255,7 +247,10 @@ export class Block extends Component<BlockProps> {
255247
</div>
256248
)}
257249
</div>
258-
<ChallengesList challenges={extendedChallenges} />
250+
<ChallengesList
251+
challenges={extendedChallenges}
252+
onChallengeClick={this.handleChallengeClick}
253+
/>
259254
</div>
260255
</Element>
261256
);
@@ -267,11 +262,7 @@ export class Block extends Component<BlockProps> {
267262
*/
268263
const LegacyChallengeGridBlock = (
269264
<Element name={block}>
270-
<div
271-
className={`block block-grid ${isExpanded ? 'open' : ''}`}
272-
onMouseOver={this.handleBlockHover}
273-
onFocus={this.handleBlockHover}
274-
>
265+
<div className={`block block-grid ${isExpanded ? 'open' : ''}`}>
275266
<BlockHeader
276267
blockDashed={block}
277268
blockTitle={blockTitle}
@@ -305,6 +296,7 @@ export class Block extends Component<BlockProps> {
305296
challenges={extendedChallenges}
306297
isProjectBlock={isProjectBlock}
307298
blockTitle={blockTitle}
299+
onChallengeClick={this.handleChallengeClick}
308300
/>
309301
</div>
310302
</>
@@ -353,6 +345,7 @@ export class Block extends Component<BlockProps> {
353345
challenges={extendedChallenges}
354346
blockTitle={blockTitle}
355347
jumpLink={!accordion}
348+
onChallengeClick={this.handleChallengeClick}
356349
/>
357350
</div>
358351
</>
@@ -368,11 +361,7 @@ export class Block extends Component<BlockProps> {
368361
*/
369362
const LegacyLinkBlock = (
370363
<Element name={block}>
371-
<div
372-
className='block block-grid grid-project-block'
373-
onMouseOver={this.handleBlockHover}
374-
onFocus={this.handleBlockHover}
375-
>
364+
<div className='block block-grid grid-project-block'>
376365
<div className='tags-wrapper'>
377366
<span className='cert-tag' aria-hidden='true'>
378367
{t('misc.certification-project')}
@@ -394,9 +383,7 @@ export class Block extends Component<BlockProps> {
394383
<h3 className='block-grid-title'>
395384
<Link
396385
className='block-header'
397-
onClick={() => {
398-
this.handleBlockClick();
399-
}}
386+
onClick={this.handleChallengeClick}
400387
to={link}
401388
>
402389
<CheckMark isCompleted={isBlockCompleted} />
@@ -423,8 +410,6 @@ export class Block extends Component<BlockProps> {
423410
</Element>
424411
<div
425412
className={`block block-grid block-grid-no-border challenge-grid-block ${isExpanded ? 'open' : ''}`}
426-
onMouseOver={this.handleBlockHover}
427-
onFocus={this.handleBlockHover}
428413
>
429414
<BlockHeader
430415
blockDashed={block}
@@ -461,9 +446,13 @@ export class Block extends Component<BlockProps> {
461446
blockTitle={blockTitle}
462447
isProjectBlock={isProjectBlock}
463448
jumpLink={false}
449+
onChallengeClick={this.handleChallengeClick}
464450
/>
465451
) : (
466-
<ChallengesList challenges={extendedChallenges} />
452+
<ChallengesList
453+
challenges={extendedChallenges}
454+
onChallengeClick={this.handleChallengeClick}
455+
/>
467456
)}
468457
</div>
469458
</div>
@@ -477,8 +466,6 @@ export class Block extends Component<BlockProps> {
477466
</Element>
478467
<div
479468
className={`block block-grid challenge-grid-block ${isExpanded ? 'open' : ''}`}
480-
onMouseOver={this.handleBlockHover}
481-
onFocus={this.handleBlockHover}
482469
>
483470
<BlockHeader
484471
blockDashed={block}
@@ -516,9 +503,13 @@ export class Block extends Component<BlockProps> {
516503
challenges={extendedChallenges}
517504
blockTitle={blockTitle}
518505
isProjectBlock={isProjectBlock}
506+
onChallengeClick={this.handleChallengeClick}
519507
/>
520508
) : (
521-
<ChallengesList challenges={extendedChallenges} />
509+
<ChallengesList
510+
challenges={extendedChallenges}
511+
onChallengeClick={this.handleChallengeClick}
512+
/>
522513
)}
523514
</div>
524515
</div>
@@ -539,6 +530,7 @@ export class Block extends Component<BlockProps> {
539530
completedCount={completedCount}
540531
courseCompletionStatus={courseCompletionStatus()}
541532
handleClick={this.handleBlockClick}
533+
onLinkClick={this.handleChallengeClick}
542534
isCompleted={isBlockCompleted}
543535
isExpanded={isExpanded}
544536
percentageCompleted={percentageCompleted}

0 commit comments

Comments
 (0)