Skip to content

Commit a7ca3f7

Browse files
ZhijieZhangjustinjung04
authored andcommitted
[bugfix] resolves #50, also fix the flashing problem when entering a wrong password and reset state for password modal when loading another document (#51)
1 parent 1706f17 commit a7ca3f7

File tree

10 files changed

+41
-37
lines changed

10 files changed

+41
-37
lines changed

src/apis/closeDocument.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import core from 'core';
22
import actions from 'actions';
33

4-
export default store => () => core.closeDocument().then(() => {
5-
store.dispatch(actions.setDocumentLoaded(false));
6-
store.dispatch(actions.closeElement('pageNavOverlay'));
7-
store.dispatch(actions.setOutlines([]));
4+
export default ({ dispatch }) => () => core.closeDocument(dispatch).then(() => {
5+
dispatch(actions.setDocumentLoaded(false));
6+
dispatch(actions.closeElement('pageNavOverlay'));
7+
dispatch(actions.setOutlines([]));
88
});

src/components/FilePickerHandler/FilePickerHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class FilePickerHandler extends React.PureComponent {
3636
this.props.setDocumentFile(file);
3737
this.props.openElement('loadingModal');
3838
this.props.closeElement('menuOverlay');
39-
core.closeDocument().then(() => {
39+
core.closeDocument(this.props.dispatch).then(() => {
4040
loadDocument({ document: this.props.document, advanced: this.props.advanced }, this.props.dispatch);
4141
});
4242
}

src/components/PasswordModal/PasswordModal.js

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { translate } from 'react-i18next';
55

66
import Button from 'components/Button';
77

8-
import core from 'core';
98
import getClassName from 'helpers/getClassName';
109
import actions from 'actions';
1110
import selectors from 'selectors';
@@ -15,7 +14,9 @@ import './PasswordModal.scss';
1514
class 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

154139
const 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

159145
const mapDispatchToProps = {
146+
setPasswordAttempts: actions.setPasswordAttempts,
160147
closeElement: actions.closeElement
161148
};
162149

src/core/closeDocument.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import actions from 'actions';
2+
13
/**
24
* https://www.pdftron.com/api/web/CoreControls.DocumentViewer.html#closeDocument__anchor
35
* @fires documentUnloaded on DocumentViewer
46
* @see onDocumentUnloaded.js (No documentation yet)
57
*/
6-
export default () => window.docViewer.closeDocument();
8+
export default dispatch => {
9+
dispatch(actions.closeElement('passwordModal'));
10+
11+
return window.docViewer.closeDocument();
12+
};
713

src/event-listeners/onBeforeDocumentLoaded.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import core from 'core';
22
import actions from 'actions';
33

44
export default dispatch => () => {
5+
// if we are opening an password-protected pdf,
6+
// this event will only be trigger after we enter the correct password, so it's safe to close this modal here
7+
dispatch(actions.closeElement('passwordModal'));
8+
59
const totalPages = core.getTotalPages();
610
const currentPage = core.getCurrentPage();
711

src/helpers/loadDocument.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { supportedPDFExtensions, supportedOfficeExtensions } from 'constants/sup
88
import actions from 'actions';
99

1010
export default (state, dispatch) => {
11-
core.closeDocument().then(() => {
11+
core.closeDocument(dispatch).then(() => {
1212
checkByteRange(state).then(streaming => {
1313
Promise.all([getPartRetriever(state, streaming), getDocOptions(state, dispatch, streaming)])
1414
.then(params => {
@@ -147,7 +147,9 @@ const getDocOptions = (state, dispatch, streaming) => {
147147

148148
Promise.all([getBackendPromise(pdfType), getBackendPromise(officeType)]).then(([pdfBackendType, officeBackendType]) => {
149149
let passwordChecked = false; // to prevent infinite loop when wrong password is passed as an argument
150+
let attempt = 0;
150151
const getPassword = checkPassword => {
152+
dispatch(actions.setPasswordAttempts(attempt++));
151153
if (password && !passwordChecked) {
152154
checkPassword(password);
153155
passwordChecked = true;

src/redux/actions/internalActions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export const setFilename = filename => ({ type: 'SET_FILENAME', payload: { filen
8787
export const setTotalPages = totalPages => ({ type: 'SET_TOTAL_PAGES', payload: { totalPages } });
8888
export const setOutlines = outlines => ({ type: 'SET_OUTLINES', payload: { outlines } });
8989
export const setCheckPasswordFunction = func => ({ type: 'SET_CHECKPASSWORD', payload: { func } });
90+
export const setPasswordAttempts = attempt => ({ type: 'SET_PASSWORD_ATTEMPTS', payload: { attempt } });
9091
export const setPrintQuality = quality => ({ type: 'SET_PRINT_QUALITY', payload: { quality } });
9192
export const setPassword = password => ({ type: 'SET_PASSWORD', payload: { password } });
9293

src/redux/initialState.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ export default {
183183
outlines: [],
184184
checkPassword: null,
185185
password: '',
186-
printQuality: 1
186+
printQuality: 1,
187+
passwordAttempts: -1,
187188
},
188189
user: {
189190
name: getHashParams('user', 'Guest'),

src/redux/reducers/documentReducer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export default initialState => (state = initialState, action) => {
2222
return { ...state, outlines: payload.outlines };
2323
case 'SET_CHECKPASSWORD':
2424
return { ...state, checkPassword: payload.func };
25+
case 'SET_PASSWORD_ATTEMPTS':
26+
return { ...state, passwordAttempts: payload.attempt };
2527
case 'SET_PASSWORD':
2628
return { ...state, password: payload.password };
2729
case 'SET_PRINT_QUALITY':

src/redux/selectors/exposedSelectors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const isEmbedPrintSupported = state => {
5555
return isPDF && isChrome;
5656
};
5757
export const getCheckPasswordFunction = state => state.document.checkPassword;
58+
export const getPasswordAttempts = state => state.document.passwordAttempts;
5859
export const getPrintQuality = state => state.document.printQuality;
5960
export const getTotalPages = state => state.document.totalPages;
6061
export const getOutlines = state => state.document.outlines;

0 commit comments

Comments
 (0)