-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.jsx
208 lines (182 loc) · 5.11 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// @flow
import React, { Component } from 'react';
// For develop/testing purposes the package build is copying
// S3PostUploader to components
//import S3PostUploader, {
//Error as UploadError,
//S3Result,
//} from '../S3PostUploader';
import S3PostUploader, {
Error as UploadError,
S3Result,
} from '@utrustdev/react-s3-post-uploader';
import Button from '@material-ui/core/Button';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemText from '@material-ui/core/ListItemText';
import Avatar from '@material-ui/core/Avatar';
import ImageIcon from '@material-ui/icons/Image';
import CircularProgress from '@material-ui/core/CircularProgress';
import './index.css';
type State = {
isLoading: boolean,
uploadPercentage: number,
uploadError: ?UploadError,
attachments: Array<S3Result>,
};
class Uploads extends Component<{}, State> {
state = {
isLoading: false,
uploadPercentage: 0,
uploadError: undefined,
attachments: [],
};
uploadInput: HTMLInputElement;
onClick = (event: SyntheticEvent<HTMLInputElement>) => {
event.preventDefault();
this.uploadInput.click();
};
openAttachment = (url: string) => {
return (event: SyntheticEvent<>) => {
event.preventDefault();
const win = window.open(url, '_blank');
win.focus();
};
};
setInputRef = (input: HTMLInputElement) => {
this.uploadInput = input;
};
setUploadState = (loading: boolean, error: UploadError) => {
this.setState({ isLoading: loading, uploadError: error });
};
setUploadPercentage = (uploadPercentage: number) => {
this.setState({ uploadPercentage: uploadPercentage });
};
addAttachment = (attachment: S3Result) => {
this.setState(prevState => ({
attachments: [...prevState.attachments, attachment],
}));
};
onUploadStart = () => {
this.setUploadState(true);
};
onUploadProgress = (progressEvent: ProgressEvent) => {
const uploadPercentage = Math.round(
(progressEvent.loaded / progressEvent.total) * 100
);
this.setUploadPercentage(uploadPercentage);
};
onUploadFinish = (uploadResult: S3Result, file: File) => {
this.addAttachment(uploadResult);
this.setUploadState(false);
this.setUploadPercentage(0);
};
onUploadError = (error: UploadError) => {
this.setUploadState(false, error);
};
getCredentials = (file: File, callback: Function) => {
const postData = {
filename: file.name,
mimeType: file.type,
path: 'album/123',
};
fetch('http://localhost:5000/get_credentials', {
method: 'POST',
headers: {
'Content-Type': 'application/vnd.api+json',
},
body: JSON.stringify(postData),
})
.then(response => {
return response.json();
})
.then(({ url, fields }) => {
callback(file, {
uploadUrl: url,
params: {
acl: fields.acl,
key: fields.key,
policy: fields.Policy,
success_action_status: fields.success_action_status,
'content-type': fields['Content-Type'],
'x-amz-signature': fields['X-Amz-Signature'],
'x-amz-algorithm': fields['X-Amz-Algorithm'],
'x-amz-date': fields['X-Amz-Date'],
'x-amz-credential': fields['X-Amz-Credential'],
},
});
})
.catch(error => {
this.onUploadError(error);
});
};
renderAttachments = () => {
const { attachments } = this.state;
return (
<List className="attachments">
<h3>Attachments</h3>
{attachments.map(attachment => (
<ListItem
key={attachment.etag}
onClick={this.openAttachment(attachment.location)}
button
>
<Avatar>
<ImageIcon />
</Avatar>
<ListItemText
primary={attachment.key}
secondary={attachment.location}
/>
</ListItem>
))}
</List>
);
};
renderError = () => {
const { uploadError } = this.state;
if (uploadError) {
return (
<div role="alert" className="errorMessage">
{uploadError.code}
{uploadError.message}
</div>
);
}
};
renderButton = () => {
const { isLoading, uploadPercentage } = this.state;
if (isLoading)
return (
<>
<CircularProgress />
<label className="percentage">{uploadPercentage}%</label>
</>
);
return (
<div>
<Button onClick={this.onClick} variant="outlined" component="span">
Upload Attachments
</Button>
{this.renderError()}
</div>
);
};
render() {
return (
<div className="uploads">
{this.renderAttachments()}
<S3PostUploader
onStart={this.onUploadStart}
onProgress={this.onUploadProgress}
onFinish={this.onUploadFinish}
onError={this.onUploadError}
getCredentials={this.getCredentials}
inputRef={this.setInputRef}
/>
{this.renderButton()}
</div>
);
}
}
export default Uploads;