-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhotosUploader.js
More file actions
208 lines (189 loc) · 7.68 KB
/
PhotosUploader.js
File metadata and controls
208 lines (189 loc) · 7.68 KB
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { NavLink } from 'react-router-dom';
import { connect } from 'react-redux';
import request from 'superagent';
import Dropzone from 'react-dropzone';
import { photosUploaded, updateUploadedPhoto } from '../actions';
import UploadedPhotoStatusContainer from './UploadedPhotosStatus';
import {CloudinaryContext} from "cloudinary-react";
class PhotosUploader extends Component {
constructor(props, context) {
super(props, context);
this.state = { uploadedPhotos: [] };
this.photoId = 1;
this.locationMiddleware = this.locationMiddleware.bind(this)
this.onPhotoSelected = this.onPhotoSelected.bind(this)
}
render() {
return (
<div>
<Dropzone
id="direct-upload-dropzone"
disableClick={true}
multiple={false}
accept="image/*"
style={{ position: 'relative' }}
onDrop={this.locationMiddleware.bind(this)}
>
<div id="direct_upload">
<h1>New Photo</h1>
<h2>
Direct upload from the browser with React File
Upload
</h2>
<p>
You can also drag and drop an image file into the
dashed area.
</p>
<form>
<div className="form_line">
<label path="title">Put your location:</label>
<div className="form_controls">
<input
type="text"
ref={titleEl =>
(this.titleEl = titleEl)
}
className="form-control"
placeholder="Title"
/>
</div>
</div>
<div className="form_line">
<label>Image:</label>
<div className="form_controls">
<div className="upload_button_holder">
<label
className="upload_button"
htmlFor="fileupload"
>
Upload
</label>
<input
type="file"
id="fileupload"
accept="image/*"
multiple="multiple"
ref={fileInputEl =>
(this.fileInputEl = fileInputEl)
}
onChange={() =>
this.locationMiddleware(
this.fileInputEl.files
)
}
/>
</div>
</div>
</div>
</form>
<h2>Status</h2>
</div>
{this.props.uploadedPhotos.map((uploadedPhoto, index) => {
return (
<UploadedPhotoStatusContainer
key={index}
uploadedPhoto={uploadedPhoto}
/>
);
})}
</Dropzone>
<NavLink className="back_link" exact to="/photos">
Back to list
</NavLink>
</div>
);
}
onPhotoSelected(files, position) {
let pos = position.coords.latitude.toString() + ' , ' + position.coords.longitude.toString()
console.log(pos)
const url = `https://api.cloudinary.com/v1_1/${
this.context.cloudName
}/upload`;
const title = this.titleEl.value;
console.log(typeof(title))
console.log('now 1')
for (let file of files) {
const photoId = this.photoId++;
const fileName = file.name;
console.log('now 2')
console.log(pos)
request.post(url)
.field('upload_preset', this.context.uploadPreset)
.field('file', file)
.field('multiple', true)
.field('tags', title ? `myphotoalbum,${title}` : 'myphotoalbum')
.field('context', `location=${pos}`)
.on('progress', (progress) => this.onPhotoUploadProgress(photoId, file.name, progress))
.end((error, response) => {
this.onPhotoUploaded(photoId, fileName, response);
});
}
}
locationMiddleware(files) {
console.log(' in location middleware')
navigator.geolocation.getCurrentPosition((position) => {
let pos = position.coords.latitude.toString() + ' , ' + position.coords.longitude.toString()
console.log(pos)
const url = `https://api.cloudinary.com/v1_1/${
this.context.cloudName
}/upload`;
const title = this.titleEl.value;
console.log(typeof(title))
console.log('now 1')
for (let file of files) {
const photoId = this.photoId++;
const fileName = file.name;
console.log('now 2')
console.log(pos)
request.post(url)
.field('upload_preset', this.context.uploadPreset)
.field('file', file)
.field('multiple', true)
.field('tags', title ? `myphotoalbum,${title}` : 'myphotoalbum')
.field('context', `location=${pos}`)
.on('progress', (progress) => this.onPhotoUploadProgress(photoId, file.name, progress))
.end((error, response) => {
this.onPhotoUploaded(photoId, fileName, response);
});
}
})
}
alerti() {
alert('asldkamsldkm')
}
onPhotoUploadProgress(id, fileName, progress) {
this.props.onUpdateUploadedPhoto({
id: id,
fileName: fileName,
progress: progress,
});
}
onPhotoUploaded(id, fileName, response) {
this.props.onUpdateUploadedPhoto({
id: id,
fileName: fileName,
response: response,
});
this.props.onPhotosUploaded([response.body]);
}
static contextType = CloudinaryContext.contextType;
}
PhotosUploader.propTypes = {
uploadedPhotos: PropTypes.array,
onUpdateUploadedPhoto: PropTypes.func,
onPhotosUploaded: PropTypes.func,
};
const PhotosUploaderContainer = connect(
state => state,
{
onUpdateUploadedPhoto: updateUploadedPhoto,
onPhotosUploaded: photosUploaded,
}
)(PhotosUploader);
Object.assign(
PhotosUploaderContainer.contextTypes,
PhotosUploader.contextTypes
);
export default PhotosUploaderContainer;