import React, { useEffect, useState } from 'react'; import PropTypes from 'prop-types'; import ReactRouterPropTypes from 'react-router-prop-types'; import { UserType } from './Types'; import moment from 'moment'; import PM from './PM'; import MessageInput from './MessageInput'; import UserInfo from './UserInfo'; import { getChat, pm } from '../api'; import './Chat.css'; export default function Chat(props) { const [chats, setChats] = useState([]); useEffect(() => { console.log(props.connection); if (props.connection) { props.connection.addEventListener('msg', onMessage); } loadChat(props.match.params.user); return () => { if (props.connection) { props.connection.removeEventListener('msg', onMessage); } }; }, [props.connection]); let loadChat = (uname) => { const { hash } = props.visitor; setChats([]); if (hash && uname) { getChat(uname) .then(response => { setChats(response.data); }); } }; let onMessage = (json) => { const msg = JSON.parse(json.data); if (msg.user.uname === props.match.params.user) { setChats([msg, ...chats]); } }; let onSend = (template) => { pm(template.to.uname, template.body) .then(res => { loadChat(props.match.params.user); }).catch(console.log); }; const uname = props.match.params.user; return (
No chat selected