2021-03-18 18:54:03 +00:00
|
|
|
/* eslint-disable react/prop-types */
|
2022-12-20 16:51:49 +00:00
|
|
|
import React from "react";
|
2021-03-18 18:54:03 +00:00
|
|
|
import { Redirect, Route } from "react-router-dom";
|
2022-07-22 19:01:25 +00:00
|
|
|
//import Loader from "@docspace/components/loader";
|
2022-03-16 12:22:48 +00:00
|
|
|
//import Section from "../Section";
|
2022-08-01 12:37:33 +00:00
|
|
|
// import Error401 from "client/Error401";
|
|
|
|
// import Error404 from "client/Error404";
|
2021-03-18 18:54:03 +00:00
|
|
|
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-12-20 16:51:49 +00:00
|
|
|
import { 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,
|
2022-09-14 06:55:22 +00:00
|
|
|
isNotPaidPeriod,
|
2022-10-12 10:41:13 +00:00
|
|
|
withManager,
|
2021-03-18 18:54:03 +00:00
|
|
|
} = rest;
|
2022-09-01 14:28:08 +00:00
|
|
|
|
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) => {
|
2022-09-24 07:46:02 +00:00
|
|
|
const isPortalUrl = props.location.pathname === "/preparation-portal";
|
2022-11-09 10:22:31 +00:00
|
|
|
const isPaymentsUrl =
|
|
|
|
props.location.pathname === "/portal-settings/payments/portal-payments";
|
|
|
|
const isBackupUrl =
|
|
|
|
props.location.pathname === "/portal-settings/backup/data-backup";
|
|
|
|
const isPortalUnavailableUrl =
|
|
|
|
props.location.pathname === "/portal-unavailable";
|
|
|
|
|
|
|
|
const isPortalDeletionUrl =
|
|
|
|
props.location.pathname === "/portal-settings/delete-data/deletion" ||
|
|
|
|
props.location.pathname === "/portal-settings/delete-data/deactivation";
|
2022-09-24 07:46:02 +00:00
|
|
|
|
2021-03-18 18:54:03 +00:00
|
|
|
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);
|
2022-08-04 12:27:44 +00:00
|
|
|
const redirectPath = wizardCompleted ? "/login" : "/wizard";
|
2022-12-23 10:56:42 +00:00
|
|
|
|
|
|
|
if (wizardCompleted && props.location.pathname !== "/") {
|
|
|
|
sessionStorage.setItem("referenceUrl", window.location.href);
|
|
|
|
}
|
|
|
|
|
2022-09-05 07:48:46 +00:00
|
|
|
return window.location.replace(redirectPath);
|
2022-08-04 12:27:44 +00:00
|
|
|
// return (
|
|
|
|
// <Redirect
|
|
|
|
// to={{
|
|
|
|
// pathname: combineUrl(
|
2022-12-20 16:51:49 +00:00
|
|
|
// window.DocSpaceConfig?.proxy?.url,
|
2022-08-04 12:27:44 +00:00
|
|
|
// wizardCompleted ? "/login" : "/wizard"
|
|
|
|
// ),
|
|
|
|
// state: { from: props.location },
|
|
|
|
// }}
|
|
|
|
// />
|
|
|
|
// );
|
2021-03-18 18:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 07:32:47 +00:00
|
|
|
if (
|
|
|
|
isLoaded &&
|
2022-11-09 11:07:59 +00:00
|
|
|
((!isNotPaidPeriod && isPortalUnavailableUrl) ||
|
|
|
|
(!user.isOwner && isPortalDeletionUrl))
|
2022-09-22 07:32:47 +00:00
|
|
|
) {
|
|
|
|
return window.location.replace("/");
|
|
|
|
}
|
|
|
|
|
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 &&
|
2022-09-24 07:46:02 +00:00
|
|
|
!isPortalUrl
|
2022-03-24 17:56:51 +00:00
|
|
|
) {
|
|
|
|
return (
|
|
|
|
<Redirect
|
|
|
|
to={{
|
|
|
|
pathname: combineUrl(
|
2022-12-20 16:51:49 +00:00
|
|
|
window.DocSpaceConfig?.proxy?.url,
|
2022-03-24 17:56:51 +00:00
|
|
|
"/preparation-portal"
|
|
|
|
),
|
|
|
|
state: { from: props.location },
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-01 14:28:08 +00:00
|
|
|
if (
|
2022-09-14 06:55:22 +00:00
|
|
|
isNotPaidPeriod &&
|
2022-09-01 14:28:08 +00:00
|
|
|
isLoaded &&
|
|
|
|
(user.isOwner || user.isAdmin) &&
|
2022-11-09 10:22:31 +00:00
|
|
|
!isPaymentsUrl &&
|
|
|
|
!isBackupUrl &&
|
|
|
|
!isPortalDeletionUrl
|
2022-09-01 14:28:08 +00:00
|
|
|
) {
|
|
|
|
return (
|
|
|
|
<Redirect
|
|
|
|
to={{
|
|
|
|
pathname: combineUrl(
|
2022-12-20 16:51:49 +00:00
|
|
|
window.DocSpaceConfig?.proxy?.url,
|
2022-09-01 14:28:08 +00:00
|
|
|
"/portal-settings/payments/portal-payments"
|
|
|
|
),
|
|
|
|
state: { from: props.location },
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-24 07:46:02 +00:00
|
|
|
if (tenantStatus !== TenantStatus.PortalRestore && isPortalUrl) {
|
|
|
|
return window.location.replace("/");
|
|
|
|
}
|
|
|
|
|
2022-09-01 14:28:08 +00:00
|
|
|
if (
|
2022-09-14 06:55:22 +00:00
|
|
|
isNotPaidPeriod &&
|
2022-09-01 14:28:08 +00:00
|
|
|
isLoaded &&
|
|
|
|
!user.isOwner &&
|
|
|
|
!user.isAdmin &&
|
2022-11-09 10:22:31 +00:00
|
|
|
isPortalUnavailableUrl
|
2022-09-01 14:28:08 +00:00
|
|
|
) {
|
|
|
|
return (
|
|
|
|
<Redirect
|
|
|
|
to={{
|
|
|
|
pathname: combineUrl(
|
2022-12-20 16:51:49 +00:00
|
|
|
window.DocSpaceConfig?.proxy?.url,
|
2022-09-01 14:28:08 +00:00
|
|
|
"/portal-unavailable"
|
|
|
|
),
|
|
|
|
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 ||
|
2022-10-12 10:41:13 +00:00
|
|
|
(withManager && !user.isVisitor) ||
|
2021-03-18 18:54:03 +00:00
|
|
|
(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 },
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
//console.log("PrivateRoute render", rest);
|
|
|
|
return <Route {...rest} render={renderComponent} />;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default inject(({ auth }) => {
|
2022-09-01 14:28:08 +00:00
|
|
|
const {
|
|
|
|
userStore,
|
|
|
|
isAuthenticated,
|
|
|
|
isLoaded,
|
|
|
|
isAdmin,
|
|
|
|
settingsStore,
|
2022-09-14 06:55:22 +00:00
|
|
|
currentTariffStatusStore,
|
2022-09-01 14:28:08 +00:00
|
|
|
} = auth;
|
2022-09-14 06:55:22 +00:00
|
|
|
const { isNotPaidPeriod } = currentTariffStatusStore;
|
2021-03-18 18:54:03 +00:00
|
|
|
const { user } = userStore;
|
2022-07-28 13:11:55 +00:00
|
|
|
|
2022-03-24 17:56:51 +00:00
|
|
|
const {
|
|
|
|
setModuleInfo,
|
|
|
|
wizardCompleted,
|
|
|
|
personal,
|
|
|
|
tenantStatus,
|
|
|
|
} = settingsStore;
|
2021-03-18 18:54:03 +00:00
|
|
|
|
|
|
|
return {
|
2022-09-14 06:55:22 +00:00
|
|
|
isNotPaidPeriod,
|
2021-03-18 18:54:03 +00:00
|
|
|
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));
|