You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reducer.js 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // @flow
  2. import { ReducerRegistry } from '../base/redux';
  3. import { ADD_MESSAGE, TOGGLE_CHAT } from './actionTypes';
  4. const DEFAULT_STATE = {
  5. isOpen: false,
  6. lastReadMessage: undefined,
  7. messages: []
  8. };
  9. ReducerRegistry.register('features/chat', (state = DEFAULT_STATE, action) => {
  10. switch (action.type) {
  11. case ADD_MESSAGE: {
  12. const newMessage = {
  13. displayName: action.displayName,
  14. error: action.error,
  15. id: action.id,
  16. messageType: action.messageType,
  17. message: action.message,
  18. timestamp: action.timestamp
  19. };
  20. return {
  21. ...state,
  22. lastReadMessage:
  23. action.hasRead ? newMessage : state.lastReadMessage,
  24. messages: [
  25. ...state.messages,
  26. newMessage
  27. ]
  28. };
  29. }
  30. case TOGGLE_CHAT:
  31. return {
  32. ...state,
  33. isOpen: !state.isOpen,
  34. lastReadMessage: state.messages[state.messages.length - 1]
  35. };
  36. }
  37. return state;
  38. });