-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathKIProtectedRoute.jsx
More file actions
51 lines (48 loc) · 1.62 KB
/
KIProtectedRoute.jsx
File metadata and controls
51 lines (48 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* eslint-disable react/jsx-props-no-spreading */
import { Redirect, Route } from 'react-router-dom';
import { connect } from 'react-redux';
import { Suspense } from 'react';
// eslint-disable-next-line react/function-component-definition
const KIProtectedRoute = ({ component: Component, render, auth, fallback, ...rest }) => {
return (
<Route
{...rest}
render={props => {
if (!auth.isAuthenticated) {
return <Redirect to={{ pathname: '/login', state: { from: props.location } }} />;
}
// TODO: Replace with a proper KI-specific permission check (e.g. canAccessKIPortal)
// if (auth.user.access && !auth.user.access.canAccessKIPortal) {
// return (
// <Redirect
// to={{ pathname: '/kitchenandinventory/login', state: { from: props.location } }}
// />
// );
// }
// eslint-disable-next-line no-nested-ternary
return Component && fallback ? (
<Suspense
fallback={
<div className="d-flex justify-content-center">
<i className="fa fa-spinner fa-pulse" />
</div>
}
>
{' '}
<Component {...props} />{' '}
</Suspense>
) : Component ? (
<Component {...props} />
) : (
render(props)
);
}}
/>
);
};
const mapStateToProps = state => ({
auth: state.auth,
// Note: roles props won't be used until permissions added to Kitchen and Inventory Dashboard
roles: state.role.roles,
});
export default connect(mapStateToProps)(KIProtectedRoute);