@@ -5,7 +5,6 @@ import { translate } from 'react-i18next';
55
66import Button from 'components/Button' ;
77
8- import core from 'core' ;
98import getClassName from 'helpers/getClassName' ;
109import actions from 'actions' ;
1110import selectors from 'selectors' ;
@@ -15,7 +14,9 @@ import './PasswordModal.scss';
1514class PasswordModal extends React . PureComponent {
1615 static propTypes = {
1716 isOpen : PropTypes . bool ,
17+ attempt : PropTypes . number . isRequired ,
1818 checkPassword : PropTypes . func ,
19+ setPasswordAttempts : PropTypes . func . isRequired ,
1920 t : PropTypes . func . isRequired ,
2021 closeElement : PropTypes . func . isRequired
2122 }
@@ -24,59 +25,43 @@ class PasswordModal extends React.PureComponent {
2425 super ( ) ;
2526 this . maxAttempts = 3 ;
2627 this . passwordInput = React . createRef ( ) ;
27- this . state = {
28- attempt : 0 ,
28+ this . initialState = {
2929 password : '' ,
3030 userCanceled : false
3131 } ;
32- }
33-
34- componentDidMount ( ) {
35- core . addEventListener ( 'beforeDocumentLoaded' , this . onBeforeDocumentLoaded ) ;
32+ this . state = this . initialState ;
3633 }
3734
3835 componentDidUpdate ( prevProps ) {
3936 if ( ! prevProps . isOpen && this . props . isOpen ) {
4037 this . props . closeElement ( 'loadingModal' ) ;
4138 }
39+ if ( prevProps . isOpen && ! this . props . isOpen ) {
40+ // when a user enters the correct password or calls core.closeDocument
41+ // reset state in case user loads another password-protected document
42+ this . setState ( this . initialState ) ;
43+ }
4244 if ( this . passwordInput . current ) {
4345 this . passwordInput . current . focus ( ) ;
4446 }
4547 }
4648
47- componentWillUnmount ( ) {
48- core . removeEventListener ( 'beforeDocumentLoaded' , this . onBeforeDocumentLoaded ) ;
49- }
50-
51- onBeforeDocumentLoaded = ( ) => {
52- this . setState ( {
53- attempt : 0
54- } ) ;
55- }
56-
5749 handleInputChange = e => {
5850 this . setState ( { password : e . target . value } ) ;
5951 }
6052
6153 handleSubmit = e => {
6254 e . preventDefault ( ) ;
6355
64- const { checkPassword, closeElement } = this . props ;
65-
66- this . setState ( prevState => ( {
67- attempt : prevState . attempt + 1 ,
68- password : ''
69- } ) ) ;
70- checkPassword ( this . state . password ) ;
71- closeElement ( 'passwordModal' ) ;
56+ this . props . checkPassword ( this . state . password ) ;
7257 }
7358
7459 handleCancel = ( ) => {
7560 this . setState ( { userCanceled : true } ) ;
7661 }
7762
7863 renderContent = ( ) => {
79- const userExceedsMaxAttempts = this . state . attempt === this . maxAttempts ;
64+ const userExceedsMaxAttempts = this . props . attempt === this . maxAttempts ;
8065
8166 if ( userExceedsMaxAttempts ) {
8267 return this . renderMaxAttemptsContent ( ) ;
@@ -98,7 +83,7 @@ class PasswordModal extends React.PureComponent {
9883
9984 renderEnterPasswordContent = ( ) => {
10085 const { t } = this . props ;
101- const wrongPassword = this . state . attempt !== 0 ;
86+ const wrongPassword = this . props . attempt !== 0 ;
10287
10388 return (
10489 < div className = "wrapper" >
@@ -117,7 +102,7 @@ class PasswordModal extends React.PureComponent {
117102 { wrongPassword &&
118103 < div className = "incorrect-password" >
119104 { t ( 'message.incorrectPassword' , {
120- remainingAttempts : this . maxAttempts - this . state . attempt
105+ remainingAttempts : this . maxAttempts - this . props . attempt
121106 } ) }
122107 </ div >
123108 }
@@ -153,10 +138,12 @@ class PasswordModal extends React.PureComponent {
153138
154139const mapStateToProps = state => ( {
155140 isOpen : selectors . isElementOpen ( state , 'passwordModal' ) ,
156- checkPassword : selectors . getCheckPasswordFunction ( state )
141+ checkPassword : selectors . getCheckPasswordFunction ( state ) ,
142+ attempt : selectors . getPasswordAttempts ( state )
157143} ) ;
158144
159145const mapDispatchToProps = {
146+ setPasswordAttempts : actions . setPasswordAttempts ,
160147 closeElement : actions . closeElement
161148} ;
162149
0 commit comments