-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Admin Stack: Show prompt if navigating into a nested Stack, even if s…
…ubroutes would normally not show prompt (#1955) This adds: - a new `ForcePromptRoute`, to be used like react-router Route, that also registers it's path in a new context created by `RouterPrompt` - `PromptHandler` can now check if the navigating-to-url matches with a path from registered `ForcePromptRoute` - and show the Prompt Also adjust `StackSwitch`: - to use the new `ForcePromptRoute` - don't use react-router `Switch` as that would only render the first matching route (but we need `ForcePromptRoute` to always render so it can register) - instead implement the logic from `Switch` by only rendering index if no other route matched --------- Co-authored-by: Johannes Obermair <[email protected]>
- Loading branch information
1 parent
a589188
commit a0bd09a
Showing
10 changed files
with
226 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@comet/admin": minor | ||
--- | ||
|
||
Add `ForcePromptRoute`, a `Route` that triggers a prompt even if it is a subroute | ||
|
||
Used in `StackSwitch` so that navigating to a nested stack subpage will show a prompt (if dirty) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as React from "react"; | ||
import { Route, RouteProps } from "react-router"; | ||
import useConstant from "use-constant"; | ||
import { v4 as uuid } from "uuid"; | ||
|
||
import { PromptContext } from "./Prompt"; | ||
|
||
export function ForcePromptRoute(props: RouteProps) { | ||
const id = useConstant<string>(() => uuid()); | ||
const context = React.useContext(PromptContext); | ||
React.useEffect(() => { | ||
if (context) { | ||
context.register(id, props.path as string); | ||
} | ||
return function cleanup() { | ||
if (context) { | ||
context.unregister(id); | ||
} | ||
}; | ||
}); | ||
return <Route {...props} />; | ||
} |
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
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
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,66 @@ | ||
import { ForcePromptRoute, RouterPrompt } from "@comet/admin"; | ||
import { storiesOf } from "@storybook/react"; | ||
import * as React from "react"; | ||
import { Redirect, Route, Switch, useLocation } from "react-router"; | ||
import { Link } from "react-router-dom"; | ||
|
||
import { storyRouterDecorator } from "../../story-router.decorator"; | ||
|
||
function Story() { | ||
return ( | ||
<Switch> | ||
<Route path="/foo"> | ||
<RouterPrompt | ||
message={() => { | ||
return "rly?"; | ||
}} | ||
subRoutePath="/foo" | ||
> | ||
<ul> | ||
<li> | ||
<Link to="/foo/sub1">/foo/sub1</Link> (no prompt) | ||
</li> | ||
<li> | ||
<Link to="/foo/sub2">/foo/sub2</Link> (force prompt) | ||
</li> | ||
<li> | ||
<Link to="/foo">/foo</Link> (back) | ||
</li> | ||
</ul> | ||
<Route path="/foo/sub1"> | ||
<div>sub1</div> | ||
</Route> | ||
<ForcePromptRoute path="/foo/sub2"> | ||
<div>sub2</div> | ||
</ForcePromptRoute> | ||
</RouterPrompt> | ||
</Route> | ||
<Redirect to="/foo" /> | ||
</Switch> | ||
); | ||
} | ||
|
||
function Path() { | ||
const location = useLocation(); | ||
const [, rerender] = React.useState(0); | ||
React.useEffect(() => { | ||
const timer = setTimeout(() => { | ||
rerender(new Date().getTime()); | ||
}, 1000); | ||
return () => clearTimeout(timer); | ||
}, []); | ||
return <div>{location.pathname}</div>; | ||
} | ||
|
||
function App() { | ||
return ( | ||
<> | ||
<Path /> | ||
<Story /> | ||
</> | ||
); | ||
} | ||
|
||
storiesOf("@comet/admin/router", module) | ||
.addDecorator(storyRouterDecorator()) | ||
.add("ForcePromptRoute", () => <App />); |