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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. isLabelHidden = { true }
  203. onChange = { this._onMessageChange }
  204. placeholder = { t('dialog.feedbackHelp') }
  205. shouldFitContainer = { true }
  206. value = { message } />
  207. </div>
  208. </div>
  209. </Dialog>
  210. );
  211. }
  212. /**
  213. * Dispatches an action notifying feedback was not submitted. The submitted
  214. * score will have one added as the rest of the app does not expect 0
  215. * indexing.
  216. *
  217. * @private
  218. * @returns {boolean} Returns true to close the dialog.
  219. */
  220. _onCancel() {
  221. const { message, score } = this.state;
  222. const scoreToSubmit = score > -1 ? score + 1 : score;
  223. this.props.dispatch(cancelFeedback(scoreToSubmit, message));
  224. return true;
  225. }
  226. /**
  227. * Updates the known entered feedback message.
  228. *
  229. * @param {Object} event - The DOM event from updating the textfield for the
  230. * feedback message.
  231. * @private
  232. * @returns {void}
  233. */
  234. _onMessageChange(event) {
  235. this.setState({ message: event.target.value });
  236. }
  237. /**
  238. * Updates the currently selected score.
  239. *
  240. * @param {number} score - The index of the selected score in SCORES.
  241. * @private
  242. * @returns {void}
  243. */
  244. _onScoreSelect(score) {
  245. this.setState({ score });
  246. }
  247. /**
  248. * Sets the currently hovered score to null to indicate no hover is
  249. * occurring.
  250. *
  251. * @private
  252. * @returns {void}
  253. */
  254. _onScoreContainerMouseLeave() {
  255. this.setState({ mousedOverScore: -1 });
  256. }
  257. /**
  258. * Updates the known state of the score icon currently behind hovered over.
  259. *
  260. * @param {number} mousedOverScore - The index of the SCORES value currently
  261. * being moused over.
  262. * @private
  263. * @returns {void}
  264. */
  265. _onScoreMouseOver(mousedOverScore) {
  266. this.setState({ mousedOverScore });
  267. }
  268. /**
  269. * Dispatches the entered feedback for submission. The submitted score will
  270. * have one added as the rest of the app does not expect 0 indexing.
  271. *
  272. * @private
  273. * @returns {boolean} Returns true to close the dialog.
  274. */
  275. _onSubmit() {
  276. const { conference, dispatch } = this.props;
  277. const { message, score } = this.state;
  278. const scoreToSubmit = score > -1 ? score + 1 : score;
  279. dispatch(submitFeedback(scoreToSubmit, message, conference));
  280. return true;
  281. }
  282. }
  283. /**
  284. * Maps (parts of) the Redux state to the associated {@code FeedbackDialog}'s
  285. * props.
  286. *
  287. * @param {Object} state - The Redux state.
  288. * @private
  289. * @returns {{
  290. * }}
  291. */
  292. function _mapStateToProps(state) {
  293. const { message, score } = state['features/feedback'];
  294. return {
  295. /**
  296. * The cached feedback message, if any, that was set when closing a
  297. * previous instance of {@code FeedbackDialog}.
  298. *
  299. * @type {string}
  300. */
  301. _message: message,
  302. /**
  303. * The currently selected score selection index.
  304. *
  305. * @type {number}
  306. */
  307. _score: score
  308. };
  309. }
  310. export default translate(connect(_mapStateToProps)(FeedbackDialog));