From b1fb5e321ec63a652a4df089b730334b14b72127 Mon Sep 17 00:00:00 2001 From: tumms2021389 <97188066+tumms2021389@users.noreply.github.com> Date: Wed, 20 Nov 2024 19:19:46 +0530 Subject: [PATCH] refactor: full portal code refactor (#538) --- src/samples/FullPortal/index.tsx | 127 ++++++++++++------------------- 1 file changed, 48 insertions(+), 79 deletions(-) diff --git a/src/samples/FullPortal/index.tsx b/src/samples/FullPortal/index.tsx index 302fd85e..03d00e82 100644 --- a/src/samples/FullPortal/index.tsx +++ b/src/samples/FullPortal/index.tsx @@ -1,5 +1,6 @@ +/* eslint-disable @typescript-eslint/no-use-before-define */ +/* eslint-disable no-console */ import { useEffect, useMemo, useState } from 'react'; -import ReactDOM from 'react-dom'; import { useLocation, useNavigate } from 'react-router-dom'; import CssBaseline from '@mui/material/CssBaseline'; import { ThemeProvider, StyledEngineProvider } from '@mui/material/styles'; @@ -23,10 +24,22 @@ function useQuery() { return useMemo(() => new URLSearchParams(search), [search]); } +function RootComponent(props) { + const PegaConnectObj = createPConnectComponent(); + const thePConnObj = ; + + const contextValue = useMemo(() => { + return { store: PCore.getStore() }; + }, []); + + return {thePConnObj}; +} + export default function FullPortal() { const [portalSelectionScreen, setPortalSelectionScreen] = useState(false); const [defaultPortalName, setDefaultPortalName] = useState(''); const [availablePortals, setAvailablePortals] = useState([]); + const [rootComponentProps, setRootComponentProps] = useState(null); const navigate = useNavigate(); const query = useQuery(); @@ -39,71 +52,16 @@ export default function FullPortal() { sessionStorage.setItem('rsdk_locale', localeOverride); } - // const outlet = document.getElementById("outlet"); - - // from react_root.js with some modifications - // eslint-disable-next-line react/no-unstable-nested-components - function RootComponent(props) { - // const { portalTarget, styleSheetTarget } = props; - const PegaConnectObj = createPConnectComponent(); - - // remove from Provider to work around compiler error for now: context={StoreContext} - // return ( - // - // - // - // ); - - // const thePConnObj =
the RootComponent
; - const thePConnObj = ; - - return ( - // eslint-disable-next-line react/jsx-no-constructed-context-values - {thePConnObj} - ); - } - /** * Callback from onPCoreReady that's called once the top-level render object * is ready to be rendered * @param inRenderObj the initial, top-level PConnect object to render */ function initialRender(inRenderObj) { - // modified from react_root.js render - const { props, domContainerID = null, componentName, portalTarget, styleSheetTarget } = inRenderObj; - let target: any = null; - if (domContainerID !== null) { - target = document.getElementById(domContainerID); - } else if (portalTarget !== null) { - target = portalTarget; - } - const Component: any = RootComponent; - if (componentName) { - Component.displayName = componentName; - } - - // 1st arg was: - // , + const { props, portalTarget, styleSheetTarget } = inRenderObj; - // var theComponent =
the Component
; - const theComponent = ( - - - - ; - - - ); - - ReactDOM.render( - // was { - // eslint-disable-next-line no-console console.log(`SdkComponentMap initialized`); // Don't call initialRender until SdkComponentMap is fully initialized @@ -137,15 +94,12 @@ export default function FullPortal() { if (queryPortal) { myLoadPortal('pega-root', queryPortal, []); } else if (thePortal) { - // eslint-disable-next-line no-console console.log(`Loading specified appPortal: ${thePortal}`); myLoadPortal('pega-root', thePortal, []); } else if (myLoadDefaultPortal && defaultPortal && !excludePortals.includes(defaultPortal)) { - // eslint-disable-next-line no-console console.log(`Loading default portal`); myLoadDefaultPortal('pega-root', []); } else { - // eslint-disable-next-line no-console console.log('Loading portal selection screen'); setPortalSelectionScreen(true); setDefaultPortalName(defaultPortal); @@ -162,39 +116,54 @@ export default function FullPortal() { } function doRedirectDone() { - navigate(window.location.pathname); - let localeOverride: any = sessionStorage.getItem('rsdk_locale'); - if (!localeOverride) { - localeOverride = undefined; - } + const redirectUrl: any = sessionStorage.getItem('url'); + navigate(redirectUrl); + sessionStorage.removeItem('url'); + + const locale: any = sessionStorage.getItem('rsdk_locale') || undefined; // appName and mainRedirect params have to be same as earlier invocation - loginIfNecessary({ appName: 'portal', mainRedirect: true, locale: localeOverride }); + loginIfNecessary({ appName: 'portal', mainRedirect: true, locale }); } // One time (initialization) useEffect(() => { - document.addEventListener('SdkConstellationReady', () => { - // start the portal - startPortal(); - }); - let localeOverride: any = sessionStorage.getItem('rsdk_locale'); - if (!localeOverride) { - localeOverride = undefined; + document.addEventListener('SdkConstellationReady', handleSdkConstellationReady); + + const locale: any = sessionStorage.getItem('rsdk_locale') || undefined; + + const isLoggedIn = sessionStorage.getItem('isLoggedIn'); + const redirected = sessionStorage.getItem('redirected'); + if (isLoggedIn !== 'true' && redirected !== 'true') { + sessionStorage.setItem('url', window.location.pathname); + navigate('/portal'); } + sessionStorage.setItem('redirected', 'true'); // Login if needed, doing an initial main window redirect loginIfNecessary({ appName: 'portal', mainRedirect: true, redirectDoneCB: doRedirectDone, - locale: localeOverride + locale + // semanticUrls: true //. enable this line for semantic urls }); }, []); + const handleSdkConstellationReady = () => { + sessionStorage.setItem('isLoggedIn', 'true'); + // start the portal + startPortal(); + }; + return portalSelectionScreen ? ( ) : ( -
-
+
+ + + + {rootComponentProps && } + +
); }