Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AbstractPollAnswer.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // @flow
  2. import React, { useCallback, 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 { getLocalParticipant, getParticipantById } from '../../base/participants';
  8. import { useBoundSelector } from '../../base/util/hooks';
  9. import { registerVote, setVoteChanging } from '../actions';
  10. import { COMMAND_ANSWER_POLL } from '../constants';
  11. import type { Poll } 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: Function,
  24. creatorName: string,
  25. poll: Poll,
  26. setCheckbox: Function,
  27. skipAnswer: Function,
  28. skipChangeVote: Function,
  29. submitAnswer: Function,
  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: AbstractComponent<AbstractProps>) => (props: InputProps) => {
  40. const { pollId } = props;
  41. const conference: Object = useSelector(state => state['features/base/conference'].conference);
  42. const poll: Poll = useSelector(state => state['features/polls'].polls[pollId]);
  43. const { id: localId } = useSelector(getLocalParticipant);
  44. const [ checkBoxStates, setCheckBoxState ] = useState(() => {
  45. if (poll.lastVote !== null) {
  46. return [ ...poll.lastVote ];
  47. }
  48. return new Array(poll.answers.length).fill(false);
  49. });
  50. const participant = useBoundSelector(getParticipantById, poll.senderId);
  51. const setCheckbox = useCallback((index, state) => {
  52. const newCheckBoxStates = [ ...checkBoxStates ];
  53. newCheckBoxStates[index] = state;
  54. setCheckBoxState(newCheckBoxStates);
  55. sendAnalytics(createPollEvent('vote.checked'));
  56. }, [ checkBoxStates ]);
  57. const dispatch = useDispatch();
  58. const localParticipant = useBoundSelector(getParticipantById, localId);
  59. const localName: string = localParticipant.name ? localParticipant.name : 'Fellow Jitster';
  60. const submitAnswer = useCallback(() => {
  61. conference.sendMessage({
  62. type: COMMAND_ANSWER_POLL,
  63. pollId,
  64. voterId: localId,
  65. voterName: localName,
  66. answers: checkBoxStates
  67. });
  68. sendAnalytics(createPollEvent('vote.sent'));
  69. dispatch(registerVote(pollId, checkBoxStates));
  70. return false;
  71. }, [ pollId, localId, localName, checkBoxStates, conference ]);
  72. const skipAnswer = useCallback(() => {
  73. dispatch(registerVote(pollId, null));
  74. sendAnalytics(createPollEvent('vote.skipped'));
  75. }, [ pollId ]);
  76. const skipChangeVote = useCallback(() => {
  77. dispatch(setVoteChanging(pollId, false));
  78. }, [ dispatch, pollId ]);
  79. const { t } = useTranslation();
  80. return (<Component
  81. checkBoxStates = { checkBoxStates }
  82. creatorName = { participant ? participant.name : '' }
  83. poll = { poll }
  84. setCheckbox = { setCheckbox }
  85. skipAnswer = { skipAnswer }
  86. skipChangeVote = { skipChangeVote }
  87. submitAnswer = { submitAnswer }
  88. t = { t } />);
  89. };
  90. export default AbstractPollAnswer;