-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fallback for private routes, allow multiple elements in <Walls>
- Loading branch information
Showing
4 changed files
with
51 additions
and
36 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 |
---|---|---|
@@ -1,25 +1,27 @@ | ||
import React, { FunctionComponent, ReactElement } from "react"; | ||
import { Route, withRouter } from "react-router-dom"; | ||
import React, { FunctionComponent } from "react"; | ||
import { Route } from "react-router-dom"; | ||
|
||
interface Props { | ||
isAuthorized: boolean; | ||
onUnauthorized: (props: any) => void; | ||
onUnauthorized?: (props: any) => void; | ||
fallback?: FunctionComponent<any>; | ||
} | ||
|
||
const PrivateRoute: FunctionComponent<Props> = ({ | ||
isAuthorized, | ||
onUnauthorized, | ||
fallback, | ||
...props | ||
}): ReactElement<any> | null => { | ||
}) => { | ||
if (!isAuthorized) { | ||
if (typeof onUnauthorized === "function") { | ||
onUnauthorized(props); | ||
} | ||
|
||
return null; | ||
return <Route {...props}>{fallback}</Route>; | ||
} | ||
|
||
return <Route {...props} />; | ||
}; | ||
|
||
export default withRouter(PrivateRoute as any); | ||
export default PrivateRoute; |
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 |
---|---|---|
@@ -1,26 +1,40 @@ | ||
import React, { ReactElement } from "react"; | ||
import React, { FunctionComponent, ReactElement, useState } from "react"; | ||
import { NavLink } from "react-router-dom"; | ||
|
||
import Walls from "../../../index"; | ||
|
||
import routes from "./routes"; | ||
|
||
const Container = (): ReactElement<any> => ( | ||
<Walls routes={routes}> | ||
<nav> | ||
<ul> | ||
<li> | ||
<NavLink to="/">Home</NavLink> | ||
</li> | ||
<li> | ||
<NavLink to="/foo">Foo</NavLink> | ||
</li> | ||
<li> | ||
<NavLink to="/bar">Bar (Private)</NavLink> | ||
</li> | ||
</ul> | ||
</nav> | ||
</Walls> | ||
); | ||
const Container: FunctionComponent<any> = () => { | ||
const [isAuthorized, setIsAuthorized] = useState(false); | ||
return ( | ||
<Walls routes={routes} isAuthorized={isAuthorized}> | ||
<div style={{ display: "flex" }}> | ||
<nav> | ||
<ul> | ||
<li> | ||
<NavLink to="/">Home</NavLink> | ||
</li> | ||
<li> | ||
<NavLink to="/foo">Foo</NavLink> | ||
</li> | ||
<li> | ||
<NavLink to="/bar">Bar (Private)</NavLink> | ||
</li> | ||
</ul> | ||
</nav> | ||
|
||
<div> | ||
<label htmlFor="isAuthorized">Is Authorized?</label> | ||
<input | ||
type="checkbox" | ||
onChange={() => setIsAuthorized(!isAuthorized)} | ||
id="isAuthorized" | ||
/> | ||
</div> | ||
</div> | ||
</Walls> | ||
); | ||
}; | ||
|
||
export default Container; |
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