aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/components/Post.js
blob: 8dd8f37add9febe60ffc211903238c2c82298ad6 (plain) (blame)
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
import React from 'react';

import Button from './Button';

import { post } from '../api';

export default class Post extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      attach : '',
      body: ''
    }
    this.fileinput = React.createRef();
    console.log(props)
  }
  submit = (event) => {
    if (event.preventDefault) event.preventDefault();
    const {attach, body} = this.state;
    const input = this.fileinput.current;
    post(body, attach ? input.files[0] : '')
      .then(response => {
        console.log(response)
        if (response.status === 200) {
          const msg = response.data.newMessage;
          console.log(msg)
          this.props.history.push(`/${this.props.visitor.uname}/${msg.mid}`);
        }
      }).catch(console.log);
  }
  attachChanged = (event) => {
    this.setState({
      attach: event.target.value
    })
  }
  bodyChanged = (event) => {
    this.setState({
      body: event.target.value
    })
  }
  render() {
    return (
    <article>
      <form id="postmsg">
        <p style={{ textAlign: 'left' }}>
          <b>Фото:</b> <span id="attachmentfile">
          <input style={{ width: '100%' }} type="file" name="attach" ref={this.fileinput} value={this.state.attach} onChange={this.attachChanged}/> <i>(JPG/PNG)</i></span>
        </p>
        <p>
          <textarea name="body" value={this.state.body} onChange={this.bodyChanged} className="newmessage" rows="7" cols="10" placeholder="*weather It's very cold today!"></textarea>
          <br/>
          <Button className="subm" onClick={this.submit}>POST</Button>
        </p>
      </form>
    </article>
    );
  }
}