Skip to content

TCA-1070 - fixes navigate back from certificate view -> dev #539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src-ts/lib/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './use-on-hover-element.hook'
export * from './use-storage.hook'
export * from './use-viewport-units-fix.hook'
export * from './use-window-size.hook'
export * from './use-navigate-back.hook'
20 changes: 20 additions & 0 deletions src-ts/lib/hooks/use-navigate-back.hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NavigateFunction, useNavigate } from 'react-router-dom'

export type NavigateBackFunction = (fallbackUrl: string) => void
type useNavigateBackType = () => NavigateBackFunction

export const useNavigateBack: useNavigateBackType = (): NavigateBackFunction => {
const navigate: NavigateFunction = useNavigate()
return (fallbackUrl: string) => {
const currentPageHref: string = window.location.href

window.history.go(-1)

setTimeout(() => {
// go back didn't work, navigate to desired fallback url
if (window.location.href === currentPageHref) {
navigate(fallbackUrl)
}
}, 30)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { FC, MutableRefObject, useCallback, useEffect, useMemo, useRef } from 'react'
import { NavigateFunction, useNavigate } from 'react-router-dom'
import classNames from 'classnames'

import {
Expand All @@ -8,7 +7,9 @@ import {
IconOutline,
LinkedinSocialShareBtn,
LoadingSpinner,
NavigateBackFunction,
TwitterSocialShareBtn,
useNavigateBack,
UserProfile,
} from '../../../../lib'
import {
Expand Down Expand Up @@ -41,7 +42,7 @@ interface CertificateViewProps {

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

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

const handleBackBtnClick: () => void = useCallback(() => {
navigate(coursePath)
}, [coursePath, navigate])
navigateBack(coursePath)
}, [coursePath, navigateBack])

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { FC, MutableRefObject, useCallback, useEffect, useMemo, useRef } from 'react'
import { NavigateFunction, useNavigate } from 'react-router-dom'
import classNames from 'classnames'

import {
Expand All @@ -8,7 +7,9 @@ import {
IconOutline,
LinkedinSocialShareBtn,
LoadingSpinner,
NavigateBackFunction,
TwitterSocialShareBtn,
useNavigateBack,
UserProfile,
} from '../../../../lib'
import {
Expand Down Expand Up @@ -38,7 +39,7 @@ interface CertificateViewProps {

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

const navigate: NavigateFunction = useNavigate()
const navigateBack: NavigateBackFunction = useNavigateBack()
const tcaCertificationPath: string = getTCACertificationPath(props.certification)
const certificateElRef: MutableRefObject<HTMLDivElement | any> = useRef()

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

const handleBackBtnClick: () => void = useCallback(() => {
navigate(tcaCertificationPath)
}, [tcaCertificationPath, navigate])
navigateBack(tcaCertificationPath)
}, [tcaCertificationPath, navigateBack])

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

Expand Down