File size: 2,878 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
import React from 'react';
import { withRouter, Link } from 'react-router-dom';

class PostForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = props.post;
    this.handleSubmit = this.handleSubmit.bind(this);
    this.handleImageUpload = this.handleImageUpload.bind(this);

    this.photoData;
  }

  componentDidUpdate(prevProps) {
    if (prevProps.match.params.userUrl !== this.props.match.params.userUrl) {
      const url = this.props.match.params.userUrl;
      let recipientId;
      
      if (url === undefined) {
        recipientId = authorId;
      } else {
        recipientId = this.props.userUrls[url];
      }
      
      this.setState({recipientId});
    }
  }

  handleChange(field) {
    return e => this.setState({[field]: e.target.value})
  }

  handleImageUpload(e) {
    console.log(e.target.files[0]);
    this.setState({
      image: URL.createObjectURL(e.target.files[0])
    })

    this.photoData = e.currentTarget.files[0];
  }

  handleSubmit(e) {
    e.preventDefault();

    // make sure the post body isn't just whitespace
    if ((/\S+/).test(this.state.body)) {
      const postData = new FormData();

      for (let key in this.state) {
        postData.append(`post[${key}]`, this.state[key]);
      }
      if (this.photoData) {
        postData.append('post[image]', this.photoData);
      } else {
        postData.delete('post[image]');
      }

      this.props.action(postData);

      this.setState({body: "", image: null});
      this.photoData = null;
      if (this.props.hideModal) this.props.hideModal();
    }
  }

  placeholderText() {
    if (this.state.authorId === this.state.recipientId) {
      return `What's on your mind, ${this.props.author.fname}?`;
    } else {
      return `Write something to ${this.props.recipient.fname}...`
    }
  }

  render() {
    const { formType, formButtonText, author } = this.props;
    
    return <div className="post-container">
        <div className="post-form-header">{formType}</div>
        <div className="post-form-content">
          <Link to={`/${author.userUrl}`} replace className="post-thumbnail" style={{ backgroundImage: `url(${author.profilePictureUrl})` }} />
          <form onSubmit={this.handleSubmit}>
            <textarea value={this.state.body} onChange={this.handleChange("body")} placeholder={this.placeholderText()} />
            { this.photoData ? <img src={this.state.image} /> : null }
            <div className="post-form-footer">
              <label htmlFor="image-upload">
                <i className="fa fa-paperclip" aria-hidden="true" />
              </label>
              <input id="image-upload" type="file" onChange={this.handleImageUpload} />
              <button>{formButtonText}</button>
            </div>
          </form>
        </div>
      </div>;
  }
}

export default withRouter(PostForm);