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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 { setVoteChanging } from '../actions';
  8. import { getPoll } from '../functions';
  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(getPoll(pollId));
  46. const [ showDetails, setShowDetails ] = useState(false);
  47. const toggleIsDetailed = useCallback(() => {
  48. sendAnalytics(createPollEvent('vote.detailsViewed'));
  49. setShowDetails(!showDetails);
  50. });
  51. const answers: Array<AnswerInfo> = useMemo(() => {
  52. const voterSet = new Set();
  53. // Getting every voters ID that participates to the poll
  54. for (const answer of pollDetails.answers) {
  55. for (const [ voterId ] of answer.voters) {
  56. voterSet.add(voterId);
  57. }
  58. }
  59. const totalVoters = voterSet.size;
  60. return pollDetails.answers.map(answer => {
  61. const percentage = totalVoters === 0 ? 0 : Math.round(answer.voters.size / totalVoters * 100);
  62. let voters = null;
  63. if (showDetails) {
  64. voters = [ ...answer.voters ].map(([ id, name ]) => {
  65. return {
  66. id,
  67. name
  68. };
  69. });
  70. }
  71. return {
  72. name: answer.name,
  73. percentage,
  74. voters,
  75. voterCount: answer.voters.size
  76. };
  77. });
  78. }, [ pollDetails.answers, showDetails ]);
  79. const dispatch = useDispatch();
  80. const changeVote = useCallback(() => {
  81. dispatch(setVoteChanging(pollId, true));
  82. sendAnalytics(createPollEvent('vote.changed'));
  83. }, [ dispatch, pollId ]);
  84. const { t } = useTranslation();
  85. return (
  86. <Component
  87. answers = { answers }
  88. changeVote = { changeVote }
  89. haveVoted = { pollDetails.lastVote !== null }
  90. question = { pollDetails.question }
  91. showDetails = { showDetails }
  92. t = { t }
  93. toggleIsDetailed = { toggleIsDetailed } />
  94. );
  95. };
  96. export default AbstractPollResults;