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

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