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

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