From e7c6862fd0a00327edec3e02a22ada8669b6978e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 28 Aug 2020 13:17:19 -0500 Subject: [PATCH] Chats: refactor ChatBox into its own component --- app/soapbox/actions/chats.js | 11 ++ app/soapbox/features/chats/chat_room.js | 81 +++---------- .../features/chats/components/chat_box.js | 108 ++++++++++++++++++ .../features/chats/components/chat_window.js | 74 ++---------- app/soapbox/features/chats/index.js | 1 - app/soapbox/reducers/chat_messages.js | 1 + app/styles/chats.scss | 41 ++++--- 7 files changed, 170 insertions(+), 147 deletions(-) create mode 100644 app/soapbox/features/chats/components/chat_box.js diff --git a/app/soapbox/actions/chats.js b/app/soapbox/actions/chats.js index 93938e3c3..d9b97ea50 100644 --- a/app/soapbox/actions/chats.js +++ b/app/soapbox/actions/chats.js @@ -111,6 +111,17 @@ export function toggleMainWindow() { }; } +export function fetchChat(chatId) { + return (dispatch, getState) => { + dispatch({ type: CHAT_FETCH_REQUEST, chatId }); + return api(getState).get(`/api/v1/pleroma/chats/${chatId}`).then(({ data }) => { + dispatch({ type: CHAT_FETCH_SUCCESS, chat: data }); + }).catch(error => { + dispatch({ type: CHAT_FETCH_FAIL, chatId, error }); + }); + }; +} + export function startChat(accountId) { return (dispatch, getState) => { dispatch({ type: CHAT_FETCH_REQUEST, accountId }); diff --git a/app/soapbox/features/chats/chat_room.js b/app/soapbox/features/chats/chat_room.js index fc17f9c2e..b10ff5470 100644 --- a/app/soapbox/features/chats/chat_room.js +++ b/app/soapbox/features/chats/chat_room.js @@ -4,104 +4,51 @@ import PropTypes from 'prop-types'; import ImmutablePropTypes from 'react-immutable-proptypes'; import { injectIntl } from 'react-intl'; import ImmutablePureComponent from 'react-immutable-pure-component'; -import { - fetchChatMessages, - sendChatMessage, - markChatRead, -} from 'soapbox/actions/chats'; -import { List as ImmutableList, OrderedSet as ImmutableOrderedSet } from 'immutable'; -import ChatMessageList from './components/chat_message_list'; +import { fetchChat } from 'soapbox/actions/chats'; +import ChatBox from './components/chat_box'; import Column from 'soapbox/features/ui/components/column'; const mapStateToProps = (state, { params }) => ({ me: state.get('me'), chat: state.getIn(['chats', params.chatId]), - chatMessageIds: state.getIn(['chat_message_lists', params.chatId], ImmutableOrderedSet()), }); export default @connect(mapStateToProps) @injectIntl -class ChatWindow extends ImmutablePureComponent { +class ChatRoom extends ImmutablePureComponent { static propTypes = { dispatch: PropTypes.func.isRequired, intl: PropTypes.object.isRequired, - chatMessageIds: ImmutablePropTypes.orderedSet, chat: ImmutablePropTypes.map, me: PropTypes.node, } - static defaultProps = { - chatMessages: ImmutableList(), - } - - state = { - content: '', - } - - handleKeyDown = (chatId) => { - return (e) => { - if (e.key === 'Enter') { - this.props.dispatch(sendChatMessage(chatId, this.state)); - this.setState({ content: '' }); - e.preventDefault(); - } - }; - } - - handleContentChange = (e) => { - this.setState({ content: e.target.value }); - } - - handleReadChat = (e) => { - const { dispatch, chat } = this.props; - dispatch(markChatRead(chat.get('id'))); - } + handleInputRef = (el) => { + this.inputElem = el; + this.focusInput(); + }; focusInput = () => { if (!this.inputElem) return; this.inputElem.focus(); } - setInputRef = (el) => { - this.inputElem = el; - this.focusInput(); - }; - componentDidMount() { - const { dispatch, chatMessages, params } = this.props; - if (chatMessages && chatMessages.count() < 1) - dispatch(fetchChatMessages(params.chatId)); - } - - componentDidUpdate(prevProps) { - const markReadConditions = [ - () => this.props.chat !== undefined, - () => document.activeElement === this.inputElem, - () => this.props.chat.get('unread') > 0, - ]; - - if (markReadConditions.every(c => c() === true)) - this.handleReadChat(); + const { dispatch, params } = this.props; + dispatch(fetchChat(params.chatId)); } render() { - const { chatMessageIds, chat } = this.props; + const { chat } = this.props; if (!chat) return null; return ( - -
-