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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 { translate } from '../../base/i18n';
  13. import { connect } from '../../base/redux';
  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. _onKeyPres: e => {
  135. if (e.key === ' ' || e.key === 'Enter') {
  136. e.preventDefault();
  137. this._onScoreSelect(index);
  138. }
  139. },
  140. _onMouseOver: () => this._onScoreMouseOver(index)
  141. };
  142. });
  143. // Bind event handlers so they are only bound once for every instance.
  144. this._onCancel = this._onCancel.bind(this);
  145. this._onMessageChange = this._onMessageChange.bind(this);
  146. this._onScoreContainerMouseLeave
  147. = this._onScoreContainerMouseLeave.bind(this);
  148. this._onSubmit = this._onSubmit.bind(this);
  149. }
  150. /**
  151. * Emits an analytics event to notify feedback has been opened.
  152. *
  153. * @inheritdoc
  154. */
  155. componentDidMount() {
  156. sendAnalytics(createFeedbackOpenEvent());
  157. if (typeof APP !== 'undefined') {
  158. APP.API.notifyFeedbackPromptDisplayed();
  159. }
  160. }
  161. /**
  162. * Invokes the onClose callback, if defined, to notify of the close event.
  163. *
  164. * @inheritdoc
  165. */
  166. componentWillUnmount() {
  167. if (this.props.onClose) {
  168. this.props.onClose();
  169. }
  170. }
  171. /**
  172. * Implements React's {@link Component#render()}.
  173. *
  174. * @inheritdoc
  175. * @returns {ReactElement}
  176. */
  177. render() {
  178. const { message, mousedOverScore, score } = this.state;
  179. const scoreToDisplayAsSelected
  180. = mousedOverScore > -1 ? mousedOverScore : score;
  181. const { t } = this.props;
  182. const scoreIcons = this._scoreClickConfigurations.map(
  183. (config, index) => {
  184. const isFilled = index <= scoreToDisplayAsSelected;
  185. const activeClass = isFilled ? 'active' : '';
  186. const className
  187. = `star-btn ${scoreAnimationClass} ${activeClass}`;
  188. return (
  189. <span
  190. aria-label = { t(SCORES[index]) }
  191. className = { className }
  192. key = { index }
  193. onClick = { config._onClick }
  194. onKeyPress = { config._onKeyPres }
  195. onMouseOver = { config._onMouseOver }
  196. role = 'button'
  197. tabIndex = { 0 }>
  198. { isFilled
  199. ? <StarFilledIcon
  200. label = 'star-filled'
  201. size = 'xlarge' />
  202. : <StarIcon
  203. label = 'star'
  204. size = 'xlarge' /> }
  205. </span>
  206. );
  207. });
  208. return (
  209. <Dialog
  210. okKey = 'dialog.Submit'
  211. onCancel = { this._onCancel }
  212. onSubmit = { this._onSubmit }
  213. titleKey = 'feedback.rateExperience'>
  214. <div className = 'feedback-dialog'>
  215. <div className = 'rating'>
  216. <div
  217. aria-label = { this.props.t('feedback.star') }
  218. className = 'star-label' >
  219. <p id = 'starLabel'>
  220. { t(SCORES[scoreToDisplayAsSelected]) }
  221. </p>
  222. </div>
  223. <div
  224. className = 'stars'
  225. onMouseLeave = { this._onScoreContainerMouseLeave }>
  226. { scoreIcons }
  227. </div>
  228. </div>
  229. <div className = 'details'>
  230. <FieldTextAreaStateless
  231. autoFocus = { true }
  232. className = 'input-control'
  233. id = 'feedbackTextArea'
  234. label = { t('feedback.detailsLabel') }
  235. onChange = { this._onMessageChange }
  236. shouldFitContainer = { true }
  237. value = { message } />
  238. </div>
  239. </div>
  240. </Dialog>
  241. );
  242. }
  243. _onCancel: () => boolean;
  244. /**
  245. * Dispatches an action notifying feedback was not submitted. The submitted
  246. * score will have one added as the rest of the app does not expect 0
  247. * indexing.
  248. *
  249. * @private
  250. * @returns {boolean} Returns true to close the dialog.
  251. */
  252. _onCancel() {
  253. const { message, score } = this.state;
  254. const scoreToSubmit = score > -1 ? score + 1 : score;
  255. this.props.dispatch(cancelFeedback(scoreToSubmit, message));
  256. return true;
  257. }
  258. _onMessageChange: (Object) => void;
  259. /**
  260. * Updates the known entered feedback message.
  261. *
  262. * @param {Object} event - The DOM event from updating the textfield for the
  263. * feedback message.
  264. * @private
  265. * @returns {void}
  266. */
  267. _onMessageChange(event) {
  268. this.setState({ message: event.target.value });
  269. }
  270. /**
  271. * Updates the currently selected score.
  272. *
  273. * @param {number} score - The index of the selected score in SCORES.
  274. * @private
  275. * @returns {void}
  276. */
  277. _onScoreSelect(score) {
  278. this.setState({ score });
  279. }
  280. _onScoreContainerMouseLeave: () => void;
  281. /**
  282. * Sets the currently hovered score to null to indicate no hover is
  283. * occurring.
  284. *
  285. * @private
  286. * @returns {void}
  287. */
  288. _onScoreContainerMouseLeave() {
  289. this.setState({ mousedOverScore: -1 });
  290. }
  291. /**
  292. * Updates the known state of the score icon currently behind hovered over.
  293. *
  294. * @param {number} mousedOverScore - The index of the SCORES value currently
  295. * being moused over.
  296. * @private
  297. * @returns {void}
  298. */
  299. _onScoreMouseOver(mousedOverScore) {
  300. this.setState({ mousedOverScore });
  301. }
  302. _onSubmit: () => void;
  303. /**
  304. * Dispatches the entered feedback for submission. The submitted score will
  305. * have one added as the rest of the app does not expect 0 indexing.
  306. *
  307. * @private
  308. * @returns {boolean} Returns true to close the dialog.
  309. */
  310. _onSubmit() {
  311. const { conference, dispatch } = this.props;
  312. const { message, score } = this.state;
  313. const scoreToSubmit = score > -1 ? score + 1 : score;
  314. dispatch(submitFeedback(scoreToSubmit, message, conference));
  315. return true;
  316. }
  317. }
  318. /**
  319. * Maps (parts of) the Redux state to the associated {@code FeedbackDialog}'s
  320. * props.
  321. *
  322. * @param {Object} state - The Redux state.
  323. * @private
  324. * @returns {{
  325. * }}
  326. */
  327. function _mapStateToProps(state) {
  328. const { message, score } = state['features/feedback'];
  329. return {
  330. /**
  331. * The cached feedback message, if any, that was set when closing a
  332. * previous instance of {@code FeedbackDialog}.
  333. *
  334. * @type {string}
  335. */
  336. _message: message,
  337. /**
  338. * The currently selected score selection index.
  339. *
  340. * @type {number}
  341. */
  342. _score: score
  343. };
  344. }
  345. export default translate(connect(_mapStateToProps)(FeedbackDialog));