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

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