-
Notifications
You must be signed in to change notification settings - Fork 0
/
TweetForm.jsx
64 lines (52 loc) · 1.44 KB
/
TweetForm.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
var React = require("react");
var Link = require("react-router").Link;
var TweetStore = require("../stores/TweetStore.js");
var TweetApi = require('../utils/TweetApi');
var TweetForm = React.createClass({
contextTypes: {
router: React.PropTypes.object.isRequired
},
getInitialState: function () {
return({
body: "",
imageFile: null,
imageUrl: null
});
},
updateBody: function (e) {
this.setState({
body: e.target.value
});
},
updateFile: function (e) {
var file = e.currentTarget.files[0];
var fileReader = new FileReader();
fileReader.onloadend = function () {
this.setState({ imageFile: file, imageUrl: fileReader.result });
}.bind(this);
if (file) {
fileReader.readAsDataURL(file);
}
},
handleSubmit: function (e) {
var formData = new FormData();
formData.append("tweet[body]", this.state.body);
formData.append("tweet[image]", this.state.imageFile);
TweetApi.createTweet(formData, this.goBack);
},
goBack: function () {
this.context.router.push("/");
},
render: function () {
return(
<div>
Tweet form!
<Link to="/">Back to Tweets</Link>
<input type="text" onChange={this.updateBody}/>
<input type="file" onChange={this.updateFile}/>
<button onClick={this.handleSubmit}>Make Tweet!</button>
<img src={this.state.imageUrl}/>
</div>);
}
});
module.exports = TweetForm;