您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

FeedbackDialog.web.js 12KB

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