Skip to content

Commit 2c469f0

Browse files
authored
Merge pull request #539 from topcoder-platform/TCA-1070_go-back-function
TCA-1070 - fixes navigate back from certificate view -> dev
2 parents 863d5b5 + ac8296d commit 2c469f0

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

src-ts/lib/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './use-on-hover-element.hook'
44
export * from './use-storage.hook'
55
export * from './use-viewport-units-fix.hook'
66
export * from './use-window-size.hook'
7+
export * from './use-navigate-back.hook'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { NavigateFunction, useNavigate } from 'react-router-dom'
2+
3+
export type NavigateBackFunction = (fallbackUrl: string) => void
4+
type useNavigateBackType = () => NavigateBackFunction
5+
6+
export const useNavigateBack: useNavigateBackType = (): NavigateBackFunction => {
7+
const navigate: NavigateFunction = useNavigate()
8+
return (fallbackUrl: string) => {
9+
const currentPageHref: string = window.location.href
10+
11+
window.history.go(-1)
12+
13+
setTimeout(() => {
14+
// go back didn't work, navigate to desired fallback url
15+
if (window.location.href === currentPageHref) {
16+
navigate(fallbackUrl)
17+
}
18+
}, 30)
19+
}
20+
}

src-ts/tools/learn/course-certificate/certificate-view/CertificateView.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { FC, MutableRefObject, useCallback, useEffect, useMemo, useRef } from 'react'
2-
import { NavigateFunction, useNavigate } from 'react-router-dom'
32
import classNames from 'classnames'
43

54
import {
@@ -8,7 +7,9 @@ import {
87
IconOutline,
98
LinkedinSocialShareBtn,
109
LoadingSpinner,
10+
NavigateBackFunction,
1111
TwitterSocialShareBtn,
12+
useNavigateBack,
1213
UserProfile,
1314
} from '../../../../lib'
1415
import {
@@ -41,7 +42,7 @@ interface CertificateViewProps {
4142

4243
const CertificateView: FC<CertificateViewProps> = (props: CertificateViewProps) => {
4344

44-
const navigate: NavigateFunction = useNavigate()
45+
const navigateBack: NavigateBackFunction = useNavigateBack()
4546
const coursePath: string = getCoursePath(props.provider, props.certification)
4647
const certificateElRef: MutableRefObject<HTMLDivElement | any> = useRef()
4748
const certificateWrapRef: MutableRefObject<HTMLDivElement | any> = useRef()
@@ -100,8 +101,8 @@ const CertificateView: FC<CertificateViewProps> = (props: CertificateViewProps)
100101
useCertificateScaling(ready ? certificateWrapRef : undefined, 880, 880)
101102

102103
const handleBackBtnClick: () => void = useCallback(() => {
103-
navigate(coursePath)
104-
}, [coursePath, navigate])
104+
navigateBack(coursePath)
105+
}, [coursePath, navigateBack])
105106

106107
const getCertificateCanvas: () => Promise<HTMLCanvasElement | void> = useCertificateCanvas(certificateElRef)
107108

src-ts/tools/learn/tca-certificate/certificate-view/CertificateView.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { FC, MutableRefObject, useCallback, useEffect, useMemo, useRef } from 'react'
2-
import { NavigateFunction, useNavigate } from 'react-router-dom'
32
import classNames from 'classnames'
43

54
import {
@@ -8,7 +7,9 @@ import {
87
IconOutline,
98
LinkedinSocialShareBtn,
109
LoadingSpinner,
10+
NavigateBackFunction,
1111
TwitterSocialShareBtn,
12+
useNavigateBack,
1213
UserProfile,
1314
} from '../../../../lib'
1415
import {
@@ -38,7 +39,7 @@ interface CertificateViewProps {
3839

3940
const CertificateView: FC<CertificateViewProps> = (props: CertificateViewProps) => {
4041

41-
const navigate: NavigateFunction = useNavigate()
42+
const navigateBack: NavigateBackFunction = useNavigateBack()
4243
const tcaCertificationPath: string = getTCACertificationPath(props.certification)
4344
const certificateElRef: MutableRefObject<HTMLDivElement | any> = useRef()
4445

@@ -80,8 +81,8 @@ const CertificateView: FC<CertificateViewProps> = (props: CertificateViewProps)
8081
), [hasCompletedTheCertification, ready])
8182

8283
const handleBackBtnClick: () => void = useCallback(() => {
83-
navigate(tcaCertificationPath)
84-
}, [tcaCertificationPath, navigate])
84+
navigateBack(tcaCertificationPath)
85+
}, [tcaCertificationPath, navigateBack])
8586

8687
const getCertificateCanvas: () => Promise<HTMLCanvasElement | void> = useCertificateCanvas(certificateElRef)
8788

0 commit comments

Comments
 (0)