1 | let id = 0;
|
2 |
|
3 | export default class MockUpload {
|
4 | id = (() => {
|
5 | id += 1;
|
6 | return id.toString();
|
7 | })();
|
8 |
|
9 | percentage = 0;
|
10 |
|
11 | errorMessage = null;
|
12 |
|
13 | status = 'pending';
|
14 |
|
15 | onProgress = [];
|
16 |
|
17 | onSuccess = [];
|
18 |
|
19 | onError = [];
|
20 |
|
21 | sendPassword = password => {
|
22 | if (password) {
|
23 | this.success();
|
24 | } else {
|
25 | this.error('Incorrect password', 'encrypted');
|
26 | }
|
27 | };
|
28 |
|
29 | progress = (amount = 10) => {
|
30 | this.percentage = Math.min(Math.max(amount + this.percentage, 0), 100);
|
31 | this.errorMessage = null;
|
32 | this.onProgress.forEach(a => a());
|
33 | if (this.percentage === 100) {
|
34 | this.onSuccess.forEach(a => a());
|
35 | }
|
36 | };
|
37 |
|
38 | success = () => {
|
39 | this.percentage = 100;
|
40 | this.errorMessage = null;
|
41 | this.status = 'accepted';
|
42 | this.onSuccess.forEach(a => a());
|
43 | };
|
44 |
|
45 | error = (message = null, status = 'rejected') => {
|
46 | this.errorMessage = message;
|
47 | this.status = status;
|
48 | this.onError.forEach(a => a());
|
49 | };
|
50 |
|
51 | reset = () => {
|
52 | this.percentage = 0;
|
53 | this.errorMessage = null;
|
54 | this.status = 'pending';
|
55 | this.progress(0);
|
56 | };
|
57 | }
|