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.

AbstractPollResults.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // @flow
  2. import React, { useCallback, useMemo, useState } from 'react';
  3. import type { AbstractComponent } from 'react';
  4. import { useTranslation } from 'react-i18next';
  5. import { useDispatch, useSelector } from 'react-redux';
  6. import { getLocalParticipant, getParticipantById } from '../../base/participants/functions';
  7. import { retractVote } from '../actions';
  8. import { COMMAND_ANSWER_POLL } from '../constants';
  9. /**
  10. * The type of the React {@code Component} props of inheriting component.
  11. */
  12. type InputProps = {
  13. /**
  14. * ID of the poll to display
  15. */
  16. pollId: string,
  17. };
  18. export type AnswerInfo = {
  19. name: string,
  20. percentage: number,
  21. voters?: Array<{ id: number, name: string }>,
  22. voterCount: number
  23. };
  24. /**
  25. * The type of the React {@code Component} props of {@link AbstractPollResults}.
  26. */
  27. export type AbstractProps = {
  28. answers: Array<AnswerInfo>,
  29. changeVote: Function,
  30. showDetails: boolean,
  31. question: string,
  32. t: Function,
  33. toggleIsDetailed: Function,
  34. haveVoted: boolean,
  35. };
  36. /**
  37. * Higher Order Component taking in a concrete PollResult component and
  38. * augmenting it with state/behavior common to both web and native implementations.
  39. *
  40. * @param {React.AbstractComponent} Component - The concrete component.
  41. * @returns {React.AbstractComponent}
  42. */
  43. const AbstractPollResults = (Component: AbstractComponent<AbstractProps>) => (props: InputProps) => {
  44. const { pollId } = props;
  45. const pollDetails = useSelector(state => state['features/polls'].polls[pollId]);
  46. const [ showDetails, setShowDetails ] = useState(false);
  47. const toggleIsDetailed = useCallback(() => {
  48. setShowDetails(!showDetails);
  49. });
  50. const answers: Array<AnswerInfo> = useMemo(() => {
  51. const voterSet = new Set();
  52. // Getting every voters ID that participates to the poll
  53. for (const answer of pollDetails.answers) {
  54. for (const [ voterId ] of answer.voters) {
  55. voterSet.add(voterId);
  56. }
  57. }
  58. const totalVoters = voterSet.size;
  59. return pollDetails.answers.map(answer => {
  60. const percentage = totalVoters === 0 ? 0 : Math.round(answer.voters.size / totalVoters * 100);
  61. let voters = null;
  62. if (showDetails) {
  63. voters = [ ...answer.voters ].map(([ id, name ]) => {
  64. return {
  65. id,
  66. name
  67. };
  68. });
  69. }
  70. return {
  71. name: answer.name,
  72. percentage,
  73. voters,
  74. voterCount: answer.voters.size
  75. };
  76. });
  77. }, [ pollDetails.answers, showDetails ]);
  78. const dispatch = useDispatch();
  79. const conference: Object = useSelector(state => state['features/base/conference'].conference);
  80. const localId = useSelector(state => getLocalParticipant(state).id);
  81. const localParticipant = useSelector(state => getParticipantById(state, localId));
  82. const localName: string = localParticipant ? localParticipant.name : 'Fellow Jitster';
  83. const changeVote = useCallback(() => {
  84. conference.sendMessage({
  85. type: COMMAND_ANSWER_POLL,
  86. pollId,
  87. voterId: localId,
  88. voterName: localName,
  89. answers: new Array(pollDetails.answers.length).fill(false)
  90. });
  91. dispatch(retractVote(pollId));
  92. }, [ pollId, localId, localName, pollDetails ]);
  93. const { t } = useTranslation();
  94. return (<Component
  95. answers = { answers }
  96. changeVote = { changeVote }
  97. haveVoted = { pollDetails.lastVote !== null }
  98. question = { pollDetails.question }
  99. showDetails = { showDetails }
  100. t = { t }
  101. toggleIsDetailed = { toggleIsDetailed } />);
  102. };
  103. export default AbstractPollResults;