Skip to content

Commit

Permalink
chore(routing): Switch to new react-router syntax (#638)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendan Mulholland authored Oct 2, 2023
1 parent 81517f2 commit d5d0243
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
HashRouter as Router,
Route,
Switch,
useLocation,
} from 'react-router-dom';

import { AppContext, AppProvider } from './context/App';
Expand All @@ -15,24 +16,16 @@ import { NotificationsRoute } from './routes/Notifications';
import { SettingsRoute } from './routes/Settings';
import { Sidebar } from './components/Sidebar';

export const PrivateRoute = ({ component: Component, ...rest }) => {
function RequireAuth({ children }) {
const { isLoggedIn } = useContext(AppContext);
const location = useLocation();

return (
<Route
{...rest}
render={(props) =>
isLoggedIn ? (
<Component {...props} />
) : (
<Redirect
to={{ pathname: '/login', state: { from: props.location } }}
/>
)
}
/>
return isLoggedIn ? (
children
) : (
<Redirect to={{ pathname: '/login', state: { from: location } }} />
);
};
}

export const App = () => {
return (
Expand All @@ -43,11 +36,28 @@ export const App = () => {
<Sidebar />

<Switch>
<PrivateRoute path="/" exact component={NotificationsRoute} />
<PrivateRoute path="/settings" exact component={SettingsRoute} />
<Route path="/login" component={LoginRoute} />
<Route path="/login-enterprise" component={LoginEnterpriseRoute} />
<Route path="/login-token" component={LoginWithToken} />
<Route path="/" exact>
<RequireAuth>
<NotificationsRoute />
</RequireAuth>
</Route>
<Route path="/settings" exact>
<RequireAuth>
<SettingsRoute />
</RequireAuth>
</Route>
<Route path="/login">
<LoginRoute />
</Route>
<Route path="/login">
<LoginRoute />
</Route>
<Route path="/login-enterprise">
<LoginEnterpriseRoute />
</Route>
<Route path="/login-token">
<LoginWithToken />
</Route>
</Switch>
</div>
</Router>
Expand Down

0 comments on commit d5d0243

Please sign in to comment.