2018-03-07 00:28:19 +00:00
|
|
|
// @flow
|
|
|
|
|
2020-04-01 15:14:02 +00:00
|
|
|
import { SET_ACTIVE_MODAL_ID } from '../base/modal';
|
2018-03-07 00:28:19 +00:00
|
|
|
import { ReducerRegistry } from '../base/redux';
|
|
|
|
|
2019-10-07 12:35:04 +00:00
|
|
|
import {
|
|
|
|
ADD_MESSAGE,
|
|
|
|
CLEAR_MESSAGES,
|
|
|
|
SET_PRIVATE_MESSAGE_RECIPIENT,
|
|
|
|
TOGGLE_CHAT
|
|
|
|
} from './actionTypes';
|
2020-04-01 15:14:02 +00:00
|
|
|
import { CHAT_VIEW_MODAL_ID } from './constants';
|
2018-03-07 00:28:19 +00:00
|
|
|
|
|
|
|
const DEFAULT_STATE = {
|
2018-08-29 17:24:25 +00:00
|
|
|
isOpen: false,
|
|
|
|
lastReadMessage: undefined,
|
2019-10-07 12:35:04 +00:00
|
|
|
messages: [],
|
|
|
|
privateMessageRecipient: undefined
|
2018-03-07 00:28:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ReducerRegistry.register('features/chat', (state = DEFAULT_STATE, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case ADD_MESSAGE: {
|
|
|
|
const newMessage = {
|
2018-08-29 17:24:25 +00:00
|
|
|
displayName: action.displayName,
|
|
|
|
error: action.error,
|
|
|
|
id: action.id,
|
|
|
|
messageType: action.messageType,
|
2018-03-07 00:28:19 +00:00
|
|
|
message: action.message,
|
2019-10-07 12:35:04 +00:00
|
|
|
privateMessage: action.privateMessage,
|
|
|
|
recipient: action.recipient,
|
2018-08-29 17:24:25 +00:00
|
|
|
timestamp: action.timestamp
|
2018-03-07 00:28:19 +00:00
|
|
|
};
|
|
|
|
|
2019-04-25 13:17:49 +00:00
|
|
|
// React native, unlike web, needs a reverse sorted message list.
|
|
|
|
const messages = navigator.product === 'ReactNative'
|
|
|
|
? [
|
|
|
|
newMessage,
|
|
|
|
...state.messages
|
|
|
|
]
|
|
|
|
: [
|
|
|
|
...state.messages,
|
|
|
|
newMessage
|
|
|
|
];
|
|
|
|
|
2018-03-07 00:28:19 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
lastReadMessage:
|
|
|
|
action.hasRead ? newMessage : state.lastReadMessage,
|
2019-04-25 13:17:49 +00:00
|
|
|
messages
|
2018-03-07 00:28:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-01 21:19:34 +00:00
|
|
|
case CLEAR_MESSAGES:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
lastReadMessage: undefined,
|
|
|
|
messages: []
|
|
|
|
};
|
|
|
|
|
2020-04-01 15:14:02 +00:00
|
|
|
case SET_ACTIVE_MODAL_ID:
|
|
|
|
if (action.activeModalId === CHAT_VIEW_MODAL_ID) {
|
|
|
|
return updateChatState(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2019-10-07 12:35:04 +00:00
|
|
|
case SET_PRIVATE_MESSAGE_RECIPIENT:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isOpen: Boolean(action.participant) || state.isOpen,
|
|
|
|
privateMessageRecipient: action.participant
|
|
|
|
};
|
|
|
|
|
2018-08-29 17:24:25 +00:00
|
|
|
case TOGGLE_CHAT:
|
2020-04-01 15:14:02 +00:00
|
|
|
return updateChatState(state);
|
2018-03-07 00:28:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
});
|
2020-04-01 15:14:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the chat status on opening the chat view.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The Redux state of the feature.
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
function updateChatState(state) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isOpen: !state.isOpen,
|
|
|
|
lastReadMessage: state.messages[
|
|
|
|
navigator.product === 'ReactNative' ? 0 : state.messages.length - 1],
|
|
|
|
privateMessageRecipient: state.isOpen ? undefined : state.privateMessageRecipient
|
|
|
|
};
|
|
|
|
}
|