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

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