aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/ui/Post.js
blob: fbd55675c864c7ddbf0f110e015ca542645d8612 (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
59
60
61
import { useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';

import qs from 'qs';

import Button from './Button';
import MessageInput from './MessageInput';

import { post, update } from '../api';
import { useVisitor } from './VisitorContext';
import { Helmet } from 'react-helmet';

/**
 *
 */
export default function Post() {
  const location = useLocation();
  const navigate = useNavigate();
  const [visitor] = useVisitor();
  let draftMessage = (location.state || {}).data || {};
  let [draft, setDraft] = useState(draftMessage.body);
  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;
          navigate(`/${visitor.uname}/${msg.mid}`);
        }
      }).catch(console.log);
  };
  let appendTag = (tag) => {
    setDraft(prevDraft => {
      return `${prevDraft || ''} *${tag} `;
    });
  };
  return (
    <div className="msg-cont">
      <Helmet>
        <title>Post</title>
      </Helmet>
      <MessageInput rows={7} text={params.body || draft || ''} data={{}} onSend={postMessage}>
        *weather It is very cold today!
      </MessageInput>
      {
        visitor.tagStats &&
        <div style={{ padding: '6px' }}>
          <p>Tags:</p>
          {
            visitor.tagStats.map(t => {
              return (<Button key={t.tag} onClick={() => { appendTag(t.tag); }}>{t.tag}</Button>);
            })
          }
        </div>
      }
    </div>
  );
}