PromucFlow_constructor/app/client/src/pages/common/ProtectedRoute.tsx

48 lines
1.2 KiB
TypeScript
Raw Normal View History

import React from "react";
2019-12-16 08:49:10 +00:00
import { Route } from "react-router-dom";
import { useDispatch } from "react-redux";
import { useSelector } from "store";
import { hasAuthExpired } from "utils/storage";
import { User } from "constants/userConstants";
2020-01-03 08:49:47 +00:00
import { setCurrentUserDetails } from "actions/userActions";
2020-01-27 11:39:27 +00:00
import { useShowPropertyPane } from "utils/hooks/dragResizeHooks";
export const checkAuth = (dispatch: any, currentUser?: User) => {
return hasAuthExpired().then(hasExpired => {
if (!currentUser || hasExpired) {
2020-01-03 08:49:47 +00:00
dispatch(setCurrentUserDetails());
}
});
};
export const WrappedComponent = (props: any) => {
2020-01-27 11:39:27 +00:00
const showPropertyPane = useShowPropertyPane();
showPropertyPane();
const dispatch = useDispatch();
const currentUser = useSelector(state => state.ui.users.current);
checkAuth(dispatch, currentUser);
return currentUser ? props.children : null;
};
2019-09-02 15:36:24 +00:00
const ProtectedRoute = ({
component: Component,
...rest
}: {
path: string;
component: React.ReactType;
exact?: boolean;
}) => {
return (
<Route
{...rest}
render={props => (
<WrappedComponent {...props}>
<Component {...props} />
</WrappedComponent>
)}
/>
);
};
2019-09-02 14:50:01 +00:00
export default ProtectedRoute;