/* eslint-disable react/prop-types */ import React, { useEffect } from "react"; import { Redirect, Route } from "react-router-dom"; //import Loader from "@appserver/components/loader"; //import PageLayout from "../PageLayout"; // import Error401 from "studio/Error401"; // import Error404 from "studio/Error404"; import AppLoader from "../AppLoader"; import { inject, observer } from "mobx-react"; import { isMe } from "../../utils"; import combineUrl from "../../utils/combineUrl"; import { AppServerConfig } from "../../constants"; const PrivateRoute = ({ component: Component, ...rest }) => { const { isAdmin, isAuthenticated, isLoaded, restricted, allowForMe, user, computedMatch, setModuleInfo, modules, currentProductId, wizardCompleted, personal, location, } = rest; const { params, path } = computedMatch; const { userId } = params; const renderComponent = (props) => { if (isLoaded && !isAuthenticated) { if (personal && process.env.NODE_ENV === "production") { window.location.replace("/"); return <>; } console.log("PrivateRoute render Redirect to login", rest); return ( ); } if (location.pathname === "/" && personal) { return ( ); } if (!isLoaded) { return ; } // const userLoaded = !isEmpty(user); // if (!userLoaded) { // return ; // } // if (!userLoaded) { // console.log("PrivateRoute render Loader", rest); // return ( // // // // // // ); // } if ( !restricted || isAdmin || (allowForMe && userId && isMe(user, userId)) ) { // console.log( // "PrivateRoute render Component", // rest, // Component.name || Component.displayName // ); return ; } if (restricted) { console.log("PrivateRoute render Error401", rest); return ( ); } console.log("PrivateRoute render Error404", rest); return ( ); }; useEffect(() => { 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)); } if (!currentModule) return; const { id, origLink, link } = currentModule; setModuleInfo(origLink || link, id); }, [path, modules, isLoaded]); //console.log("PrivateRoute render", rest); return ; }; export default inject(({ auth }) => { const { userStore, isAuthenticated, isLoaded, isAdmin, settingsStore, moduleStore, } = auth; const { user } = userStore; const { modules } = moduleStore; const { setModuleInfo, wizardCompleted, personal } = settingsStore; return { modules, user, isAuthenticated, isAdmin, isLoaded, setModuleInfo, wizardCompleted, personal, }; })(observer(PrivateRoute));