-
Notifications
You must be signed in to change notification settings - Fork 4k
Refactor title handling #5168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
benjiwheeler
merged 8 commits into
scratchfoundation:develop
from
benjiwheeler:clear-title-on-default-project
Oct 21, 2019
Merged
Refactor title handling #5168
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
91e2fc9
on showing project without id, tell owner of GUI to set title to default
benjiwheeler b63ac51
move project title management into titled hoc
benjiwheeler 9c33bbb
remove TitledHOC from playground, and explicitly pass canEditTitle
benjiwheeler 22e2c17
don’t pass onUpdateProjectTitle below titledHIC
benjiwheeler 68780ae
added back missing canEditTitle
benjiwheeler b6ff1f4
better names for prop functions
benjiwheeler f4a9bfa
in titledhoc, consolidate function calls
benjiwheeler e3fa196
revised name of updateReduxProjectTitle, updateReduxProjectTitleWithD…
benjiwheeler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,98 @@ | ||
| import PropTypes from 'prop-types'; | ||
| import React from 'react'; | ||
| import bindAll from 'lodash.bindall'; | ||
| import {connect} from 'react-redux'; | ||
| import {defineMessages, injectIntl, intlShape} from 'react-intl'; | ||
|
|
||
| import {getIsShowingWithoutId} from '../reducers/project-state'; | ||
| import {setProjectTitle} from '../reducers/project-title'; | ||
|
|
||
| const messages = defineMessages({ | ||
| defaultProjectTitle: { | ||
| id: 'gui.gui.defaultProjectTitle', | ||
| description: 'Default title for project', | ||
| defaultMessage: 'Scratch Project' | ||
| } | ||
| }); | ||
|
|
||
| /* Higher Order Component to get and set the project title | ||
| * @param {React.Component} WrappedComponent component to receive project title related props | ||
| * @returns {React.Component} component with project loading behavior | ||
| */ | ||
| const TitledHOC = function (WrappedComponent) { | ||
| class TitledComponent extends React.Component { | ||
| constructor (props) { | ||
| super(props); | ||
| bindAll(this, [ | ||
| 'handleUpdateProjectTitle' | ||
| ]); | ||
| this.state = { | ||
| projectTitle: null | ||
| }; | ||
| componentDidMount () { | ||
| this.handleReceivedProjectTitle(this.props.projectTitle); | ||
| } | ||
| componentDidUpdate (prevProps) { | ||
| if (this.props.projectTitle !== prevProps.projectTitle) { | ||
| this.handleReceivedProjectTitle(this.props.projectTitle); | ||
| } | ||
| // if the projectTitle hasn't changed, but the reduxProjectTitle | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note this addition... here is where the change to redux title gets echoed up to the enclosing context (i.e., www) |
||
| // HAS changed, we need to report that change to the projectTitle's owner | ||
| if (this.props.reduxProjectTitle !== prevProps.reduxProjectTitle && | ||
| this.props.reduxProjectTitle !== this.props.projectTitle) { | ||
| this.props.onUpdateProjectTitle(this.props.reduxProjectTitle); | ||
| } | ||
| } | ||
| handleUpdateProjectTitle (newTitle) { | ||
| this.setState({projectTitle: newTitle}); | ||
| handleReceivedProjectTitle (requestedTitle) { | ||
| let newTitle = requestedTitle; | ||
| if (newTitle === null || typeof newTitle === 'undefined') { | ||
| newTitle = this.props.intl.formatMessage(messages.defaultProjectTitle); | ||
| } | ||
| this.props.onChangedProjectTitle(newTitle); | ||
| } | ||
| render () { | ||
| const { | ||
| /* eslint-disable no-unused-vars */ | ||
| intl, | ||
| isShowingWithoutId, | ||
| onChangedProjectTitle, | ||
| // for children, we replace onUpdateProjectTitle with our own | ||
| onUpdateProjectTitle, | ||
| // we don't pass projectTitle prop to children -- they must use | ||
| // redux value | ||
| projectTitle, | ||
| reduxProjectTitle, | ||
| /* eslint-enable no-unused-vars */ | ||
| ...componentProps | ||
| } = this.props; | ||
| return ( | ||
| <WrappedComponent | ||
| canEditTitle | ||
| projectTitle={this.state.projectTitle} | ||
| onUpdateProjectTitle={this.handleUpdateProjectTitle} | ||
| {...this.props} | ||
| {...componentProps} | ||
| /> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return TitledComponent; | ||
| TitledComponent.propTypes = { | ||
| intl: intlShape, | ||
| isShowingWithoutId: PropTypes.bool, | ||
| onChangedProjectTitle: PropTypes.func, | ||
| onUpdateProjectTitle: PropTypes.func, | ||
| projectTitle: PropTypes.string, | ||
| reduxProjectTitle: PropTypes.string | ||
| }; | ||
|
|
||
| TitledComponent.defaultProps = { | ||
| onUpdateProjectTitle: () => {} | ||
| }; | ||
|
|
||
| const mapStateToProps = state => { | ||
| const loadingState = state.scratchGui.projectState.loadingState; | ||
| return { | ||
| isShowingWithoutId: getIsShowingWithoutId(loadingState), | ||
| reduxProjectTitle: state.scratchGui.projectTitle | ||
| }; | ||
| }; | ||
|
|
||
| const mapDispatchToProps = dispatch => ({ | ||
| onChangedProjectTitle: title => dispatch(setProjectTitle(title)) | ||
| }); | ||
|
|
||
| return injectIntl(connect( | ||
| mapStateToProps, | ||
| mapDispatchToProps, | ||
| )(TitledComponent)); | ||
| }; | ||
|
|
||
| export { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should change this back to how it was before
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine to do since your handler is coming from Redux now. I just think the name of the prop should change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing this...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done