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.

PollAnswer.tsx 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* eslint-disable react/jsx-no-bind */
  2. import React from 'react';
  3. import { useDispatch } from 'react-redux';
  4. import { makeStyles } from 'tss-react/mui';
  5. import Icon from '../../../base/icons/components/Icon';
  6. import { IconCloseLarge } from '../../../base/icons/svg';
  7. import { withPixelLineHeight } from '../../../base/styles/functions.web';
  8. import Button from '../../../base/ui/components/web/Button';
  9. import Checkbox from '../../../base/ui/components/web/Checkbox';
  10. import { BUTTON_TYPES } from '../../../base/ui/constants.web';
  11. import { editPoll, removePoll } from '../../actions';
  12. import { isSubmitAnswerDisabled } from '../../functions';
  13. import AbstractPollAnswer, { AbstractProps } from '../AbstractPollAnswer';
  14. const useStyles = makeStyles()(theme => {
  15. return {
  16. container: {
  17. margin: '24px',
  18. padding: '16px',
  19. backgroundColor: theme.palette.ui02,
  20. borderRadius: '8px',
  21. wordBreak: 'break-word'
  22. },
  23. closeBtn: {
  24. cursor: 'pointer',
  25. float: 'right'
  26. },
  27. header: {
  28. marginBottom: '24px'
  29. },
  30. question: {
  31. ...withPixelLineHeight(theme.typography.heading6),
  32. color: theme.palette.text01,
  33. marginBottom: '8px'
  34. },
  35. creator: {
  36. ...withPixelLineHeight(theme.typography.bodyShortRegular),
  37. color: theme.palette.text02
  38. },
  39. answerList: {
  40. listStyleType: 'none',
  41. margin: 0,
  42. padding: 0,
  43. marginBottom: '24px'
  44. },
  45. answer: {
  46. display: 'flex',
  47. marginBottom: '16px'
  48. },
  49. footer: {
  50. display: 'flex',
  51. justifyContent: 'flex-end'
  52. },
  53. buttonMargin: {
  54. marginRight: theme.spacing(3)
  55. }
  56. };
  57. });
  58. const PollAnswer = ({
  59. creatorName,
  60. checkBoxStates,
  61. poll,
  62. pollId,
  63. setCheckbox,
  64. setCreateMode,
  65. skipAnswer,
  66. skipChangeVote,
  67. sendPoll,
  68. submitAnswer,
  69. t
  70. }: AbstractProps) => {
  71. const { changingVote, saved: pollSaved } = poll;
  72. const dispatch = useDispatch();
  73. const { classes } = useStyles();
  74. return (
  75. <div className = { classes.container }>
  76. {
  77. pollSaved && <Icon
  78. ariaLabel = { t('polls.closeButton') }
  79. className = { classes.closeBtn }
  80. onClick = { () => dispatch(removePoll(pollId, poll)) }
  81. role = 'button'
  82. src = { IconCloseLarge }
  83. tabIndex = { 0 } />
  84. }
  85. <div className = { classes.header }>
  86. <div className = { classes.question }>
  87. { poll.question }
  88. </div>
  89. <div className = { classes.creator }>
  90. { t('polls.by', { name: creatorName }) }
  91. </div>
  92. </div>
  93. <ul className = { classes.answerList }>
  94. {
  95. poll.answers.map((answer, index: number) => (
  96. <li
  97. className = { classes.answer }
  98. key = { index }>
  99. <Checkbox
  100. checked = { checkBoxStates[index] }
  101. disabled = { poll.saved }
  102. key = { index }
  103. label = { answer.name }
  104. onChange = { ev => setCheckbox(index, ev.target.checked) } />
  105. </li>
  106. ))
  107. }
  108. </ul>
  109. <div className = { classes.footer } >
  110. {
  111. pollSaved ? <>
  112. <Button
  113. accessibilityLabel = { t('polls.answer.edit') }
  114. className = { classes.buttonMargin }
  115. labelKey = { 'polls.answer.edit' }
  116. onClick = { () => {
  117. setCreateMode(true);
  118. dispatch(editPoll(pollId, true));
  119. } }
  120. type = { BUTTON_TYPES.SECONDARY } />
  121. <Button
  122. accessibilityLabel = { t('polls.answer.send') }
  123. labelKey = { 'polls.answer.send' }
  124. onClick = { sendPoll } />
  125. </> : <>
  126. <Button
  127. accessibilityLabel = { t('polls.answer.skip') }
  128. className = { classes.buttonMargin }
  129. labelKey = { 'polls.answer.skip' }
  130. onClick = { changingVote ? skipChangeVote : skipAnswer }
  131. type = { BUTTON_TYPES.SECONDARY } />
  132. <Button
  133. accessibilityLabel = { t('polls.answer.submit') }
  134. disabled = { isSubmitAnswerDisabled(checkBoxStates) }
  135. labelKey = { 'polls.answer.submit' }
  136. onClick = { submitAnswer } />
  137. </>
  138. }
  139. </div>
  140. </div>
  141. );
  142. };
  143. /*
  144. * We apply AbstractPollAnswer to fill in the AbstractProps common
  145. * to both the web and native implementations.
  146. */
  147. // eslint-disable-next-line new-cap
  148. export default AbstractPollAnswer(PollAnswer);