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

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