Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | 631x 631x 631x 631x 631x 631x 289x 289x 289x 289x 3x 3x 3x 3x 3x 289x 289x 289x 62x 62x 289x | /*
* Copyright 2021 Harness Inc. All rights reserved.
* Use of this source code is governed by the PolyForm Shield 1.0.0 license
* that can be found in the licenses directory at the root of this repository, also available at
* https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
*/
import React, { useState, useEffect } from 'react'
import { Prompt } from 'react-router-dom'
import { useConfirmationDialog } from '@wings-software/uicore'
import { Intent } from '@harness/design-system'
import type * as History from 'history'
import { useStrings } from 'framework/strings'
interface Props {
when?: boolean
textProps?: {
contentText?: string
titleText?: string
confirmButtonText?: string
cancelButtonText?: string
}
navigate: (path: string) => void
shouldBlockNavigation?: (location: History.Location) => boolean
}
export const NavigationCheck = ({ when, navigate, shouldBlockNavigation, textProps }: Props): JSX.Element => {
const [lastLocation, setLastLocation] = useState<History.Location | null>(null)
const [confirmedNavigation, setConfirmedNavigation] = useState(false)
const { getString } = useStrings()
const handleBlockedNavigation = (nextLocation: History.Location): string | boolean => {
Eif (!confirmedNavigation) {
Eif (!shouldBlockNavigation || (shouldBlockNavigation && shouldBlockNavigation(nextLocation))) {
openDialog()
setLastLocation(nextLocation)
return false
}
}
return true
}
const handleConfirmNavigationClick = (): void => {
setConfirmedNavigation(true)
}
const { openDialog } = useConfirmationDialog({
cancelButtonText: textProps?.cancelButtonText || getString('cancel'),
contentText: textProps?.contentText || getString('navigationCheckText'),
titleText: textProps?.titleText || getString('navigationCheckTitle'),
confirmButtonText: textProps?.confirmButtonText || getString('confirm'),
intent: Intent.WARNING,
onCloseDialog: isConfirmed => {
if (isConfirmed) {
handleConfirmNavigationClick()
}
}
})
useEffect(() => {
Iif (confirmedNavigation && lastLocation) {
// Navigate to the previous blocked location with your navigate function
navigate(lastLocation.pathname + lastLocation.search)
}
// reset back to false
confirmedNavigation && setConfirmedNavigation(false)
}, [confirmedNavigation, lastLocation])
return (
<>
<Prompt when={when} message={handleBlockedNavigation} />
</>
)
}
|