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 4.0KB

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