Chats: refactor ChatBox state
This commit is contained in:
parent
b7295e238f
commit
afd823fae0
|
@ -12,6 +12,7 @@ import { OrderedSet as ImmutableOrderedSet } from 'immutable';
|
||||||
import ChatMessageList from './chat_message_list';
|
import ChatMessageList from './chat_message_list';
|
||||||
import UploadButton from 'soapbox/features/compose/components/upload_button';
|
import UploadButton from 'soapbox/features/compose/components/upload_button';
|
||||||
import { uploadMedia } from 'soapbox/actions/media';
|
import { uploadMedia } from 'soapbox/actions/media';
|
||||||
|
import { Map as ImmutableMap } from 'immutable';
|
||||||
|
|
||||||
const messages = defineMessages({
|
const messages = defineMessages({
|
||||||
placeholder: { id: 'chat_box.input.placeholder', defaultMessage: 'Send a message…' },
|
placeholder: { id: 'chat_box.input.placeholder', defaultMessage: 'Send a message…' },
|
||||||
|
@ -37,22 +38,42 @@ class ChatBox extends ImmutablePureComponent {
|
||||||
me: PropTypes.node,
|
me: PropTypes.node,
|
||||||
}
|
}
|
||||||
|
|
||||||
state = {
|
initialParams = {
|
||||||
content: '',
|
content: '',
|
||||||
media_id: undefined,
|
media_id: undefined,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
state = {
|
||||||
|
params: ImmutableMap(this.initialParams),
|
||||||
|
}
|
||||||
|
|
||||||
|
setParams = newParams => {
|
||||||
|
const { params } = this.state;
|
||||||
|
this.setState({ params: params.merge(newParams) });
|
||||||
|
}
|
||||||
|
|
||||||
|
clearParams = () => {
|
||||||
|
this.setState({ params: ImmutableMap(this.initialParams) });
|
||||||
|
}
|
||||||
|
|
||||||
sendMessage = () => {
|
sendMessage = () => {
|
||||||
const { chatId } = this.props;
|
const { chatId } = this.props;
|
||||||
const { content, media_id } = this.state;
|
const { params } = this.state;
|
||||||
if (content.length < 1 && !media_id) return;
|
|
||||||
this.props.dispatch(sendChatMessage(chatId, this.state));
|
const conds = [
|
||||||
this.setState({ content: '' });
|
params.get('content', '').length > 0,
|
||||||
|
params.get('media_id'),
|
||||||
|
];
|
||||||
|
|
||||||
|
if (conds.some(c => c)) {
|
||||||
|
this.props.dispatch(sendChatMessage(chatId, params.toJS()));
|
||||||
|
this.clearParams();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
insertLine = () => {
|
insertLine = () => {
|
||||||
const { content } = this.state;
|
const { params } = this.state;
|
||||||
this.setState({ content: content + '\n' });
|
this.setParams({ content: params.get('content') + '\n' });
|
||||||
}
|
}
|
||||||
|
|
||||||
handleKeyDown = (e) => {
|
handleKeyDown = (e) => {
|
||||||
|
@ -66,7 +87,7 @@ class ChatBox extends ImmutablePureComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
handleContentChange = (e) => {
|
handleContentChange = (e) => {
|
||||||
this.setState({ content: e.target.value });
|
this.setParams({ content: e.target.value });
|
||||||
}
|
}
|
||||||
|
|
||||||
markRead = () => {
|
markRead = () => {
|
||||||
|
@ -99,12 +120,13 @@ class ChatBox extends ImmutablePureComponent {
|
||||||
const data = new FormData();
|
const data = new FormData();
|
||||||
data.append('file', files[0]);
|
data.append('file', files[0]);
|
||||||
this.props.dispatch(uploadMedia(data)).then(response => {
|
this.props.dispatch(uploadMedia(data)).then(response => {
|
||||||
this.setState({ media_id: response.data.id });
|
this.setParams({ media_id: response.data.id });
|
||||||
}).catch(() => {});
|
}).catch(() => {});
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { chatMessageIds, chatId, intl } = this.props;
|
const { chatMessageIds, chatId, intl } = this.props;
|
||||||
|
const { params } = this.state;
|
||||||
if (!chatMessageIds) return null;
|
if (!chatMessageIds) return null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -117,7 +139,7 @@ class ChatBox extends ImmutablePureComponent {
|
||||||
placeholder={intl.formatMessage(messages.placeholder)}
|
placeholder={intl.formatMessage(messages.placeholder)}
|
||||||
onKeyDown={this.handleKeyDown}
|
onKeyDown={this.handleKeyDown}
|
||||||
onChange={this.handleContentChange}
|
onChange={this.handleContentChange}
|
||||||
value={this.state.content}
|
value={params.get('content', '')}
|
||||||
ref={this.setInputRef}
|
ref={this.setInputRef}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue