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";
|
2022-03-16 12:22:48 +00:00
|
|
|
//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";
|
2022-03-24 17:56:51 +00:00
|
|
|
import { AppServerConfig, TenantStatus } 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,
|
2021-06-03 13:30:56 +00:00
|
|
|
personal,
|
|
|
|
location,
|
2022-03-24 17:56:51 +00:00
|
|
|
tenantStatus,
|
2021-03-18 18:54:03 +00:00
|
|
|
} = rest;
|
2022-03-24 17:56:51 +00:00
|
|
|
const isPortal = window.location.pathname === "/preparation-portal";
|
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) {
|
2021-08-24 08:47:00 +00:00
|
|
|
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 },
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-03 13:30:56 +00:00
|
|
|
if (location.pathname === "/" && personal) {
|
|
|
|
return (
|
|
|
|
<Redirect
|
|
|
|
to={{
|
|
|
|
pathname: "/products/files",
|
|
|
|
state: { from: props.location },
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:56:51 +00:00
|
|
|
if (
|
|
|
|
isLoaded &&
|
|
|
|
isAuthenticated &&
|
|
|
|
tenantStatus === TenantStatus.PortalRestore &&
|
|
|
|
!isPortal
|
|
|
|
) {
|
|
|
|
return (
|
|
|
|
<Redirect
|
|
|
|
to={{
|
|
|
|
pathname: combineUrl(
|
|
|
|
AppServerConfig.proxyURL,
|
|
|
|
"/preparation-portal"
|
|
|
|
),
|
|
|
|
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 (
|
2022-03-16 12:22:48 +00:00
|
|
|
// <Section>
|
|
|
|
// <Section.SectionBody>
|
2021-03-18 18:54:03 +00:00
|
|
|
// <Loader className="pageLoader" type="rombs" size="40px" />
|
2022-03-16 12:22:48 +00:00
|
|
|
// </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;
|
2022-03-24 17:56:51 +00:00
|
|
|
const {
|
|
|
|
setModuleInfo,
|
|
|
|
wizardCompleted,
|
|
|
|
personal,
|
|
|
|
tenantStatus,
|
|
|
|
} = settingsStore;
|
2021-03-18 18:54:03 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
modules,
|
|
|
|
user,
|
|
|
|
isAuthenticated,
|
|
|
|
isAdmin,
|
|
|
|
isLoaded,
|
|
|
|
setModuleInfo,
|
|
|
|
wizardCompleted,
|
2022-03-24 17:56:51 +00:00
|
|
|
tenantStatus,
|
2021-06-03 13:30:56 +00:00
|
|
|
personal,
|
2021-03-18 18:54:03 +00:00
|
|
|
};
|
|
|
|
})(observer(PrivateRoute));
|