Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

reducer.js 3.1KB

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