11import PropTypes from 'prop-types' ;
22import React from 'react' ;
3- import bindAll from 'lodash.bindall' ;
43import { connect } from 'react-redux' ;
54import { defineMessages , injectIntl , intlShape } from 'react-intl' ;
65
@@ -21,23 +20,18 @@ const messages = defineMessages({
2120 */
2221const TitledHOC = function ( WrappedComponent ) {
2322 class TitledComponent extends React . Component {
24- constructor ( props ) {
25- super ( props ) ;
26- bindAll ( this , [
27- 'handleUpdateProjectTitle'
28- ] ) ;
29- }
3023 componentDidMount ( ) {
31- this . setReduxTitle ( this . props . projectTitle ) ;
24+ this . props . updateReduxProjectTitle ( this . titleWithDefault ( this . props . projectTitle ) ) ;
3225 }
3326 componentDidUpdate ( prevProps ) {
3427 if ( this . props . projectTitle !== prevProps . projectTitle ) {
35- this . setReduxTitle ( this . props . projectTitle ) ;
28+ this . props . updateReduxProjectTitle ( this . titleWithDefault ( this . props . projectTitle ) ) ;
3629 }
37- if ( this . props . isShowingWithoutId && ! prevProps . isShowingWithoutId ) {
38- const defaultProjectTitle = this . titleWithDefault ( ) ;
39- this . setReduxTitle ( defaultProjectTitle ) ;
40- this . props . onUpdateProjectTitle ( defaultProjectTitle ) ;
30+ // if the projectTitle hasn't changed, but the reduxProjectTitle
31+ // HAS changed, we need to report that change to the projectTitle's owner
32+ if ( this . props . reduxProjectTitle !== prevProps . reduxProjectTitle &&
33+ this . props . reduxProjectTitle !== this . props . projectTitle ) {
34+ this . props . onUpdateProjectTitle ( this . props . reduxProjectTitle ) ;
4135 }
4236 }
4337 titleWithDefault ( title ) {
@@ -46,36 +40,23 @@ const TitledHOC = function (WrappedComponent) {
4640 }
4741 return title ;
4842 }
49- setReduxTitle ( newTitle ) {
50- if ( newTitle === null || typeof newTitle === 'undefined' ) {
51- this . props . onUpdateReduxProjectTitle (
52- this . props . intl . formatMessage ( messages . defaultProjectTitle )
53- ) ;
54- } else {
55- this . props . onUpdateReduxProjectTitle ( newTitle ) ;
56- }
57- }
58- handleUpdateProjectTitle ( newTitle ) {
59- this . setReduxTitle ( newTitle ) ;
60- this . props . onUpdateProjectTitle ( newTitle ) ;
61- }
6243 render ( ) {
6344 const {
6445 /* eslint-disable no-unused-vars */
6546 intl,
6647 isShowingWithoutId,
6748 // for children, we replace onUpdateProjectTitle with our own
6849 onUpdateProjectTitle,
69- onUpdateReduxProjectTitle,
7050 // we don't pass projectTitle prop to children -- they must use
7151 // redux value
7252 projectTitle,
53+ reduxProjectTitle,
54+ updateReduxProjectTitle,
7355 /* eslint-enable no-unused-vars */
7456 ...componentProps
7557 } = this . props ;
7658 return (
7759 < WrappedComponent
78- onUpdateProjectTitle = { this . handleUpdateProjectTitle }
7960 { ...componentProps }
8061 />
8162 ) ;
@@ -86,8 +67,9 @@ const TitledHOC = function (WrappedComponent) {
8667 intl : intlShape ,
8768 isShowingWithoutId : PropTypes . bool ,
8869 onUpdateProjectTitle : PropTypes . func ,
89- onUpdateReduxProjectTitle : PropTypes . func ,
90- projectTitle : PropTypes . string
70+ projectTitle : PropTypes . string ,
71+ reduxProjectTitle : PropTypes . string ,
72+ updateReduxProjectTitle : PropTypes . func
9173 } ;
9274
9375 TitledComponent . defaultProps = {
@@ -97,12 +79,13 @@ const TitledHOC = function (WrappedComponent) {
9779 const mapStateToProps = state => {
9880 const loadingState = state . scratchGui . projectState . loadingState ;
9981 return {
100- isShowingWithoutId : getIsShowingWithoutId ( loadingState )
82+ isShowingWithoutId : getIsShowingWithoutId ( loadingState ) ,
83+ reduxProjectTitle : state . scratchGui . projectTitle
10184 } ;
10285 } ;
10386
10487 const mapDispatchToProps = dispatch => ( {
105- onUpdateReduxProjectTitle : title => dispatch ( setProjectTitle ( title ) )
88+ updateReduxProjectTitle : title => dispatch ( setProjectTitle ( title ) )
10689 } ) ;
10790
10891 return injectIntl ( connect (
0 commit comments