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.

AbstractPollAnswer.tsx 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import React, { ComponentType, useCallback, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { useDispatch, useSelector } from 'react-redux';
  4. import { createPollEvent } from '../../analytics/AnalyticsEvents';
  5. import { sendAnalytics } from '../../analytics/functions';
  6. import { IReduxState } from '../../app/types';
  7. import { getParticipantDisplayName } from '../../base/participants/functions';
  8. import { useBoundSelector } from '../../base/util/hooks';
  9. import { registerVote, removePoll, setVoteChanging } from '../actions';
  10. import { COMMAND_ANSWER_POLL, COMMAND_NEW_POLL } from '../constants';
  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. pollId: string;
  18. setCreateMode: (mode: boolean) => void;
  19. };
  20. /*
  21. * Props that will be passed by the AbstractPollAnswer to its
  22. * concrete implementations (web/native).
  23. **/
  24. export type AbstractProps = {
  25. checkBoxStates: boolean[];
  26. creatorName: string;
  27. poll: IPoll;
  28. pollId: string;
  29. sendPoll: () => void;
  30. setCheckbox: Function;
  31. setCreateMode: (mode: boolean) => void;
  32. skipAnswer: () => void;
  33. skipChangeVote: () => void;
  34. submitAnswer: () => void;
  35. t: Function;
  36. };
  37. /**
  38. * Higher Order Component taking in a concrete PollAnswer 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 AbstractPollAnswer = (Component: ComponentType<AbstractProps>) => (props: InputProps) => {
  45. const { pollId, setCreateMode } = props;
  46. const { conference } = useSelector((state: IReduxState) => state['features/base/conference']);
  47. const poll: IPoll = useSelector(getPoll(pollId));
  48. const { answers, lastVote, question, senderId } = poll;
  49. const [ checkBoxStates, setCheckBoxState ] = useState(() => {
  50. if (lastVote !== null) {
  51. return [ ...lastVote ];
  52. }
  53. return new Array(answers.length).fill(false);
  54. });
  55. const participantName = useBoundSelector(getParticipantDisplayName, senderId);
  56. const setCheckbox = useCallback((index, state) => {
  57. const newCheckBoxStates = [ ...checkBoxStates ];
  58. newCheckBoxStates[index] = state;
  59. setCheckBoxState(newCheckBoxStates);
  60. sendAnalytics(createPollEvent('vote.checked'));
  61. }, [ checkBoxStates ]);
  62. const dispatch = useDispatch();
  63. const submitAnswer = useCallback(() => {
  64. conference?.sendMessage({
  65. type: COMMAND_ANSWER_POLL,
  66. pollId,
  67. answers: checkBoxStates
  68. });
  69. sendAnalytics(createPollEvent('vote.sent'));
  70. dispatch(registerVote(pollId, checkBoxStates));
  71. return false;
  72. }, [ pollId, checkBoxStates, conference ]);
  73. const sendPoll = useCallback(() => {
  74. conference?.sendMessage({
  75. type: COMMAND_NEW_POLL,
  76. pollId,
  77. question,
  78. answers: answers.map(answer => answer.name)
  79. });
  80. dispatch(removePoll(pollId, poll));
  81. }, [ conference, question, answers ]);
  82. const skipAnswer = useCallback(() => {
  83. dispatch(registerVote(pollId, null));
  84. sendAnalytics(createPollEvent('vote.skipped'));
  85. }, [ pollId ]);
  86. const skipChangeVote = useCallback(() => {
  87. dispatch(setVoteChanging(pollId, false));
  88. }, [ dispatch, pollId ]);
  89. const { t } = useTranslation();
  90. return (<Component
  91. checkBoxStates = { checkBoxStates }
  92. creatorName = { participantName }
  93. poll = { poll }
  94. pollId = { pollId }
  95. sendPoll = { sendPoll }
  96. setCheckbox = { setCheckbox }
  97. setCreateMode = { setCreateMode }
  98. skipAnswer = { skipAnswer }
  99. skipChangeVote = { skipChangeVote }
  100. submitAnswer = { submitAnswer }
  101. t = { t } />);
  102. };
  103. export default AbstractPollAnswer;