2018-03-07 00:28:19 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import { ReducerRegistry } from '../base/redux';
|
|
|
|
|
2019-01-01 21:19:34 +00:00
|
|
|
import { ADD_MESSAGE, CLEAR_MESSAGES, TOGGLE_CHAT } from './actionTypes';
|
2018-03-07 00:28:19 +00:00
|
|
|
|
|
|
|
const DEFAULT_STATE = {
|
2018-08-29 17:24:25 +00:00
|
|
|
isOpen: false,
|
|
|
|
lastReadMessage: undefined,
|
|
|
|
messages: []
|
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,
|
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: []
|
|
|
|
};
|
|
|
|
|
2018-08-29 17:24:25 +00:00
|
|
|
case TOGGLE_CHAT:
|
2018-03-07 00:28:19 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2018-08-29 17:24:25 +00:00
|
|
|
isOpen: !state.isOpen,
|
2019-04-25 13:17:49 +00:00
|
|
|
lastReadMessage: state.messages[
|
|
|
|
navigator.product === 'ReactNative' ? 0 : state.messages.length - 1]
|
2018-03-07 00:28:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
});
|