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.js 3.4KB

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