Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

AbstractPollAnswer.tsx 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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, setVoteChanging } from '../actions';
  10. import { COMMAND_ANSWER_POLL } from '../constants';
  11. import { IPoll } from '../types';
  12. /**
  13. * The type of the React {@code Component} props of inheriting component.
  14. */
  15. type InputProps = {
  16. pollId: string;
  17. };
  18. /*
  19. * Props that will be passed by the AbstractPollAnswer to its
  20. * concrete implementations (web/native).
  21. **/
  22. export type AbstractProps = {
  23. checkBoxStates: boolean[];
  24. creatorName: string;
  25. poll: IPoll;
  26. setCheckbox: Function;
  27. skipAnswer: () => void;
  28. skipChangeVote: () => void;
  29. submitAnswer: () => void;
  30. t: Function;
  31. };
  32. /**
  33. * Higher Order Component taking in a concrete PollAnswer component and
  34. * augmenting it with state/behavior common to both web and native implementations.
  35. *
  36. * @param {React.AbstractComponent} Component - The concrete component.
  37. * @returns {React.AbstractComponent}
  38. */
  39. const AbstractPollAnswer = (Component: ComponentType<AbstractProps>) => (props: InputProps) => {
  40. const { pollId } = props;
  41. const conference: any = useSelector((state: IReduxState) => state['features/base/conference'].conference);
  42. const poll: IPoll = useSelector((state: IReduxState) => state['features/polls'].polls[pollId]);
  43. const [ checkBoxStates, setCheckBoxState ] = useState(() => {
  44. if (poll.lastVote !== null) {
  45. return [ ...poll.lastVote ];
  46. }
  47. return new Array(poll.answers.length).fill(false);
  48. });
  49. const participantName = useBoundSelector(getParticipantDisplayName, poll.senderId);
  50. const setCheckbox = useCallback((index, state) => {
  51. const newCheckBoxStates = [ ...checkBoxStates ];
  52. newCheckBoxStates[index] = state;
  53. setCheckBoxState(newCheckBoxStates);
  54. sendAnalytics(createPollEvent('vote.checked'));
  55. }, [ checkBoxStates ]);
  56. const dispatch = useDispatch();
  57. const submitAnswer = useCallback(() => {
  58. conference.sendMessage({
  59. type: COMMAND_ANSWER_POLL,
  60. pollId,
  61. answers: checkBoxStates
  62. });
  63. sendAnalytics(createPollEvent('vote.sent'));
  64. dispatch(registerVote(pollId, checkBoxStates));
  65. return false;
  66. }, [ pollId, checkBoxStates, conference ]);
  67. const skipAnswer = useCallback(() => {
  68. dispatch(registerVote(pollId, null));
  69. sendAnalytics(createPollEvent('vote.skipped'));
  70. }, [ pollId ]);
  71. const skipChangeVote = useCallback(() => {
  72. dispatch(setVoteChanging(pollId, false));
  73. }, [ dispatch, pollId ]);
  74. const { t } = useTranslation();
  75. return (<Component
  76. checkBoxStates = { checkBoxStates }
  77. creatorName = { participantName }
  78. poll = { poll }
  79. setCheckbox = { setCheckbox }
  80. skipAnswer = { skipAnswer }
  81. skipChangeVote = { skipChangeVote }
  82. submitAnswer = { submitAnswer }
  83. t = { t } />);
  84. };
  85. export default AbstractPollAnswer;