import React from 'react'; import { withRouter } from 'react-router-dom'; import merge from 'lodash/merge'; class PostForm extends React.Component { constructor(props) { super(props); this.state ={ author_id:this.props.user.id, body:this.props.post.body, photo:{imageFile:null,imageUrl:null}, wall_id:this.props.user.id }; this.handleSubmit = this.handleSubmit.bind(this); this.uploadNewPhoto = this.uploadNewPhoto.bind(this); this.postPicture = this.postPicture.bind(this); } resetPostForm() { this.setState({ body: '', photo: {imageFile:null,imageUrl:null} }); } update(field) { return (e) => { this.setState({[field]: e.target.value}); }; } handleSubmit(e) { e.preventDefault(); var formData = new FormData(); formData.append("post[body]", this.state.body); if (this.state.photo.imageFile) { formData.append("post[photo]", this.state.photo.imageFile); } formData.append("post[wall_id]", this.props.user.id); formData.append("post[author_id]", this.props.currentUser.id); this.props.action(formData).then(()=>{ this.resetPostForm(); }); } uploadNewPhoto(e) { var reader = new FileReader(); var file = e.currentTarget.files[0]; reader.onloadend = function() { this.setState({photo:{ imageFile: file, imageUrl: reader.result }}); }.bind(this); if (file) { reader.readAsDataURL(file); } } postPicture(){ return(