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.

FeedbackDialog.web.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. import StarIcon from '@atlaskit/icon/glyph/star';
  2. import StarFilledIcon from '@atlaskit/icon/glyph/star-filled';
  3. import PropTypes from 'prop-types';
  4. import React, { Component } from 'react';
  5. import { connect } from 'react-redux';
  6. import { sendAnalyticsEvent } from '../../analytics';
  7. import { Dialog } from '../../base/dialog';
  8. import { translate } from '../../base/i18n';
  9. import { cancelFeedback, submitFeedback } from '../actions';
  10. declare var interfaceConfig: Object;
  11. const scoreAnimationClass
  12. = interfaceConfig.ENABLE_FEEDBACK_ANIMATION ? 'shake-rotate' : '';
  13. /**
  14. * The scores to display for selecting. The score is the index in the array and
  15. * the value of the index is a translation key used for display in the dialog.
  16. *
  17. * @types {string[]}
  18. */
  19. const SCORES = [
  20. 'feedback.veryBad',
  21. 'feedback.bad',
  22. 'feedback.average',
  23. 'feedback.good',
  24. 'feedback.veryGood'
  25. ];
  26. /**
  27. * A React {@code Component} for displaying a dialog to rate the current
  28. * conference quality, write a message describing the experience, and submit
  29. * the feedback.
  30. *
  31. * @extends Component
  32. */
  33. class FeedbackDialog extends Component {
  34. /**
  35. * {@code FeedbackDialog} component's property types.
  36. *
  37. * @static
  38. */
  39. static propTypes = {
  40. /**
  41. * The cached feedback message, if any, that was set when closing a
  42. * previous instance of {@code FeedbackDialog}.
  43. */
  44. _message: PropTypes.string,
  45. /**
  46. * The cached feedback score, if any, that was set when closing a
  47. * previous instance of {@code FeedbackDialog}.
  48. */
  49. _score: PropTypes.number,
  50. /**
  51. * The JitsiConference that is being rated. The conference is passed in
  52. * because feedback can occur after a conference has been left, so
  53. * references to it may no longer exist in redux.
  54. *
  55. * @type {JitsiConference}
  56. */
  57. conference: PropTypes.object,
  58. /**
  59. * Invoked to signal feedback submission or canceling.
  60. */
  61. dispatch: PropTypes.func,
  62. /**
  63. * Callback invoked when {@code FeedbackDialog} is unmounted.
  64. */
  65. onClose: PropTypes.func,
  66. /**
  67. * Invoked to obtain translated strings.
  68. */
  69. t: PropTypes.func
  70. };
  71. /**
  72. * Initializes a new {@code FeedbackDialog} instance.
  73. *
  74. * @param {Object} props - The read-only React {@code Component} props with
  75. * which the new instance is to be initialized.
  76. */
  77. constructor(props) {
  78. super(props);
  79. const { _message, _score } = this.props;
  80. this.state = {
  81. /**
  82. * The currently entered feedback message.
  83. *
  84. * @type {string}
  85. */
  86. message: _message,
  87. /**
  88. * The score selection index which is currently being hovered. The
  89. * value -1 is used as a sentinel value to match store behavior of
  90. * using -1 for no score having been selected.
  91. *
  92. * @type {number}
  93. */
  94. mousedOverScore: -1,
  95. /**
  96. * The currently selected score selection index. The score will not
  97. * be 0 indexed so subtract one to map with SCORES.
  98. *
  99. * @type {number}
  100. */
  101. score: _score > -1 ? _score - 1 : _score
  102. };
  103. /**
  104. * An array of objects with click handlers for each of the scores listed
  105. * in SCORES. This pattern is used for binding event handlers only once
  106. * for each score selection icon.
  107. *
  108. * @type {Object[]}
  109. */
  110. this._scoreClickConfigurations = SCORES.map((textKey, index) => {
  111. return {
  112. _onClick: () => this._onScoreSelect(index),
  113. _onMouseOver: () => this._onScoreMouseOver(index)
  114. };
  115. });
  116. // Bind event handlers so they are only bound once for every instance.
  117. this._onCancel = this._onCancel.bind(this);
  118. this._onMessageChange = this._onMessageChange.bind(this);
  119. this._onScoreContainerMouseLeave
  120. = this._onScoreContainerMouseLeave.bind(this);
  121. this._onSubmit = this._onSubmit.bind(this);
  122. }
  123. /**
  124. * Emits an analytics event to notify feedback has been opened.
  125. *
  126. * @inheritdoc
  127. */
  128. componentDidMount() {
  129. sendAnalyticsEvent('feedback.open');
  130. }
  131. /**
  132. * Invokes the onClose callback, if defined, to notify of the close event.
  133. *
  134. * @inheritdoc
  135. */
  136. componentWillUnmount() {
  137. if (this.props.onClose) {
  138. this.props.onClose();
  139. }
  140. }
  141. /**
  142. * Implements React's {@link Component#render()}.
  143. *
  144. * @inheritdoc
  145. * @returns {ReactElement}
  146. */
  147. render() {
  148. const { message, mousedOverScore, score } = this.state;
  149. const scoreToDisplayAsSelected
  150. = mousedOverScore > -1 ? mousedOverScore : score;
  151. const scoreIcons = this._scoreClickConfigurations.map(
  152. (config, index) => {
  153. const isFilled = index <= scoreToDisplayAsSelected;
  154. const activeClass = isFilled ? 'active' : '';
  155. const className
  156. = `star-btn ${scoreAnimationClass} ${activeClass}`;
  157. return (
  158. <a
  159. className = { className }
  160. key = { index }
  161. onClick = { config._onClick }
  162. onMouseOver = { config._onMouseOver }>
  163. { isFilled
  164. ? <StarFilledIcon
  165. label = 'star-filled'
  166. size = 'xlarge' />
  167. : <StarIcon
  168. label = 'star'
  169. size = 'xlarge' /> }
  170. </a>
  171. );
  172. });
  173. const { t } = this.props;
  174. return (
  175. <Dialog
  176. okTitleKey = 'dialog.Submit'
  177. onCancel = { this._onCancel }
  178. onSubmit = { this._onSubmit }
  179. titleKey = 'feedback.rateExperience'>
  180. <div className = 'feedback-dialog'>
  181. <div className = 'rating'>
  182. <div className = 'star-label'>
  183. <p id = 'starLabel'>
  184. { t(SCORES[scoreToDisplayAsSelected]) }
  185. </p>
  186. </div>
  187. <div
  188. className = 'stars'
  189. onMouseLeave = { this._onScoreContainerMouseLeave }>
  190. { scoreIcons }
  191. </div>
  192. </div>
  193. <div className = 'details'>
  194. <textarea
  195. autoFocus = { true }
  196. className = 'input-control'
  197. id = 'feedbackTextArea'
  198. onChange = { this._onMessageChange }
  199. placeholder = { t('dialog.feedbackHelp') }
  200. value = { message } />
  201. </div>
  202. </div>
  203. </Dialog>
  204. );
  205. }
  206. /**
  207. * Dispatches an action notifying feedback was not submitted. The submitted
  208. * score will have one added as the rest of the app does not expect 0
  209. * indexing.
  210. *
  211. * @private
  212. * @returns {boolean} Returns true to close the dialog.
  213. */
  214. _onCancel() {
  215. const { message, score } = this.state;
  216. const scoreToSubmit = score > -1 ? score + 1 : score;
  217. this.props.dispatch(cancelFeedback(scoreToSubmit, message));
  218. return true;
  219. }
  220. /**
  221. * Updates the known entered feedback message.
  222. *
  223. * @param {Object} event - The DOM event from updating the textfield for the
  224. * feedback message.
  225. * @private
  226. * @returns {void}
  227. */
  228. _onMessageChange(event) {
  229. this.setState({ message: event.target.value });
  230. }
  231. /**
  232. * Updates the currently selected score.
  233. *
  234. * @param {number} score - The index of the selected score in SCORES.
  235. * @private
  236. * @returns {void}
  237. */
  238. _onScoreSelect(score) {
  239. this.setState({ score });
  240. }
  241. /**
  242. * Sets the currently hovered score to null to indicate no hover is
  243. * occurring.
  244. *
  245. * @private
  246. * @returns {void}
  247. */
  248. _onScoreContainerMouseLeave() {
  249. this.setState({ mousedOverScore: -1 });
  250. }
  251. /**
  252. * Updates the known state of the score icon currently behind hovered over.
  253. *
  254. * @param {number} mousedOverScore - The index of the SCORES value currently
  255. * being moused over.
  256. * @private
  257. * @returns {void}
  258. */
  259. _onScoreMouseOver(mousedOverScore) {
  260. this.setState({ mousedOverScore });
  261. }
  262. /**
  263. * Dispatches the entered feedback for submission. The submitted score will
  264. * have one added as the rest of the app does not expect 0 indexing.
  265. *
  266. * @private
  267. * @returns {boolean} Returns true to close the dialog.
  268. */
  269. _onSubmit() {
  270. const { conference, dispatch } = this.props;
  271. const { message, score } = this.state;
  272. const scoreToSubmit = score > -1 ? score + 1 : score;
  273. dispatch(submitFeedback(scoreToSubmit, message, conference));
  274. return true;
  275. }
  276. }
  277. /**
  278. * Maps (parts of) the Redux state to the associated {@code FeedbackDialog}'s
  279. * props.
  280. *
  281. * @param {Object} state - The Redux state.
  282. * @private
  283. * @returns {{
  284. * }}
  285. */
  286. function _mapStateToProps(state) {
  287. const { message, score } = state['features/feedback'];
  288. return {
  289. /**
  290. * The cached feedback message, if any, that was set when closing a
  291. * previous instance of {@code FeedbackDialog}.
  292. *
  293. * @type {string}
  294. */
  295. _message: message,
  296. /**
  297. * The currently selected score selection index.
  298. *
  299. * @type {number}
  300. */
  301. _score: score
  302. };
  303. }
  304. export default translate(connect(_mapStateToProps)(FeedbackDialog));