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.tsx 4.3KB

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