DocSpace-client/packages/asc-web-common/components/PrivateRoute/index.js

177 lines
4.0 KiB
JavaScript
Raw Normal View History

2021-03-18 18:54:03 +00:00
/* eslint-disable react/prop-types */
import React, { useEffect } from "react";
import { Redirect, Route } from "react-router-dom";
//import Loader from "@appserver/components/loader";
//import Section from "../Section";
2021-03-18 18:54:03 +00:00
// import Error401 from "studio/Error401";
// import Error404 from "studio/Error404";
import AppLoader from "../AppLoader";
import { inject, observer } from "mobx-react";
import { isMe } from "../../utils";
2021-03-22 14:35:33 +00:00
import combineUrl from "../../utils/combineUrl";
import { AppServerConfig } from "../../constants";
2021-03-18 18:54:03 +00:00
const PrivateRoute = ({ component: Component, ...rest }) => {
const {
isAdmin,
isAuthenticated,
isLoaded,
restricted,
allowForMe,
user,
computedMatch,
setModuleInfo,
modules,
2021-05-20 08:27:50 +00:00
currentProductId,
2021-03-18 18:54:03 +00:00
wizardCompleted,
personal,
location,
2021-03-18 18:54:03 +00:00
} = rest;
2021-05-20 08:27:50 +00:00
const { params, path } = computedMatch;
const { userId } = params;
2021-03-18 18:54:03 +00:00
const renderComponent = (props) => {
if (isLoaded && !isAuthenticated) {
2021-09-07 08:38:24 +00:00
if (personal) {
window.location.replace("/");
return <></>;
}
2021-03-18 18:54:03 +00:00
console.log("PrivateRoute render Redirect to login", rest);
return (
<Redirect
to={{
2021-03-22 14:35:33 +00:00
pathname: combineUrl(
AppServerConfig.proxyURL,
wizardCompleted ? "/login" : "/wizard"
),
2021-03-18 18:54:03 +00:00
state: { from: props.location },
}}
/>
);
}
if (location.pathname === "/" && personal) {
return (
<Redirect
to={{
pathname: "/products/files",
state: { from: props.location },
}}
/>
);
}
2021-03-18 18:54:03 +00:00
if (!isLoaded) {
return <AppLoader />;
}
// const userLoaded = !isEmpty(user);
// if (!userLoaded) {
// return <Component {...props} />;
// }
// if (!userLoaded) {
// console.log("PrivateRoute render Loader", rest);
// return (
// <Section>
// <Section.SectionBody>
2021-03-18 18:54:03 +00:00
// <Loader className="pageLoader" type="rombs" size="40px" />
// </Section.SectionBody>
// </Section>
2021-03-18 18:54:03 +00:00
// );
// }
if (
!restricted ||
isAdmin ||
(allowForMe && userId && isMe(user, userId))
) {
// console.log(
// "PrivateRoute render Component",
// rest,
// Component.name || Component.displayName
// );
return <Component {...props} {...rest} />;
}
if (restricted) {
console.log("PrivateRoute render Error401", rest);
return (
<Redirect
to={{
pathname: "/error401",
state: { from: props.location },
}}
/>
);
}
console.log("PrivateRoute render Error404", rest);
return (
<Redirect
to={{
pathname: "/error404",
state: { from: props.location },
}}
/>
);
};
useEffect(() => {
2021-05-20 08:27:50 +00:00
if (!isLoaded) return;
let currentModule;
if (path === "" || path === "/") {
currentModule = {
id: "home",
origLink: "/",
};
} else if (path.startsWith("/my")) {
currentModule = {
id: "f4d98afd-d336-4332-8778-3c6945c81ea0",
origLink: "/products/people",
};
} else {
currentModule = modules.find((m) => m.link.startsWith(path));
2021-03-18 18:54:03 +00:00
}
2021-05-20 08:27:50 +00:00
if (!currentModule) return;
const { id, origLink, link } = currentModule;
setModuleInfo(origLink || link, id);
}, [path, modules, isLoaded]);
2021-03-18 18:54:03 +00:00
//console.log("PrivateRoute render", rest);
return <Route {...rest} render={renderComponent} />;
};
export default inject(({ auth }) => {
const {
userStore,
isAuthenticated,
isLoaded,
isAdmin,
settingsStore,
moduleStore,
} = auth;
const { user } = userStore;
const { modules } = moduleStore;
const { setModuleInfo, wizardCompleted, personal } = settingsStore;
2021-03-18 18:54:03 +00:00
return {
modules,
user,
isAuthenticated,
isAdmin,
isLoaded,
setModuleInfo,
wizardCompleted,
personal,
2021-03-18 18:54:03 +00:00
};
})(observer(PrivateRoute));