-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dataset undo/redo refactoring (#1957)
- Loading branch information
Showing
16 changed files
with
451 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/ui/units/datasets/components/DatasetRouter/UnloadConfirmation.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React from 'react'; | ||
|
||
import type History from 'history'; | ||
import {i18n} from 'i18n'; | ||
import {useSelector} from 'react-redux'; | ||
import {Prompt, useLocation} from 'react-router-dom'; | ||
import {selectIsRenameWithoutReload} from 'ui/store/selectors/entryContent'; | ||
import { | ||
isDatasetChangedDatasetSelector, | ||
isDatasetRevisionMismatchSelector, | ||
isSavingDatasetDisabledSelector, | ||
} from 'ui/units/datasets/store/selectors'; | ||
|
||
export const UnloadConfirmation = () => { | ||
const currentLocation = useLocation(); | ||
const isRenameWithoutReload = useSelector(selectIsRenameWithoutReload); | ||
const isSavingDatasetDisabled = useSelector(isSavingDatasetDisabledSelector); | ||
const isDatasetRevisionMismatch = useSelector(isDatasetRevisionMismatchSelector); | ||
const isDatasetChangedDataset = useSelector(isDatasetChangedDatasetSelector); | ||
const [isAlreadyConfirmed, setIsAlreadyConfirmed] = React.useState(false); | ||
const isConfirmationAvailable = | ||
!isAlreadyConfirmed && | ||
(!isSavingDatasetDisabled || isDatasetChangedDataset) && | ||
!isRenameWithoutReload && | ||
!isDatasetRevisionMismatch; | ||
|
||
const message = React.useCallback( | ||
(location: History.Location<unknown>) => { | ||
setIsAlreadyConfirmed(true); | ||
setTimeout(() => setIsAlreadyConfirmed(false), 0); | ||
if (location.pathname !== currentLocation.pathname) { | ||
return i18n('component.navigation-prompt', 'label_prompt-message'); | ||
} | ||
|
||
return true; | ||
}, | ||
[currentLocation.pathname], | ||
); | ||
|
||
React.useEffect(() => { | ||
const beforeUnloadHandler = (event: BeforeUnloadEvent) => { | ||
if (isConfirmationAvailable) { | ||
// eslint-disable-next-line no-param-reassign | ||
event.returnValue = true; | ||
} | ||
}; | ||
|
||
window.addEventListener('beforeunload', beforeUnloadHandler); | ||
|
||
return () => { | ||
window.removeEventListener('beforeunload', beforeUnloadHandler); | ||
}; | ||
}, [isConfirmationAvailable]); | ||
|
||
return <Prompt when={isConfirmationAvailable} message={message} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.