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.6KB

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