DocSpace-buildtools/packages/common/components/PrivateRoute/index.js

235 lines
5.4 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 "@docspace/components/loader";
//import Section from "../Section";
// 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-03-24 17:56:51 +00:00
import { AppServerConfig, TenantStatus } from "../../constants";
2022-09-14 06:55:22 +00:00
import CurrentTariffStatus from "../../store/CurrentTariffStatusStore";
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,
2022-03-24 17:56:51 +00:00
tenantStatus,
2022-09-14 06:55:22 +00:00
isNotPaidPeriod,
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) => {
const isPortalUrl = props.location.pathname === "/preparation-portal";
2021-03-18 18:54:03 +00:00
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);
const redirectPath = wizardCompleted ? "/login" : "/wizard";
return window.location.replace(redirectPath);
// return (
// <Redirect
// to={{
// pathname: combineUrl(
// AppServerConfig.proxyURL,
// wizardCompleted ? "/login" : "/wizard"
// ),
// state: { from: props.location },
// }}
// />
// );
2021-03-18 18:54:03 +00:00
}
if (
isLoaded &&
!isNotPaidPeriod &&
props.location.pathname === "/portal-unavailable"
) {
return window.location.replace("/");
}
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 &&
!isPortalUrl
2022-03-24 17:56:51 +00:00
) {
return (
<Redirect
to={{
pathname: combineUrl(
AppServerConfig.proxyURL,
"/preparation-portal"
),
state: { from: props.location },
}}
/>
);
}
if (
2022-09-14 06:55:22 +00:00
isNotPaidPeriod &&
isLoaded &&
(user.isOwner || user.isAdmin) &&
props.location.pathname !== "/portal-settings/payments/portal-payments" &&
props.location.pathname !== "/portal-settings/backup/data-backup"
) {
return (
<Redirect
to={{
pathname: combineUrl(
AppServerConfig.proxyURL,
"/portal-settings/payments/portal-payments"
),
state: { from: props.location },
}}
/>
);
}
if (tenantStatus !== TenantStatus.PortalRestore && isPortalUrl) {
return window.location.replace("/");
}
if (
2022-09-14 06:55:22 +00:00
isNotPaidPeriod &&
isLoaded &&
!user.isOwner &&
!user.isAdmin &&
props.location.pathname !== "/portal-unavailable"
) {
return (
<Redirect
to={{
pathname: combineUrl(
AppServerConfig.proxyURL,
"/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 (
// <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 },
}}
/>
);
};
//console.log("PrivateRoute render", rest);
return <Route {...rest} render={renderComponent} />;
};
export default inject(({ auth }) => {
const {
userStore,
isAuthenticated,
isLoaded,
isAdmin,
settingsStore,
2022-09-14 06:55:22 +00:00
currentTariffStatusStore,
} = auth;
2022-09-14 06:55:22 +00:00
const { isNotPaidPeriod } = currentTariffStatusStore;
2021-03-18 18:54:03 +00:00
const { user } = userStore;
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,
personal,
2021-03-18 18:54:03 +00:00
};
})(observer(PrivateRoute));