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

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