@@ -26,7 +26,28 @@ if (window.NC_DBG) console.log(`inc ${module.id}`);
26
26
It then uses Unisys.SetAppState to set "SESSION" to the decoded value.
27
27
if a groupId is detected, then it forces a redirect.
28
28
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.
30
51
31
52
\*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ * //////////////////////////////////////*/
32
53
@@ -87,17 +108,16 @@ class SessionShell extends UNISYS.Component {
87
108
this . renderLoggedIn = this . renderLoggedIn . bind ( this ) ;
88
109
this . handleChange = this . handleChange . bind ( this ) ;
89
110
this . onSubmit = this . onSubmit . bind ( this ) ;
90
- this . state = {
111
+ this . state = { // `state` tracks the login input field
91
112
token : null ,
92
113
classId : null ,
93
114
projId : null ,
94
115
hashedId : null ,
95
116
subId : null ,
96
117
groupId : null ,
97
- subId : null ,
98
118
isValid : false
99
119
} ;
100
-
120
+ this . previousIsValid = false ; // to track changes in loggedIn status
101
121
}
102
122
103
123
/// ROUTE RENDER FUNCTIONS ////////////////////////////////////////////////////
@@ -211,13 +231,20 @@ class SessionShell extends UNISYS.Component {
211
231
// to a valid token string AFTER the changeHandler() detected a valid
212
232
// login after a ForceReload. This is a bit hacky and the app would benefit
213
233
// 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.
214
239
let token = this . props . match . params . token ;
215
240
let decoded = SESSION . DecodeToken ( token , window . NC_CONFIG . dataset ) || { } ;
216
241
this . SetAppState ( "SESSION" , decoded ) ;
242
+ this . previousIsValid = decoded . isValid ;
217
243
}
218
244
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
219
245
/*/ Main Render Function
220
- /*/ render ( ) {
246
+ /*/
247
+ render ( ) {
221
248
// FUN FACTS
222
249
// this.state set in constructor
223
250
// this.props.history, location, match added by withRouter(AppShell)
@@ -237,9 +264,12 @@ class SessionShell extends UNISYS.Component {
237
264
</ FormGroup >
238
265
) ;
239
266
}
240
- // no token so just render login
267
+
241
268
let token = this . props . match . params . token ;
269
+
270
+ // no token so just render login
242
271
if ( ! token ) return this . renderLogin ( ) ;
272
+
243
273
// try to decode token
244
274
let decoded = SESSION . DecodeToken ( token , window . NC_CONFIG . dataset ) ;
245
275
if ( decoded . isValid ) {
@@ -248,6 +278,22 @@ class SessionShell extends UNISYS.Component {
248
278
return this . renderLogin ( token ) ;
249
279
}
250
280
}
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
+ }
251
297
252
298
/// EVENT HANDLERS ////////////////////////////////////////////////////////////
253
299
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
0 commit comments