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

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