aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/ui/Post.js
blob: dc1c7a9d0de9a20bba30773b786d8093f24835eb (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
import React, { memo } from 'react';

import ReactRouterPropTypes from 'react-router-prop-types';
import { UserType } from './Types';

import qs from 'qs';

import MessageInput from './MessageInput';

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

function PostComponent({ location, visitor, history }) {
  let draftMessage = (location.state || {}).draft || {};
  let params = qs.parse(window.location.search.substring(1));
  let postMessage = (template) => {
    const { attach, body } = template;
    const postAction = draftMessage.mid ? update(draftMessage.mid, 0, body) : post(body, attach);
    postAction
      .then(response => {
        if (response.status === 200) {
          const msg = response.data.newMessage;
          history.push(`/${visitor.uname}/${msg.mid}`);
        }
      }).catch(console.log);
  };
  return (
    <div className="msg-cont">
      <MessageInput rows="7" text={params.body || draftMessage.body || ''} data={{ mid: 0, timestamp: '0' }} onSend={postMessage}>
        *weather It is very cold today!
          </MessageInput>
    </div>
  );
}

export default memo(PostComponent);

PostComponent.propTypes = {
  location: ReactRouterPropTypes.location,
  history: ReactRouterPropTypes.history.isRequired,
  visitor: UserType
};