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

AbstractPollCreate.tsx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import React, { ComponentType, FormEvent, useCallback, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { useSelector } from 'react-redux';
  4. import { createPollEvent } from '../../analytics/AnalyticsEvents';
  5. import { sendAnalytics } from '../../analytics/functions';
  6. import { IReduxState } from '../../app/types';
  7. import { COMMAND_NEW_POLL } from '../constants';
  8. /**
  9. * The type of the React {@code Component} props of inheriting component.
  10. */
  11. type InputProps = {
  12. setCreateMode: (mode: boolean) => void;
  13. };
  14. /*
  15. * Props that will be passed by the AbstractPollCreate to its
  16. * concrete implementations (web/native).
  17. **/
  18. export type AbstractProps = InputProps & {
  19. addAnswer: (index?: number) => void;
  20. answers: Array<string>;
  21. isSubmitDisabled: boolean;
  22. onSubmit: (event?: FormEvent<HTMLFormElement>) => void;
  23. question: string;
  24. removeAnswer: (index: number) => void;
  25. setAnswer: (index: number, value: string) => void;
  26. setQuestion: (question: string) => void;
  27. t: Function;
  28. };
  29. /**
  30. * Higher Order Component taking in a concrete PollCreate 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 AbstractPollCreate = (Component: ComponentType<AbstractProps>) => (props: InputProps) => {
  37. const { setCreateMode } = props;
  38. const [ question, setQuestion ] = useState('');
  39. const [ answers, setAnswers ] = useState([ '', '' ]);
  40. const setAnswer = useCallback((i, answer) => {
  41. answers[i] = answer;
  42. setAnswers([ ...answers ]);
  43. }, [ answers ]);
  44. const addAnswer = useCallback((i?: number) => {
  45. const newAnswers = [ ...answers ];
  46. sendAnalytics(createPollEvent('option.added'));
  47. newAnswers.splice(typeof i === 'number' ? i : answers.length, 0, '');
  48. setAnswers(newAnswers);
  49. }, [ answers ]);
  50. const removeAnswer = useCallback(i => {
  51. if (answers.length <= 2) {
  52. return;
  53. }
  54. const newAnswers = [ ...answers ];
  55. sendAnalytics(createPollEvent('option.removed'));
  56. newAnswers.splice(i, 1);
  57. setAnswers(newAnswers);
  58. }, [ answers ]);
  59. const conference = useSelector((state: IReduxState) => state['features/base/conference'].conference);
  60. const onSubmit = useCallback(ev => {
  61. if (ev) {
  62. ev.preventDefault();
  63. }
  64. const filteredAnswers = answers.filter(answer => answer.trim().length > 0);
  65. if (filteredAnswers.length < 2) {
  66. return;
  67. }
  68. conference?.sendMessage({
  69. type: COMMAND_NEW_POLL,
  70. pollId: Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(36),
  71. question,
  72. answers: filteredAnswers
  73. });
  74. sendAnalytics(createPollEvent('created'));
  75. setCreateMode(false);
  76. }, [ conference, question, answers ]);
  77. // Check if the poll create form can be submitted i.e. if the send button should be disabled.
  78. const isSubmitDisabled
  79. = question.trim().length <= 0 // If no question is provided
  80. || answers.filter(answer => answer.trim().length > 0).length < 2; // If not enough options are provided
  81. const { t } = useTranslation();
  82. return (<Component
  83. addAnswer = { addAnswer }
  84. answers = { answers }
  85. isSubmitDisabled = { isSubmitDisabled }
  86. onSubmit = { onSubmit }
  87. question = { question }
  88. removeAnswer = { removeAnswer }
  89. setAnswer = { setAnswer }
  90. setCreateMode = { setCreateMode }
  91. setQuestion = { setQuestion }
  92. t = { t } />);
  93. };
  94. export default AbstractPollCreate;