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 11KB

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