mirror of
https://github.com/IRBorisov/ConceptPortal.git
synced 2025-06-26 13:00:39 +03:00
Fix navigation
This commit is contained in:
parent
fe2fe4ee5a
commit
9c16c3fdb7
|
@ -79,18 +79,17 @@ const router = createBrowserRouter([
|
||||||
path: 'manuals',
|
path: 'manuals',
|
||||||
element: <ManualsPage />,
|
element: <ManualsPage />,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
path: 'library',
|
path: 'library',
|
||||||
element: <LibraryPage />,
|
element: <LibraryPage />,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'rsforms/:id',
|
path: 'library/create',
|
||||||
element: <RSFormPage />,
|
element: <CreateRSFormPage />,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: 'rsform-create',
|
path: 'rsforms/:id',
|
||||||
element: <CreateRSFormPage />,
|
element: <RSFormPage />,
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { createContext, useCallback, useContext, useEffect, useState } from 'react';
|
import { createContext, useCallback, useContext, useEffect, useState } from 'react';
|
||||||
import { unstable_usePrompt, useLocation, useNavigate } from 'react-router-dom';
|
import { useLocation, useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
import { globalIDs } from '@/utils/constants';
|
import { globalIDs } from '@/utils/constants';
|
||||||
|
|
||||||
|
@ -35,15 +35,15 @@ export const NavigationState = ({ children }: NavigationStateProps) => {
|
||||||
const { pathname } = useLocation();
|
const { pathname } = useLocation();
|
||||||
|
|
||||||
const [isBlocked, setIsBlocked] = useState(false);
|
const [isBlocked, setIsBlocked] = useState(false);
|
||||||
unstable_usePrompt({
|
const validate = useCallback(
|
||||||
when: isBlocked,
|
|
||||||
message: 'Изменения не сохранены. Вы уверены что хотите совершить переход?'
|
|
||||||
});
|
|
||||||
|
|
||||||
const canBack = useCallback(
|
|
||||||
() => {
|
() => {
|
||||||
return (!!window.history && window.history?.length !== 0);
|
return (
|
||||||
}, []);
|
!isBlocked ||
|
||||||
|
confirm('Изменения не сохранены. Вы уверены что хотите совершить переход?')
|
||||||
|
);
|
||||||
|
}, [isBlocked]);
|
||||||
|
|
||||||
|
const canBack = useCallback(() => (!!window.history && window.history?.length !== 0), []);
|
||||||
|
|
||||||
const scrollTop = useCallback(
|
const scrollTop = useCallback(
|
||||||
() => {
|
() => {
|
||||||
|
@ -56,31 +56,39 @@ export const NavigationState = ({ children }: NavigationStateProps) => {
|
||||||
|
|
||||||
const push = useCallback(
|
const push = useCallback(
|
||||||
(path: string) => {
|
(path: string) => {
|
||||||
|
if (validate()) {
|
||||||
scrollTop();
|
scrollTop();
|
||||||
setIsBlocked(false);
|
|
||||||
router(path);
|
router(path);
|
||||||
}, [router, scrollTop]);
|
setIsBlocked(false);
|
||||||
|
}
|
||||||
|
}, [router, validate, scrollTop]);
|
||||||
|
|
||||||
const replace = useCallback(
|
const replace = useCallback(
|
||||||
(path: string) => {
|
(path: string) => {
|
||||||
|
if (validate()) {
|
||||||
scrollTop();
|
scrollTop();
|
||||||
setIsBlocked(false);
|
|
||||||
router(path, {replace: true});
|
router(path, {replace: true});
|
||||||
}, [router, scrollTop]);
|
setIsBlocked(false);
|
||||||
|
}
|
||||||
|
}, [router, validate, scrollTop]);
|
||||||
|
|
||||||
const back = useCallback(
|
const back = useCallback(
|
||||||
() => {
|
() => {
|
||||||
|
if (validate()) {
|
||||||
scrollTop();
|
scrollTop();
|
||||||
setIsBlocked(false);
|
|
||||||
router(-1);
|
router(-1);
|
||||||
}, [router, scrollTop]);
|
setIsBlocked(false);
|
||||||
|
}
|
||||||
|
}, [router, validate, scrollTop]);
|
||||||
|
|
||||||
const forward = useCallback(
|
const forward = useCallback(
|
||||||
() => {
|
() => {
|
||||||
|
if (validate()) {
|
||||||
scrollTop();
|
scrollTop();
|
||||||
setIsBlocked(false);
|
|
||||||
router(1);
|
router(1);
|
||||||
}, [router, scrollTop]);
|
setIsBlocked(false);
|
||||||
|
}
|
||||||
|
}, [router, validate, scrollTop]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
scrollTop();
|
scrollTop();
|
||||||
|
@ -94,3 +102,12 @@ export const NavigationState = ({ children }: NavigationStateProps) => {
|
||||||
{children}
|
{children}
|
||||||
</NagivationContext.Provider>);
|
</NagivationContext.Provider>);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function useBlockNavigation(isBlocked: boolean) {
|
||||||
|
const router = useConceptNavigation();
|
||||||
|
useEffect(
|
||||||
|
() => {
|
||||||
|
router.setIsBlocked(isBlocked);
|
||||||
|
return () => router.setIsBlocked(false);
|
||||||
|
}, [router, isBlocked]);
|
||||||
|
}
|
|
@ -11,7 +11,7 @@ import ConceptTab from '@/components/Common/ConceptTab';
|
||||||
import TextURL from '@/components/Common/TextURL';
|
import TextURL from '@/components/Common/TextURL';
|
||||||
import InfoError, { ErrorData } from '@/components/InfoError';
|
import InfoError, { ErrorData } from '@/components/InfoError';
|
||||||
import { useLibrary } from '@/context/LibraryContext';
|
import { useLibrary } from '@/context/LibraryContext';
|
||||||
import { useConceptNavigation } from '@/context/NagivationContext';
|
import { useBlockNavigation, useConceptNavigation } from '@/context/NagivationContext';
|
||||||
import { useRSForm } from '@/context/RSFormContext';
|
import { useRSForm } from '@/context/RSFormContext';
|
||||||
import { useConceptTheme } from '@/context/ThemeContext';
|
import { useConceptTheme } from '@/context/ThemeContext';
|
||||||
import DlgCloneLibraryItem from '@/dialogs/DlgCloneLibraryItem';
|
import DlgCloneLibraryItem from '@/dialogs/DlgCloneLibraryItem';
|
||||||
|
@ -66,6 +66,7 @@ function RSTabs() {
|
||||||
const { setNoFooter, noNavigation } = useConceptTheme();
|
const { setNoFooter, noNavigation } = useConceptTheme();
|
||||||
|
|
||||||
const [isModified, setIsModified] = useState(false);
|
const [isModified, setIsModified] = useState(false);
|
||||||
|
useBlockNavigation(isModified);
|
||||||
|
|
||||||
const [activeTab, setActiveTab] = useState(RSTabID.CARD);
|
const [activeTab, setActiveTab] = useState(RSTabID.CARD);
|
||||||
const [activeID, setActiveID] = useState<number | undefined>(undefined);
|
const [activeID, setActiveID] = useState<number | undefined>(undefined);
|
||||||
|
|
|
@ -1,17 +1,16 @@
|
||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useEffect, useLayoutEffect, useMemo, useState } from 'react';
|
import { useLayoutEffect, useMemo, useState } from 'react';
|
||||||
import { toast } from 'react-toastify';
|
import { toast } from 'react-toastify';
|
||||||
|
|
||||||
import SubmitButton from '@/components/Common/SubmitButton';
|
import SubmitButton from '@/components/Common/SubmitButton';
|
||||||
import TextInput from '@/components/Common/TextInput';
|
import TextInput from '@/components/Common/TextInput';
|
||||||
import { useConceptNavigation } from '@/context/NagivationContext';
|
import { useBlockNavigation } from '@/context/NagivationContext';
|
||||||
import { useUserProfile } from '@/context/UserProfileContext';
|
import { useUserProfile } from '@/context/UserProfileContext';
|
||||||
import { IUserUpdateData } from '@/models/library';
|
import { IUserUpdateData } from '@/models/library';
|
||||||
|
|
||||||
function EditorProfile() {
|
function EditorProfile() {
|
||||||
const { updateUser, user, processing } = useUserProfile();
|
const { updateUser, user, processing } = useUserProfile();
|
||||||
const router = useConceptNavigation();
|
|
||||||
|
|
||||||
const [username, setUsername] = useState('');
|
const [username, setUsername] = useState('');
|
||||||
const [email, setEmail] = useState('');
|
const [email, setEmail] = useState('');
|
||||||
|
@ -29,12 +28,7 @@ function EditorProfile() {
|
||||||
user.last_name !== last_name
|
user.last_name !== last_name
|
||||||
);
|
);
|
||||||
}, [user, email, first_name, last_name]);
|
}, [user, email, first_name, last_name]);
|
||||||
|
useBlockNavigation(isModified);
|
||||||
useEffect(
|
|
||||||
() => {
|
|
||||||
router.setIsBlocked(isModified);
|
|
||||||
return () => router.setIsBlocked(false);
|
|
||||||
}, [router, isModified]);
|
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
if (user) {
|
if (user) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user