您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AbstractPollAnswer.js 2.9KB

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