1 | import React, { Component } from 'react';
|
2 | import PropTypes from 'prop-types';
|
3 | import Progress from '@availity/progress';
|
4 |
|
5 | import {
|
6 | Button,
|
7 | Modal,
|
8 | ModalHeader,
|
9 | ModalBody,
|
10 | ModalFooter,
|
11 | Label,
|
12 | Input,
|
13 | } from 'reactstrap';
|
14 |
|
15 | class UploadProgressBar extends Component {
|
16 | constructor(props) {
|
17 | super(props);
|
18 | this.state = {
|
19 | percentage: props.upload.percentage || 0,
|
20 | };
|
21 | props.upload.onProgress.push(this.onProgress);
|
22 | props.upload.onSuccess.push(this.onSuccess);
|
23 | props.upload.onError.push(this.onError);
|
24 | }
|
25 |
|
26 | onProgress = () => {
|
27 | const { upload, onProgress } = this.props;
|
28 | this.setState({ percentage: upload.percentage, error: false });
|
29 | if (onProgress) onProgress(upload);
|
30 | };
|
31 |
|
32 | onSuccess = () => {
|
33 | const { onSuccess, upload } = this.props;
|
34 | this.setState({ percentage: 100, error: false });
|
35 | if (onSuccess) onSuccess(upload);
|
36 | };
|
37 |
|
38 | onError = () => {
|
39 | const { onError, upload } = this.props;
|
40 | this.setState({ error: true });
|
41 | if (onError) onError(upload);
|
42 | };
|
43 |
|
44 | verifyPassword = event => {
|
45 | const { upload } = this.props;
|
46 | const { password } = this.state;
|
47 | event.preventDefault();
|
48 | upload.sendPassword(password);
|
49 | this.toggleModal();
|
50 | };
|
51 |
|
52 | handlePasswordChange = event => {
|
53 | this.setState({ password: event.target.value });
|
54 | };
|
55 |
|
56 | toggleModal = () => {
|
57 | this.setState(({ modalOpen }) => ({ modalOpen: !modalOpen, password: '' }));
|
58 | };
|
59 |
|
60 | render() {
|
61 | const { upload, ...rest } = this.props;
|
62 | const { percentage, error, modalOpen } = this.state;
|
63 | return upload.errorMessage ? (
|
64 | <>
|
65 | <span data-testid="upload-error-message" className="text-danger">
|
66 | {upload.errorMessage}
|
67 | </span>
|
68 | {upload.status === 'encrypted' && (
|
69 | <div className="pwRequired" data-testid="password-form-encrypted">
|
70 | <Button size="sm" color="primary" onClick={this.toggleModal}>
|
71 | Enter password
|
72 | </Button>
|
73 | <Modal isOpen={modalOpen} toggle={this.toggleModal}>
|
74 | <form
|
75 | onSubmit={this.verifyPassword}
|
76 | data-testid="password-form-modal"
|
77 | >
|
78 | <ModalHeader toggle={this.toggleModal}>
|
79 | Enter Password
|
80 | </ModalHeader>
|
81 | <ModalBody>
|
82 | <Label for="upload-password">Password</Label>
|
83 | <Input
|
84 | id="upload-password"
|
85 | onChange={this.handlePasswordChange}
|
86 | type="password"
|
87 | placeholder="password"
|
88 | />
|
89 | </ModalBody>
|
90 | <ModalFooter>
|
91 | <Button color="primary">Ok</Button>
|
92 | </ModalFooter>
|
93 | </form>
|
94 | </Modal>
|
95 | </div>
|
96 | )}
|
97 | </>
|
98 | ) : (
|
99 | <Progress
|
100 | data-testid="upload-progress"
|
101 | value={percentage}
|
102 | complete={percentage === 100}
|
103 | color={error ? 'danger' : 'success'}
|
104 | {...rest}
|
105 | >
|
106 | <span className="sr-only">{percentage}% Complete</span>
|
107 | </Progress>
|
108 | );
|
109 | }
|
110 | }
|
111 |
|
112 | UploadProgressBar.propTypes = {
|
113 | upload: PropTypes.shape({
|
114 | sendPassword: PropTypes.func.isRequired,
|
115 | onProgress: PropTypes.array.isRequired,
|
116 | onSuccess: PropTypes.array.isRequired,
|
117 | onError: PropTypes.array.isRequired,
|
118 | percentage: PropTypes.number,
|
119 | errorMessage: PropTypes.string,
|
120 | id: PropTypes.string.isRequired,
|
121 | status: PropTypes.string,
|
122 | }).isRequired,
|
123 | onProgress: PropTypes.func,
|
124 | onSuccess: PropTypes.func,
|
125 | onError: PropTypes.func,
|
126 | animated: PropTypes.bool,
|
127 | className: PropTypes.string,
|
128 | striped: PropTypes.bool,
|
129 | };
|
130 |
|
131 | UploadProgressBar.defaultProps = {};
|
132 |
|
133 | export default UploadProgressBar;
|