-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
183 additions
and
95 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
65 changes: 65 additions & 0 deletions
65
src/Geopilot.Frontend/src/components/controlledNavigate/controlledNavigateProvider.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,65 @@ | ||
import { createContext, FC, PropsWithChildren, useState } from "react"; | ||
import { useNavigate } from "react-router-dom"; | ||
|
||
export const ControlledNavigateContext = createContext({ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
navigateTo: (path: string) => {}, | ||
checkIsDirty: false, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
registerCheckIsDirty: (path: string) => {}, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
unregisterCheckIsDirty: (path: string) => {}, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
leaveEditingPage: (canLeave: boolean) => {}, | ||
}); | ||
|
||
export const ControlledNavigateProvider: FC<PropsWithChildren> = ({ children }) => { | ||
const [path, setPath] = useState<string>(); | ||
const [registeredEditPages, setRegisteredEditPages] = useState<string[]>([]); | ||
const [checkIsDirty, setCheckIsDirty] = useState<boolean>(false); | ||
const navigate = useNavigate(); | ||
|
||
const registerCheckIsDirty = (path: string) => { | ||
if (!registeredEditPages.includes(path)) { | ||
setRegisteredEditPages([...registeredEditPages, path]); | ||
} | ||
}; | ||
|
||
const unregisterCheckIsDirty = (path: string) => { | ||
setRegisteredEditPages(registeredEditPages.filter(value => value !== path)); | ||
}; | ||
|
||
const navigateTo = (path: string) => { | ||
if ( | ||
registeredEditPages.find(value => { | ||
return window.location.pathname.includes(value); | ||
}) | ||
) { | ||
setPath(path); | ||
setCheckIsDirty(true); | ||
} else { | ||
navigate(path); | ||
} | ||
}; | ||
|
||
const leaveEditingPage = (canLeave: boolean) => { | ||
if (canLeave && path) { | ||
navigate(path); | ||
} | ||
setCheckIsDirty(false); | ||
setPath(undefined); | ||
}; | ||
|
||
return ( | ||
<ControlledNavigateContext.Provider | ||
value={{ | ||
navigateTo, | ||
registerCheckIsDirty, | ||
unregisterCheckIsDirty, | ||
checkIsDirty, | ||
leaveEditingPage, | ||
}}> | ||
{children} | ||
</ControlledNavigateContext.Provider> | ||
); | ||
}; |
4 changes: 4 additions & 0 deletions
4
src/Geopilot.Frontend/src/components/controlledNavigate/index.ts
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,4 @@ | ||
import { useContext } from "react"; | ||
import { ControlledNavigateContext } from "./controlledNavigateProvider.tsx"; | ||
|
||
export const useControlledNavigate = () => useContext(ControlledNavigateContext); |
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
Oops, something went wrong.