Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AbstractPollAnswer.tsx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* eslint-disable lines-around-comment */
  2. import React, { ComponentType, useCallback, useState } from 'react';
  3. import { useTranslation } from 'react-i18next';
  4. import { useDispatch, useSelector } from 'react-redux';
  5. // @ts-ignore
  6. import { sendAnalytics, createPollEvent } from '../../analytics';
  7. import { IState } from '../../app/types';
  8. import { getLocalParticipant, getParticipantById } from '../../base/participants/functions';
  9. import { useBoundSelector } from '../../base/util/hooks';
  10. // @ts-ignore
  11. import { registerVote, setVoteChanging } from '../actions';
  12. import { COMMAND_ANSWER_POLL } from '../constants';
  13. import { Poll } from '../types';
  14. /**
  15. * The type of the React {@code Component} props of inheriting component.
  16. */
  17. type InputProps = {
  18. pollId: string,
  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: Poll,
  28. setCheckbox: Function,
  29. skipAnswer: () => void,
  30. skipChangeVote: () => void,
  31. submitAnswer: () => void,
  32. t: Function,
  33. };
  34. /**
  35. * Higher Order Component taking in a concrete PollAnswer component and
  36. * augmenting it with state/behavior common to both web and native implementations.
  37. *
  38. * @param {React.AbstractComponent} Component - The concrete component.
  39. * @returns {React.AbstractComponent}
  40. */
  41. const AbstractPollAnswer = (Component: ComponentType<AbstractProps>) => (props: InputProps) => {
  42. const { pollId } = props;
  43. const conference: any = useSelector((state: IState) => state['features/base/conference'].conference);
  44. const poll: Poll = useSelector((state: any) => state['features/polls'].polls[pollId]);
  45. const { id: localId } = useSelector(getLocalParticipant);
  46. const [ checkBoxStates, setCheckBoxState ] = useState(() => {
  47. if (poll.lastVote !== null) {
  48. return [ ...poll.lastVote ];
  49. }
  50. return new Array(poll.answers.length).fill(false);
  51. });
  52. const participant = useBoundSelector(getParticipantById, poll.senderId);
  53. const setCheckbox = useCallback((index, state) => {
  54. const newCheckBoxStates = [ ...checkBoxStates ];
  55. newCheckBoxStates[index] = state;
  56. setCheckBoxState(newCheckBoxStates);
  57. sendAnalytics(createPollEvent('vote.checked'));
  58. }, [ checkBoxStates ]);
  59. const dispatch = useDispatch();
  60. const localParticipant = useBoundSelector(getParticipantById, localId);
  61. const localName: string = localParticipant.name ? localParticipant.name : 'Fellow Jitster';
  62. const submitAnswer = useCallback(() => {
  63. conference.sendMessage({
  64. type: COMMAND_ANSWER_POLL,
  65. pollId,
  66. voterId: localId,
  67. voterName: localName,
  68. answers: checkBoxStates
  69. });
  70. sendAnalytics(createPollEvent('vote.sent'));
  71. dispatch(registerVote(pollId, checkBoxStates));
  72. return false;
  73. }, [ pollId, localId, localName, checkBoxStates, conference ]);
  74. const skipAnswer = useCallback(() => {
  75. dispatch(registerVote(pollId, null));
  76. sendAnalytics(createPollEvent('vote.skipped'));
  77. }, [ pollId ]);
  78. const skipChangeVote = useCallback(() => {
  79. dispatch(setVoteChanging(pollId, false));
  80. }, [ dispatch, pollId ]);
  81. const { t } = useTranslation();
  82. return (<Component
  83. checkBoxStates = { checkBoxStates }
  84. creatorName = { participant ? participant.name : '' }
  85. poll = { poll }
  86. setCheckbox = { setCheckbox }
  87. skipAnswer = { skipAnswer }
  88. skipChangeVote = { skipChangeVote }
  89. submitAnswer = { submitAnswer }
  90. t = { t } />);
  91. };
  92. export default AbstractPollAnswer;