File size: 2,673 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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(
    <div>
    <label>
    <i className="fas fa-camera"></i>
    <input type="file" onChange={this.uploadNewPhoto}/>
    </label>
    </div>
    );
  }

  render () {
  return (
  <div className="create-post1">
   <div className="post-title">
   <i class="fas fa-pencil-alt fav-icon"></i>
   <h3 className="bor deadlinks"> Make Post </h3>
   <i class="far fa-images fav-icon"></i>
   <h3 className="bor bcol deadlinks"> Photo/Video Album </h3>
   <i class="fas fa-video fav-icon"></i>
   <h3 className=" bcol deadlinks"> Live Video</h3>
   </div>
   <section className="p-triangle"></section>
    <form onSubmit={this.handleSubmit}>
      <div className="post-name new-pad">
      <section><img className="profile_img nsp nimg"
        src={this.props.currentUser.profile_image_url} /></section>
      <label>
      <textarea
        value={this.state.body}
        placeholder={`Whats in your mind , ${this.props.currentUser.fname} ?`}
        onChange={this.update('body')}/>
      </label>
      </div>
      <div className="last-buttons">
      {this.postPicture()}

      <input type="submit" value="Post" />
      </div>
      </form>
    </div>
    );
  }
}

export default withRouter(PostForm);