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.

AbstractPollCreate.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 { getParticipantDisplayName } from '../../base/participants';
  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. const newAnswers = [ ...answers ];
  43. newAnswers[i] = answer;
  44. setAnswers(newAnswers);
  45. });
  46. const addAnswer = useCallback((i: ?number) => {
  47. const newAnswers = [ ...answers ];
  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. newAnswers.splice(i, 1);
  55. newAnswers.splice(j, 0, answer);
  56. setAnswers(newAnswers);
  57. });
  58. const removeAnswer = useCallback(i => {
  59. if (answers.length <= 2) {
  60. return;
  61. }
  62. const newAnswers = [ ...answers ];
  63. newAnswers.splice(i, 1);
  64. setAnswers(newAnswers);
  65. });
  66. const conference = useSelector(state => state['features/base/conference'].conference);
  67. const myId = conference.myUserId();
  68. const myName = useSelector(state => getParticipantDisplayName(state, myId));
  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. senderId: myId,
  81. senderName: myName,
  82. question,
  83. answers: filteredAnswers
  84. });
  85. setCreateMode(false);
  86. }, [ conference, question, answers ]);
  87. // Check if the poll create form can be submitted i.e. if the send button should be disabled.
  88. const isSubmitDisabled
  89. = question.trim().length <= 0 // If no question is provided
  90. || answers.filter(answer => answer.trim().length > 0).length < 2; // If not enough options are provided
  91. const { t } = useTranslation();
  92. return (<Component
  93. addAnswer = { addAnswer }
  94. answers = { answers }
  95. isSubmitDisabled = { isSubmitDisabled }
  96. moveAnswer = { moveAnswer }
  97. onSubmit = { onSubmit }
  98. question = { question }
  99. removeAnswer = { removeAnswer }
  100. setAnswer = { setAnswer }
  101. setCreateMode = { setCreateMode }
  102. setQuestion = { setQuestion }
  103. t = { t } />);
  104. };
  105. export default AbstractPollCreate;