Skip to content

Commit

Permalink
Merge pull request #37 from Zextras/fix--fix-nav-guard-behaviour-arou…
Browse files Browse the repository at this point in the history
…nd-subsections

fix: fixed nav-guard behaviour around settings subsection
  • Loading branch information
zovomat authored Mar 17, 2022
2 parents 7506a0c + 2205529 commit 5450edb
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/ui-extras/nav-guard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/

import React, { useEffect, useState, FC } from 'react';
import React, { useEffect, useState, FC, useMemo } from 'react';
import { Location } from 'history';
import { Prompt, useHistory } from 'react-router-dom';
import { Modal } from '@zextras/carbonio-design-system';
Expand All @@ -15,16 +15,20 @@ export const RouteLeavingGuard: FC<{
onSave: () => void;
}> = ({ children, when, onSave }) => {
const history = useHistory();
const lastLocationInitial = useMemo(() => history.location.pathname, [history]);
const [modalVisible, setModalVisible] = useState(false);
const [lastLocation, setLastLocation] = useState<Location | null>(null);
const [lastLocation, setLastLocation] = useState<Location>(lastLocationInitial);
const [confirmedNavigation, setConfirmedNavigation] = useState(false);
const [t] = useTranslation();
const onClose = (): void => {
setModalVisible(false);
setConfirmedNavigation(true);
};
const handleBlockedNavigation = (nextLocation: Location): boolean => {
if (!confirmedNavigation) {
if (
!confirmedNavigation &&
nextLocation.pathname !== (lastLocation?.pathname || lastLocationInitial)
) {
setModalVisible(true);
setLastLocation(nextLocation);
return false;
Expand Down

0 comments on commit 5450edb

Please sign in to comment.