Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AbstractPollCreate.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // @flow
  2. import React, { useCallback, useState } from 'react';
  3. import type { AbstractComponent } from 'react';
  4. import { useTranslation } from 'react-i18next';
  5. import { useSelector } from 'react-redux';
  6. import { createPollEvent, sendAnalytics } from '../../analytics';
  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: 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. answers: Array<string>,
  20. question: string,
  21. setQuestion: string => void,
  22. setAnswer: (number, string) => void,
  23. addAnswer: ?number => void,
  24. moveAnswer: (number, number) => void,
  25. removeAnswer: number => void,
  26. onSubmit: Function,
  27. isSubmitDisabled: boolean,
  28. t: Function,
  29. };
  30. /**
  31. * Higher Order Component taking in a concrete PollCreate 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 AbstractPollCreate = (Component: AbstractComponent<AbstractProps>) => (props: InputProps) => {
  38. const { setCreateMode } = props;
  39. const [ question, setQuestion ] = useState('');
  40. const [ answers, setAnswers ] = useState([ '', '' ]);
  41. const setAnswer = useCallback((i, answer) => {
  42. answers[i] = answer;
  43. setAnswers([ ...answers ]);
  44. });
  45. const addAnswer = useCallback((i: ?number) => {
  46. const newAnswers = [ ...answers ];
  47. sendAnalytics(createPollEvent('option.added'));
  48. newAnswers.splice(typeof i === 'number' ? i : answers.length, 0, '');
  49. setAnswers(newAnswers);
  50. });
  51. const moveAnswer = useCallback((i, j) => {
  52. const newAnswers = [ ...answers ];
  53. const answer = answers[i];
  54. sendAnalytics(createPollEvent('option.moved'));
  55. newAnswers.splice(i, 1);
  56. newAnswers.splice(j, 0, answer);
  57. setAnswers(newAnswers);
  58. });
  59. const removeAnswer = useCallback(i => {
  60. if (answers.length <= 2) {
  61. return;
  62. }
  63. const newAnswers = [ ...answers ];
  64. sendAnalytics(createPollEvent('option.removed'));
  65. newAnswers.splice(i, 1);
  66. setAnswers(newAnswers);
  67. });
  68. const conference = useSelector(state => state['features/base/conference'].conference);
  69. const onSubmit = useCallback(ev => {
  70. if (ev) {
  71. ev.preventDefault();
  72. }
  73. const filteredAnswers = answers.filter(answer => answer.trim().length > 0);
  74. if (filteredAnswers.length < 2) {
  75. return;
  76. }
  77. conference.sendMessage({
  78. type: COMMAND_NEW_POLL,
  79. pollId: Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(36),
  80. question,
  81. answers: filteredAnswers
  82. });
  83. sendAnalytics(createPollEvent('created'));
  84. setCreateMode(false);
  85. }, [ conference, question, answers ]);
  86. // Check if the poll create form can be submitted i.e. if the send button should be disabled.
  87. const isSubmitDisabled
  88. = question.trim().length <= 0 // If no question is provided
  89. || answers.filter(answer => answer.trim().length > 0).length < 2; // If not enough options are provided
  90. const { t } = useTranslation();
  91. return (<Component
  92. addAnswer = { addAnswer }
  93. answers = { answers }
  94. isSubmitDisabled = { isSubmitDisabled }
  95. moveAnswer = { moveAnswer }
  96. onSubmit = { onSubmit }
  97. question = { question }
  98. removeAnswer = { removeAnswer }
  99. setAnswer = { setAnswer }
  100. setCreateMode = { setCreateMode }
  101. setQuestion = { setQuestion }
  102. t = { t } />);
  103. };
  104. export default AbstractPollCreate;