1- import { useEffect , useMemo , useState } from 'react' ;
1+ import { useEffect , useState } from 'react' ;
22import { getSdkConfig } from '@pega/auth/lib/sdk-auth-manager' ;
33import { makeStyles } from '@mui/styles' ;
44
5- import StoreContext from '@pega/react-sdk-components/lib/bridge/Context/StoreContext ' ;
6- import createPConnectComponent from '@pega/react-sdk-components/lib/bridge/react_pconnect ' ;
5+ import { usePegaAuth } from '../context/PegaAuthProvider ' ;
6+ import { usePega } from '../context/PegaReadyContext ' ;
77
88import ShoppingOptionCard from '../ShoppingOptionCard' ;
99import ResolutionScreen from '../ResolutionScreen' ;
@@ -105,45 +105,35 @@ const useStyles = makeStyles(theme => ({
105105 pegaForm : { }
106106} ) ) ;
107107
108- function RootComponent ( props ) {
109- const PegaConnectObj = createPConnectComponent ( ) ;
110- const thePConnObj = < PegaConnectObj { ... props } /> ;
108+ export default function MainScreen ( ) {
109+ const { isAuthenticated } = usePegaAuth ( ) ;
110+ const { isPegaReady , PegaContainer , createCase } = usePega ( ) ;
111111
112- /**
113- * NOTE: For Embedded mode, we add in displayOnlyFA to our React context
114- * so it is available to any component that may need it.
115- * VRS: Attempted to remove displayOnlyFA but it presently handles various components which
116- * SDK does not yet support, so all those need to be fixed up before it can be removed.
117- * To be done in a future sprint.
118- */
119- const contextValue = useMemo ( ( ) => {
120- return { store : PCore . getStore ( ) , displayOnlyFA : true } ;
121- } , [ PCore . getStore ( ) ] ) ;
122-
123- return < StoreContext . Provider value = { contextValue } > { thePConnObj } </ StoreContext . Provider > ;
124- }
125-
126- export default function MainScreen ( props ) {
127112 const classes = useStyles ( ) ;
113+
128114 const [ showPega , setShowPega ] = useState ( false ) ;
129115 const [ showLandingPage , setShowLandingPage ] = useState ( true ) ;
130116 const [ showResolution , setShowResolution ] = useState ( false ) ;
131117
132118 useEffect ( ( ) => {
133- // Subscribe to the EVENT_CANCEL event to handle the assignment cancellation
134- PCore . getPubSubUtils ( ) . subscribe ( PCore . getConstants ( ) . PUB_SUB_EVENTS . EVENT_CANCEL , ( ) => cancelAssignment ( ) , 'cancelAssignment' ) ;
135- // Subscribe to the END_OF_ASSIGNMENT_PROCESSING event to handle assignment completion
136- PCore . getPubSubUtils ( ) . subscribe (
137- PCore . getConstants ( ) . PUB_SUB_EVENTS . CASE_EVENTS . END_OF_ASSIGNMENT_PROCESSING ,
138- ( ) => assignmentFinished ( ) ,
139- 'endOfAssignmentProcessing'
140- ) ;
119+ if ( isPegaReady ) {
120+ // Subscribe to the EVENT_CANCEL event to handle the assignment cancellation
121+ PCore . getPubSubUtils ( ) . subscribe ( PCore . getConstants ( ) . PUB_SUB_EVENTS . EVENT_CANCEL , ( ) => cancelAssignment ( ) , 'cancelAssignment' ) ;
122+
123+ // Subscribe to the END_OF_ASSIGNMENT_PROCESSING event to handle assignment completion
124+ PCore . getPubSubUtils ( ) . subscribe (
125+ PCore . getConstants ( ) . PUB_SUB_EVENTS . CASE_EVENTS . END_OF_ASSIGNMENT_PROCESSING ,
126+ ( ) => assignmentFinished ( ) ,
127+ 'endOfAssignmentProcessing'
128+ ) ;
129+ }
130+
141131 return ( ) => {
142132 // unsubscribe to the events
143133 PCore . getPubSubUtils ( ) . unsubscribe ( PCore . getConstants ( ) . PUB_SUB_EVENTS . EVENT_CANCEL , 'cancelAssignment' ) ;
144134 PCore . getPubSubUtils ( ) . unsubscribe ( PCore . getConstants ( ) . PUB_SUB_EVENTS . CASE_EVENTS . END_OF_ASSIGNMENT_PROCESSING , 'endOfAssignmentProcessing' ) ;
145135 } ;
146- } , [ ] ) ;
136+ } , [ isPegaReady ] ) ;
147137
148138 const cancelAssignment = ( ) => {
149139 setShowLandingPage ( true ) ;
@@ -159,14 +149,8 @@ export default function MainScreen(props) {
159149 setShowLandingPage ( false ) ;
160150 setShowPega ( true ) ;
161151 const sdkConfig = await getSdkConfig ( ) ;
162- let mashupCaseType = sdkConfig . serverConfig . appMashupCaseType ;
163- // If mashupCaseType is null or undefined, get the first case type from the environment info
164- if ( ! mashupCaseType ) {
165- const caseTypes = PCore . getEnvironmentInfo ( ) ?. environmentInfoObject ?. pyCaseTypeList ;
166- if ( caseTypes && caseTypes . length > 0 ) {
167- mashupCaseType = caseTypes [ 0 ] . pyWorkTypeImplementationClassName ;
168- }
169- }
152+ const mashupCaseType = sdkConfig . serverConfig . appMashupCaseType ;
153+
170154 let selectedPhoneGUID = '' ;
171155 const phoneName = optionClicked ? optionClicked . trim ( ) : '' ;
172156 switch ( phoneName ) {
@@ -198,12 +182,10 @@ export default function MainScreen(props) {
198182 console . warn ( `Unexpected case type: ${ mashupCaseType } . PhoneModelss field not set.` ) ;
199183 }
200184
201- // Create a new case using the mashup API
202- PCore . getMashupApi ( )
203- . createCase ( mashupCaseType , PCore . getConstants ( ) . APP . APP , options )
204- . then ( ( ) => {
205- console . log ( 'createCase rendering is complete' ) ;
206- } ) ;
185+ // Call the createCase function from context to create a new case using the mashup API
186+ createCase ( mashupCaseType , options ) . then ( ( ) => {
187+ console . log ( 'createCase rendering is complete' ) ;
188+ } ) ;
207189 } ;
208190
209191 function renderLandingPage ( ) {
@@ -244,13 +226,15 @@ export default function MainScreen(props) {
244226 return (
245227 < div className = { classes . pegaViewContainer } >
246228 < div className = { classes . pegaForm } id = 'pega-part-of-page' >
247- < RootComponent { ... props } />
229+ < PegaContainer />
248230 < br />
249231 </ div >
250232 </ div >
251233 ) ;
252234 }
253235
236+ if ( ! isAuthenticated ) return < div style = { { textAlign : 'center' } } > Loading...</ div > ;
237+
254238 return (
255239 < div className = { classes . appContainer } >
256240 { showLandingPage && renderLandingPage ( ) }
0 commit comments