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
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
lastReadMessage:
|
|
|
|
action.hasRead ? newMessage : state.lastReadMessage,
|
|
|
|
messages: [
|
|
|
|
...state.messages,
|
|
|
|
newMessage
|
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
lastReadMessage: state.messages[state.messages.length - 1]
|
2018-03-07 00:28:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
});
|