Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

reducer.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // @flow
  2. import { ReducerRegistry } from '../base/redux';
  3. import {
  4. CHANGE_VOTE,
  5. RECEIVE_POLL,
  6. RECEIVE_ANSWER,
  7. REGISTER_VOTE,
  8. RETRACT_VOTE,
  9. RESET_NB_UNREAD_POLLS
  10. } from './actionTypes';
  11. import type { Answer } from './types';
  12. const INITIAL_STATE = {
  13. polls: {},
  14. // Number of not read message
  15. nbUnreadPolls: 0
  16. };
  17. ReducerRegistry.register('features/polls', (state = INITIAL_STATE, action) => {
  18. switch (action.type) {
  19. case CHANGE_VOTE: {
  20. const { pollId, value } = action;
  21. return {
  22. ...state,
  23. polls: {
  24. ...state.polls,
  25. [pollId]: {
  26. ...state.polls[pollId],
  27. changingVote: value,
  28. showResults: !value
  29. }
  30. }
  31. };
  32. }
  33. // Reducer triggered when a poll is received
  34. case RECEIVE_POLL: {
  35. const newState = {
  36. ...state,
  37. polls: {
  38. ...state.polls,
  39. // The poll is added to the dictionnary of received polls
  40. [action.pollId]: action.poll
  41. },
  42. nbUnreadPolls: state.nbUnreadPolls + 1
  43. };
  44. return newState;
  45. }
  46. // Reducer triggered when an answer is received
  47. // The answer is added to an existing poll
  48. case RECEIVE_ANSWER: {
  49. const { pollId, answer }: { pollId: string; answer: Answer } = action;
  50. // if the poll doesn't exist
  51. if (!(pollId in state.polls)) {
  52. console.warn('requested poll does not exist: pollId ', pollId);
  53. return state;
  54. }
  55. // if the poll exists, we update it with the incoming answer
  56. const newAnswers = state.polls[pollId].answers
  57. .map(_answer => {
  58. return {
  59. name: _answer.name,
  60. voters: new Map(_answer.voters)
  61. };
  62. });
  63. for (let i = 0; i < newAnswers.length; i++) {
  64. // if the answer was chosen, we add the sender to the set of voters of this answer
  65. const voters = newAnswers[i].voters;
  66. if (answer.answers[i]) {
  67. voters.set(answer.voterId, answer.voterName);
  68. } else {
  69. voters.delete(answer.voterId);
  70. }
  71. }
  72. // finally we update the state by returning the updated poll
  73. return {
  74. ...state,
  75. polls: {
  76. ...state.polls,
  77. [pollId]: {
  78. ...state.polls[pollId],
  79. answers: newAnswers
  80. }
  81. }
  82. };
  83. }
  84. case REGISTER_VOTE: {
  85. const { answers, pollId }: { answers: Array<boolean> | null; pollId: string } = action;
  86. return {
  87. ...state,
  88. polls: {
  89. ...state.polls,
  90. [pollId]: {
  91. ...state.polls[pollId],
  92. changingVote: false,
  93. lastVote: answers,
  94. showResults: true
  95. }
  96. }
  97. };
  98. }
  99. case RETRACT_VOTE: {
  100. const { pollId }: { pollId: string } = action;
  101. return {
  102. ...state,
  103. polls: {
  104. ...state.polls,
  105. [pollId]: {
  106. ...state.polls[pollId],
  107. showResults: false
  108. }
  109. }
  110. };
  111. }
  112. case RESET_NB_UNREAD_POLLS: {
  113. return {
  114. ...state,
  115. nbUnreadPolls: 0
  116. };
  117. }
  118. default:
  119. return state;
  120. }
  121. });