@@ -26,7 +26,28 @@ if (window.NC_DBG) console.log(`inc ${module.id}`);
2626 It then uses Unisys.SetAppState to set "SESSION" to the decoded value.
2727 if a groupId is detected, then it forces a redirect.
2828
29- TODO: if an invalid URL is entered, should reset
29+
30+ A change in logged in status can come from four places:
31+
32+ 1. User has typed in login field.
33+ => This is handled by `handleChange()`
34+ 2. User has hit the "Login" submit button.
35+ => This is handled by `onSubmit()`
36+ 3. User has entered a full URL for the first time
37+ => This is handled by `componentWillMount()`
38+ 4. User has changed an existing URL
39+ => This does not trigger `compomentWillMount()`
40+ so it needs special handling.
41+ Changing the url does trigger:
42+ * render()
43+ render is triggered because the props for the token
44+ passed by the Route change.
45+ And render does detect the correct login state.
46+ However, if this represents a change in login state,
47+ the change in state needs to be broadcast.
48+ * componentDidUpdate()
49+ componentDidUpdate checks for the change in loggedinIn
50+ status and sends an AppStateChanged event as needed.
3051
3152\*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ * //////////////////////////////////////*/
3253
@@ -87,17 +108,16 @@ class SessionShell extends UNISYS.Component {
87108 this . renderLoggedIn = this . renderLoggedIn . bind ( this ) ;
88109 this . handleChange = this . handleChange . bind ( this ) ;
89110 this . onSubmit = this . onSubmit . bind ( this ) ;
90- this . state = {
111+ this . state = { // `state` tracks the login input field
91112 token : null ,
92113 classId : null ,
93114 projId : null ,
94115 hashedId : null ,
95116 subId : null ,
96117 groupId : null ,
97- subId : null ,
98118 isValid : false
99119 } ;
100-
120+ this . previousIsValid = false ; // to track changes in loggedIn status
101121 }
102122
103123 /// ROUTE RENDER FUNCTIONS ////////////////////////////////////////////////////
@@ -211,13 +231,20 @@ class SessionShell extends UNISYS.Component {
211231 // to a valid token string AFTER the changeHandler() detected a valid
212232 // login after a ForceReload. This is a bit hacky and the app would benefit
213233 // from not relying on forced reloads. See handleChange().
234+ //
235+ // This is only called with the initial page load.
236+ // Subsequent changes to the URL (e.g. changing token directly in the url)
237+ // do not result in a second componentWillMount call. These are handled
238+ // by the componentDidUpdate() call.
214239 let token = this . props . match . params . token ;
215240 let decoded = SESSION . DecodeToken ( token , window . NC_CONFIG . dataset ) || { } ;
216241 this . SetAppState ( "SESSION" , decoded ) ;
242+ this . previousIsValid = decoded . isValid ;
217243 }
218244 /// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
219245 /*/ Main Render Function
220- /*/ render ( ) {
246+ /*/
247+ render ( ) {
221248 // FUN FACTS
222249 // this.state set in constructor
223250 // this.props.history, location, match added by withRouter(AppShell)
@@ -237,9 +264,12 @@ class SessionShell extends UNISYS.Component {
237264 </ FormGroup >
238265 ) ;
239266 }
240- // no token so just render login
267+
241268 let token = this . props . match . params . token ;
269+
270+ // no token so just render login
242271 if ( ! token ) return this . renderLogin ( ) ;
272+
243273 // try to decode token
244274 let decoded = SESSION . DecodeToken ( token , window . NC_CONFIG . dataset ) ;
245275 if ( decoded . isValid ) {
@@ -248,6 +278,22 @@ class SessionShell extends UNISYS.Component {
248278 return this . renderLogin ( token ) ;
249279 }
250280 }
281+ /// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
282+ componentDidUpdate ( prevProps ) {
283+ // SIDE EFFECT
284+ // Check for changes in logged in status and
285+ // trigger AppStateChange if necessary
286+
287+ // Read token from props (because they are not saved in state)
288+ // We have to decode again here because we're using props directly
289+ let token = this . props . match . params . token ;
290+ if ( ! token ) return ; // don't bother to check if this was a result of changes from the form
291+ let decoded = SESSION . DecodeToken ( token , window . NC_CONFIG . dataset ) ;
292+ if ( decoded . isValid !== this . previousIsValid ) {
293+ this . SetAppState ( "SESSION" , decoded ) ;
294+ this . previousIsValid = decoded . isValid ;
295+ }
296+ }
251297
252298 /// EVENT HANDLERS ////////////////////////////////////////////////////////////
253299 /// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
0 commit comments