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.

actions.js 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { ADD_MESSAGE, SET_LAST_READ_MESSAGE } from './actionTypes';
  2. /* eslint-disable max-params */
  3. /**
  4. * Adds a chat message to the collection of messages.
  5. *
  6. * @param {string} userName - The username to display of the participant that
  7. * authored the message.
  8. * @param {string} message - The received message to display.
  9. * @param {string} timestamp - A timestamp to display for when the message was
  10. * received.
  11. * @param {boolean} hasRead - Whether or not to immediately mark the message as
  12. * read.
  13. * @returns {{
  14. * type: ADD_MESSAGE,
  15. * hasRead: boolean,
  16. * message: string,
  17. * timestamp: string,
  18. * userName: string
  19. * }}
  20. */
  21. export function addMessage(userName, message, timestamp, hasRead) {
  22. return {
  23. type: ADD_MESSAGE,
  24. hasRead,
  25. message,
  26. timestamp,
  27. userName
  28. };
  29. }
  30. /* eslint-enable max-params */
  31. /**
  32. * Sets the last read message cursor to the latest message.
  33. *
  34. * @returns {Function}
  35. */
  36. export function markAllRead() {
  37. return (dispatch, getState) => {
  38. const { messages } = getState()['features/chat'];
  39. dispatch(setLastReadMessage(messages[messages.length - 1]));
  40. };
  41. }
  42. /**
  43. * Updates the last read message cursor to be set at the passed in message. The
  44. * assumption is that messages will be ordered chronologically.
  45. *
  46. * @param {Object} message - The message from the redux state.
  47. * @returns {{
  48. * type: SET_LAST_READ_MESSAGE,
  49. * message: Object
  50. * }}
  51. */
  52. export function setLastReadMessage(message) {
  53. return {
  54. type: SET_LAST_READ_MESSAGE,
  55. message
  56. };
  57. }