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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 } 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. submitAnswer: Function,
  27. t: Function,
  28. };
  29. /**
  30. * Higher Order Component taking in a concrete PollAnswer component and
  31. * augmenting it with state/behavior common to both web and native implementations.
  32. *
  33. * @param {React.AbstractComponent} Component - The concrete component.
  34. * @returns {React.AbstractComponent}
  35. */
  36. const AbstractPollAnswer = (Component: AbstractComponent<AbstractProps>) => (props: InputProps) => {
  37. const { pollId } = props;
  38. const conference: Object = useSelector(state => state['features/base/conference'].conference);
  39. const poll: Poll = useSelector(state => state['features/polls'].polls[pollId]);
  40. const { id: localId } = useSelector(getLocalParticipant);
  41. const [ checkBoxStates, setCheckBoxState ] = useState(() => {
  42. if (poll.lastVote !== null) {
  43. return [ ...poll.lastVote ];
  44. }
  45. return new Array(poll.answers.length).fill(false);
  46. });
  47. const setCheckbox = useCallback((index, state) => {
  48. const newCheckBoxStates = [ ...checkBoxStates ];
  49. newCheckBoxStates[index] = state;
  50. setCheckBoxState(newCheckBoxStates);
  51. sendAnalytics(createPollEvent('vote.checked'));
  52. }, [ checkBoxStates ]);
  53. const dispatch = useDispatch();
  54. const localParticipant = useSelector(state => getParticipantById(state, localId));
  55. const localName: string = localParticipant.name ? localParticipant.name : 'Fellow Jitster';
  56. const submitAnswer = useCallback(() => {
  57. conference.sendMessage({
  58. type: COMMAND_ANSWER_POLL,
  59. pollId,
  60. voterId: localId,
  61. voterName: localName,
  62. answers: checkBoxStates
  63. });
  64. sendAnalytics(createPollEvent('vote.sent'));
  65. dispatch(registerVote(pollId, checkBoxStates));
  66. return false;
  67. }, [ pollId, localId, localName, checkBoxStates, conference ]);
  68. const skipAnswer = useCallback(() => {
  69. dispatch(registerVote(pollId, null));
  70. sendAnalytics(createPollEvent('vote.skipped'));
  71. }, [ pollId ]);
  72. const { t } = useTranslation();
  73. return (<Component
  74. checkBoxStates = { checkBoxStates }
  75. poll = { poll }
  76. setCheckbox = { setCheckbox }
  77. skipAnswer = { skipAnswer }
  78. submitAnswer = { submitAnswer }
  79. t = { t } />);
  80. };
  81. export default AbstractPollAnswer;