1+ import _ from 'lodash' ;
12import React , { Component } from 'react' ;
23import PropTypes from 'prop-types' ;
34import { connect } from 'react-redux' ;
@@ -11,6 +12,7 @@ import Nav from 'react-bootstrap/lib/Nav';
1112import NavItem from 'react-bootstrap/lib/NavItem' ;
1213import Alert from 'react-bootstrap/lib/Alert' ;
1314import Helmet from 'react-helmet' ;
15+ import qs from 'qs' ;
1416import { isLoaded as isInfoLoaded , load as loadInfo } from 'redux/modules/info' ;
1517import { isLoaded as isAuthLoaded , load as loadAuth , logout } from 'redux/modules/auth' ;
1618import { Notifs , InfoBar } from 'components' ;
@@ -56,39 +58,30 @@ export default class App extends Component {
5658 store : PropTypes . object . isRequired
5759 } ;
5860
59- static getDerivedStateFromProps ( nextProps , prevState ) {
60- if ( ! prevState . user && nextProps . user ) {
61- const query = new URLSearchParams ( nextProps . location . search ) ;
62- nextProps . pushState ( query . get ( 'redirect' ) || '/login-success' ) ;
63- return {
64- user : nextProps . user
65- } ;
66- } else if ( prevState . user && ! nextProps . user ) {
67- nextProps . pushState ( '/' ) ;
68- return {
69- user : null
70- } ;
61+ static getDerivedStateFromProps ( props , state ) {
62+ const { prevProps } = state ;
63+ // Compare the incoming prop to previous prop
64+ const user = ! _ . isEqual ( prevProps . user , props . user ) ? props . user : state . user ;
65+
66+ if ( ! prevProps . user && props . user ) {
67+ const query = qs . parse ( props . location . search , { ignoreQueryPrefix : true } ) ;
68+ props . pushState ( query . redirect || '/login-success' ) ;
69+ } else if ( prevProps . user && ! props . user ) {
70+ // logout
71+ props . pushState ( '/' ) ;
7172 }
72- return null ;
73+
74+ return {
75+ // Store the previous props in state
76+ prevProps : props ,
77+ user
78+ } ;
7379 }
7480
7581 state = {
76- user : null
82+ prevProps : this . props , // eslint-disable-line react/no-unused-state
83+ user : this . props . user
7784 } ;
78- /* Made Unsafe as per React Docs v16.3 */
79- // componentWillReceiveProps(nextProps) {
80- // console.log(this.props.user);
81- // console.log(nextProps.user);
82- // if (!this.props.user && nextProps.user) {
83- // // login
84- // const query = new URLSearchParams(this.props.location.search);
85- // this.props.pushState(query.get('redirect') || '/login-success');
86- // } else if (this.props.user && !nextProps.user) {
87- // // logout
88- // this.props.pushState('/');
89- // }
90- // }
91- /* Unsafe as per v16.3 */
9285
9386 componentDidUpdate ( prevProps ) {
9487 if ( this . props . location !== prevProps . location ) {
0 commit comments