11import bindAll from 'lodash.bindall' ;
22import React from 'react' ;
33import PropTypes from 'prop-types' ;
4+ import { defineMessages , intlShape , injectIntl } from 'react-intl' ;
45import { connect } from 'react-redux' ;
56import log from '../lib/log' ;
7+ import sharedMessages from './shared-messages' ;
68
79import {
810 LoadingStates ,
@@ -19,13 +21,13 @@ import {
1921 closeFileMenu
2022} from '../reducers/menus' ;
2123
22- // const messages = defineMessages({
23- // loadError: {
24- // id: 'gui.projectLoader.loadError',
25- // defaultMessage: 'The project file that was selected failed to load.',
26- // description: 'An error that displays when a local project file fails to load.'
27- // }
28- // });
24+ const messages = defineMessages ( {
25+ loadError : {
26+ id : 'gui.projectLoader.loadError' ,
27+ defaultMessage : 'The project file that was selected failed to load.' ,
28+ description : 'An error that displays when a local project file fails to load.'
29+ }
30+ } ) ;
2931
3032/**
3133 * Higher Order Component to provide behavior for loading local project files into editor.
@@ -45,15 +47,9 @@ const SBFileUploaderHOC = function (WrappedComponent) {
4547 'handleStartSelectingFileUpload' ,
4648 'setFileInput' ,
4749 'handleChange' ,
48- 'handleConfirmClickedCancel' ,
49- 'handleConfirmClickedOK' ,
50- 'handleConfirmClose' ,
5150 'onload' ,
5251 'resetFileInput'
5352 ] ) ;
54- this . state = {
55- showingConfirm : false
56- } ;
5753 }
5854 componentWillMount ( ) {
5955 this . reader = new FileReader ( ) ;
@@ -90,6 +86,7 @@ const SBFileUploaderHOC = function (WrappedComponent) {
9086 // called when user has finished selecting a file to upload
9187 handleChange ( e ) {
9288 const {
89+ intl,
9390 isShowingWithoutId,
9491 loadingState,
9592 projectChanged,
@@ -103,25 +100,20 @@ const SBFileUploaderHOC = function (WrappedComponent) {
103100 // If user owns the project, or user has changed the project,
104101 // we must confirm with the user that they really intend to replace it.
105102 // (If they don't own the project and haven't changed it, no need to confirm.)
103+ let uploadAllowed = true ;
106104 if ( userOwnsProject || ( projectChanged && isShowingWithoutId ) ) {
107- this . setState ( { showingConfirm : true } ) ;
108- } else {
105+ uploadAllowed = confirm ( // eslint-disable-line no-alert
106+ intl . formatMessage ( sharedMessages . replaceProjectWarning )
107+ ) ;
108+ }
109+ if ( uploadAllowed ) {
109110 this . props . requestProjectUpload ( loadingState ) ;
111+ } else {
112+ this . resetFileInput ( ) ;
110113 }
114+ this . props . closeFileMenu ( ) ;
111115 }
112116 }
113- handleConfirmClickedCancel ( ) {
114- this . resetFileInput ( ) ;
115- this . handleConfirmClose ( ) ;
116- }
117- handleConfirmClickedOK ( ) {
118- this . props . requestProjectUpload ( this . props . loadingState ) ;
119- this . handleConfirmClose ( ) ;
120- }
121- handleConfirmClose ( ) {
122- this . setState ( { showingConfirm : false } ) ;
123- this . props . closeFileMenu ( ) ;
124- }
125117 // called when file upload raw data is available in the reader
126118 onload ( ) {
127119 if ( this . reader ) {
@@ -140,8 +132,7 @@ const SBFileUploaderHOC = function (WrappedComponent) {
140132 } )
141133 . catch ( error => {
142134 log . warn ( error ) ;
143- // NOTE: revise this
144- alert ( 'this.props.intl.formatMessage(messages.loadError)' ) ; // eslint-disable-line no-alert
135+ this . props . intl . formatMessage ( messages . loadError ) ; // eslint-disable-line no-alert
145136 this . props . onLoadingFinished ( this . props . loadingState , false ) ;
146137 // Reset the file input after project is loaded
147138 // This is necessary in case the user wants to reload a project
@@ -183,9 +174,6 @@ const SBFileUploaderHOC = function (WrappedComponent) {
183174 return (
184175 < React . Fragment >
185176 < WrappedComponent
186- fileUploadConfirmVisible = { this . state . showingConfirm }
187- onCancelFileUpload = { this . handleConfirmClickedCancel }
188- onConfirmFileUpload = { this . handleConfirmClickedOK }
189177 onStartSelectingFileUpload = { this . handleStartSelectingFileUpload }
190178 { ...componentProps }
191179 />
@@ -199,6 +187,7 @@ const SBFileUploaderHOC = function (WrappedComponent) {
199187 canSave : PropTypes . bool ,
200188 cancelFileUpload : PropTypes . func ,
201189 closeFileMenu : PropTypes . func ,
190+ intl : intlShape . isRequired ,
202191 isLoadingUpload : PropTypes . bool ,
203192 isShowingWithoutId : PropTypes . bool ,
204193 loadingState : PropTypes . oneOf ( LoadingStates ) ,
@@ -240,11 +229,11 @@ const SBFileUploaderHOC = function (WrappedComponent) {
240229 const mergeProps = ( stateProps , dispatchProps , ownProps ) => Object . assign (
241230 { } , stateProps , dispatchProps , ownProps
242231 ) ;
243- return connect (
232+ return injectIntl ( connect (
244233 mapStateToProps ,
245234 mapDispatchToProps ,
246235 mergeProps
247- ) ( SBFileUploaderComponent ) ;
236+ ) ( SBFileUploaderComponent ) ) ;
248237} ;
249238
250239export {
0 commit comments