Skip to content
Snippets Groups Projects
Unverified Commit cf9e165c authored by Benjamin Wheeler's avatar Benjamin Wheeler Committed by GitHub
Browse files

Merge pull request #4676 from mzgoddard/menu-bar

Move projectChanged into HOC wrapping MenuBar, to avoid unnecessary renders
parents e877486d fc8c2421
No related branches found
No related tags found
No related merge requests found
import {connect} from 'react-redux';
import PropTypes from 'prop-types';
import bindAll from 'lodash.bindall';
import React from 'react';
const ConfirmReplaceHOC = function (WrappedComponent) {
class ConfirmReplaceProject extends React.PureComponent {
constructor (props) {
super(props);
bindAll(this, [
'confirmReadyToReplaceProject'
]);
}
confirmReadyToReplaceProject (message) {
let readyToReplaceProject = true;
if (this.props.projectChanged && !this.props.canCreateNew) {
readyToReplaceProject = confirm(message); // eslint-disable-line no-alert
}
return readyToReplaceProject;
}
render () {
const {
/* eslint-disable no-unused-vars */
projectChanged,
/* eslint-enable no-unused-vars */
...props
} = this.props;
return (<WrappedComponent
confirmReadyToReplaceProject={this.confirmReadyToReplaceProject}
{...props}
/>);
}
}
ConfirmReplaceProject.propTypes = {
canCreateNew: PropTypes.bool,
projectChanged: PropTypes.bool
};
const _mapStateToProps = state => ({
projectChanged: state.scratchGui.projectChanged
});
return connect(_mapStateToProps)(ConfirmReplaceProject);
};
export default ConfirmReplaceHOC;
import classNames from 'classnames'; import classNames from 'classnames';
import {connect} from 'react-redux'; import {connect} from 'react-redux';
import {compose} from 'redux';
import {defineMessages, FormattedMessage, injectIntl, intlShape} from 'react-intl'; import {defineMessages, FormattedMessage, injectIntl, intlShape} from 'react-intl';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import bindAll from 'lodash.bindall'; import bindAll from 'lodash.bindall';
...@@ -27,6 +28,7 @@ import LoginDropdown from './login-dropdown.jsx'; ...@@ -27,6 +28,7 @@ import LoginDropdown from './login-dropdown.jsx';
import SB3Downloader from '../../containers/sb3-downloader.jsx'; import SB3Downloader from '../../containers/sb3-downloader.jsx';
import DeletionRestorer from '../../containers/deletion-restorer.jsx'; import DeletionRestorer from '../../containers/deletion-restorer.jsx';
import TurboMode from '../../containers/turbo-mode.jsx'; import TurboMode from '../../containers/turbo-mode.jsx';
import ConfirmReplaceHOC from './confirm-replace-hoc.jsx';
import {openTipsLibrary} from '../../reducers/modals'; import {openTipsLibrary} from '../../reducers/modals';
import {setPlayer} from '../../reducers/mode'; import {setPlayer} from '../../reducers/mode';
...@@ -164,17 +166,14 @@ class MenuBar extends React.Component { ...@@ -164,17 +166,14 @@ class MenuBar extends React.Component {
document.removeEventListener('keydown', this.handleKeyPress); document.removeEventListener('keydown', this.handleKeyPress);
} }
handleClickNew () { handleClickNew () {
let readyToReplaceProject = true;
// if the project is dirty, and user owns the project, we will autosave. // if the project is dirty, and user owns the project, we will autosave.
// but if they are not logged in and can't save, user should consider // but if they are not logged in and can't save, user should consider
// downloading or logging in first. // downloading or logging in first.
// Note that if user is logged in and editing someone else's project, // Note that if user is logged in and editing someone else's project,
// they'll lose their work. // they'll lose their work.
if (this.props.projectChanged && !this.props.canCreateNew) { const readyToReplaceProject = this.props.confirmReadyToReplaceProject(
readyToReplaceProject = confirm( // eslint-disable-line no-alert this.props.intl.formatMessage(sharedMessages.replaceProjectWarning)
this.props.intl.formatMessage(sharedMessages.replaceProjectWarning) );
);
}
this.props.onRequestCloseFile(); this.props.onRequestCloseFile();
if (readyToReplaceProject) { if (readyToReplaceProject) {
this.props.onClickNew(this.props.canSave && this.props.canCreateNew); this.props.onClickNew(this.props.canSave && this.props.canCreateNew);
...@@ -759,7 +758,6 @@ MenuBar.propTypes = { ...@@ -759,7 +758,6 @@ MenuBar.propTypes = {
onShare: PropTypes.func, onShare: PropTypes.func,
onToggleLoginOpen: PropTypes.func, onToggleLoginOpen: PropTypes.func,
onUpdateProjectTitle: PropTypes.func, onUpdateProjectTitle: PropTypes.func,
projectChanged: PropTypes.bool,
projectTitle: PropTypes.string, projectTitle: PropTypes.string,
renderLogin: PropTypes.func, renderLogin: PropTypes.func,
sessionExists: PropTypes.bool, sessionExists: PropTypes.bool,
...@@ -785,7 +783,6 @@ const mapStateToProps = (state, ownProps) => { ...@@ -785,7 +783,6 @@ const mapStateToProps = (state, ownProps) => {
languageMenuOpen: languageMenuOpen(state), languageMenuOpen: languageMenuOpen(state),
locale: state.locales.locale, locale: state.locales.locale,
loginMenuOpen: loginMenuOpen(state), loginMenuOpen: loginMenuOpen(state),
projectChanged: state.scratchGui.projectChanged,
projectTitle: state.scratchGui.projectTitle, projectTitle: state.scratchGui.projectTitle,
sessionExists: state.session && typeof state.session.session !== 'undefined', sessionExists: state.session && typeof state.session.session !== 'undefined',
username: user ? user.username : null, username: user ? user.username : null,
...@@ -815,7 +812,11 @@ const mapDispatchToProps = dispatch => ({ ...@@ -815,7 +812,11 @@ const mapDispatchToProps = dispatch => ({
onSeeCommunity: () => dispatch(setPlayer(true)) onSeeCommunity: () => dispatch(setPlayer(true))
}); });
export default injectIntl(connect( export default compose(
mapStateToProps, injectIntl,
mapDispatchToProps ConfirmReplaceHOC,
)(MenuBar)); connect(
mapStateToProps,
mapDispatchToProps
)
)(MenuBar);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment