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.

AddPeopleDialog.web.js 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import React, { Component } from 'react';
  2. import { Immutable } from 'nuclear-js';
  3. import { connect } from 'react-redux';
  4. import Avatar from '@atlaskit/avatar';
  5. import InlineMessage from '@atlaskit/inline-message';
  6. import { getInviteURL } from '../../base/connection';
  7. import { Dialog, hideDialog } from '../../base/dialog';
  8. import { translate } from '../../base/i18n';
  9. import MultiSelectAutocomplete
  10. from '../../base/react/components/web/MultiSelectAutocomplete';
  11. import { invitePeople, searchPeople } from '../functions';
  12. declare var interfaceConfig: Object;
  13. /**
  14. * The dialog that allows to invite people to the call.
  15. */
  16. class AddPeopleDialog extends Component {
  17. /**
  18. * {@code AddPeopleDialog}'s property types.
  19. *
  20. * @static
  21. */
  22. static propTypes = {
  23. /**
  24. * The URL pointing to the service allowing for people invite.
  25. */
  26. _inviteServiceUrl: React.PropTypes.string,
  27. /**
  28. * The url of the conference to invite people to.
  29. */
  30. _inviteUrl: React.PropTypes.string,
  31. /**
  32. * The JWT token.
  33. */
  34. _jwt: React.PropTypes.string,
  35. /**
  36. * The URL pointing to the service allowing for people search.
  37. */
  38. _peopleSearchUrl: React.PropTypes.string,
  39. /**
  40. * The function closing the dialog.
  41. */
  42. hideDialog: React.PropTypes.func,
  43. /**
  44. * Invoked to obtain translated strings.
  45. */
  46. t: React.PropTypes.func
  47. };
  48. /**
  49. * Initializes a new {@code AddPeopleDialog} instance.
  50. *
  51. * @param {Object} props - The read-only properties with which the new
  52. * instance is to be initialized.
  53. */
  54. constructor(props) {
  55. super(props);
  56. this.state = {
  57. /**
  58. * Indicating that an error occurred when adding people to the call.
  59. */
  60. addToCallError: false,
  61. /**
  62. * Indicating that we're currently adding the new people to the
  63. * call.
  64. */
  65. addToCallInProgress: false,
  66. /**
  67. * The list of invite items.
  68. */
  69. inviteItems: new Immutable.List()
  70. };
  71. this._multiselect = null;
  72. this._resourceClient = {
  73. makeQuery: text => searchPeople(
  74. this.props._peopleSearchUrl, this.props._jwt, text),
  75. parseResults: response => response.map(user => {
  76. const avatar = ( // eslint-disable-line no-extra-parens
  77. <Avatar
  78. size = 'medium'
  79. src = { user.avatar } />
  80. );
  81. return {
  82. content: user.name,
  83. value: user.id,
  84. elemBefore: avatar,
  85. item: user
  86. };
  87. })
  88. };
  89. this._isAddDisabled = this._isAddDisabled.bind(this);
  90. this._onSelectionChange = this._onSelectionChange.bind(this);
  91. this._onSubmit = this._onSubmit.bind(this);
  92. this._setMultiSelectElement = this._setMultiSelectElement.bind(this);
  93. }
  94. /**
  95. * React Component method that executes once component is updated.
  96. *
  97. * @param {Object} prevState - The state object before the update.
  98. * @returns {void}
  99. */
  100. componentDidUpdate(prevState) {
  101. /**
  102. * Clears selected items from the multi select component on successful
  103. * invite.
  104. */
  105. if (prevState.addToCallError
  106. && !this.state.addToCallInProgress
  107. && !this.state.addToCallError
  108. && this._multiselect) {
  109. this._multiselect.clear();
  110. }
  111. }
  112. /**
  113. * Renders the content of this component.
  114. *
  115. * @returns {ReactElement}
  116. */
  117. render() {
  118. return (
  119. <Dialog
  120. okDisabled = { this._isAddDisabled() }
  121. okTitleKey = 'addPeople.add'
  122. onSubmit = { this._onSubmit }
  123. titleKey = 'addPeople.title'
  124. width = 'small'>
  125. { this._renderUserInputForm() }
  126. </Dialog>
  127. );
  128. }
  129. /**
  130. * Renders the input form.
  131. *
  132. * @returns {ReactElement}
  133. * @private
  134. */
  135. _renderUserInputForm() {
  136. const { t } = this.props;
  137. return (
  138. <div className = 'add-people-form-wrap'>
  139. { this._renderErrorMessage() }
  140. <MultiSelectAutocomplete
  141. isDisabled
  142. = { this.state.addToCallInProgress || false }
  143. noMatchesFound = { t('addPeople.noResults') }
  144. onSelectionChange = { this._onSelectionChange }
  145. placeholder = { t('addPeople.searchPlaceholder') }
  146. ref = { this._setMultiSelectElement }
  147. resourceClient = { this._resourceClient }
  148. shouldFitContainer = { true }
  149. shouldFocus = { true } />
  150. </div>
  151. );
  152. }
  153. /**
  154. * Indicates if the Add button should be disabled.
  155. *
  156. * @returns {boolean} - True to indicate that the Add button should
  157. * be disabled, false otherwise.
  158. * @private
  159. */
  160. _isAddDisabled() {
  161. return !this.state.inviteItems.length
  162. || this.state.addToCallInProgress;
  163. }
  164. /**
  165. * Handles a selection change.
  166. *
  167. * @param {Map} selectedItems - The list of selected items.
  168. * @private
  169. * @returns {void}
  170. */
  171. _onSelectionChange(selectedItems) {
  172. const selectedIds = selectedItems.map(o => o.item);
  173. this.setState({
  174. inviteItems: selectedIds
  175. });
  176. }
  177. /**
  178. * Handles the submit button action.
  179. *
  180. * @private
  181. * @returns {void}
  182. */
  183. _onSubmit() {
  184. if (!this._isAddDisabled()) {
  185. this.setState({
  186. addToCallInProgress: true
  187. });
  188. invitePeople(
  189. this.props._inviteServiceUrl,
  190. this.props._inviteUrl,
  191. this.props._jwt,
  192. this.state.inviteItems)
  193. .then(() => {
  194. this.setState({
  195. addToCallInProgress: false
  196. });
  197. this.props.hideDialog();
  198. })
  199. .catch(() => {
  200. this.setState({
  201. addToCallInProgress: false,
  202. addToCallError: true
  203. });
  204. });
  205. }
  206. }
  207. /**
  208. * Renders the error message if the add doesn't succeed.
  209. *
  210. * @returns {ReactElement|null}
  211. * @private
  212. */
  213. _renderErrorMessage() {
  214. if (!this.state.addToCallError) {
  215. return null;
  216. }
  217. const { t } = this.props;
  218. const supportString = t('inlineDialogFailure.supportMsg');
  219. const supportLink = interfaceConfig.SUPPORT_URL;
  220. const supportLinkContent
  221. = ( // eslint-disable-line no-extra-parens
  222. <span>
  223. <span>
  224. { supportString.padEnd(supportString.length + 1) }
  225. </span>
  226. <span>
  227. <a
  228. href = { supportLink }
  229. rel = 'noopener noreferrer'
  230. target = '_blank'>
  231. { t('inlineDialogFailure.support') }
  232. </a>
  233. </span>
  234. <span>.</span>
  235. </span>
  236. );
  237. return (
  238. <div className = 'modal-dialog-form-error'>
  239. <InlineMessage
  240. title = { t('addPeople.failedToAdd') }
  241. type = 'error'>
  242. { supportLinkContent }
  243. </InlineMessage>
  244. </div>
  245. );
  246. }
  247. /**
  248. * Sets the instance variable for the multi select component
  249. * element so it can be accessed directly.
  250. *
  251. * @param {Object} element - The DOM element for the component's dialog.
  252. * @private
  253. * @returns {void}
  254. */
  255. _setMultiSelectElement(element) {
  256. this._multiselect = element;
  257. }
  258. }
  259. /**
  260. * Maps (parts of) the Redux state to the associated
  261. * {@code AddPeopleDialog}'s props.
  262. *
  263. * @param {Object} state - The Redux state.
  264. * @private
  265. * @returns {{
  266. * _peopleSearchUrl: React.PropTypes.string,
  267. * _jwt: React.PropTypes.string
  268. * }}
  269. */
  270. function _mapStateToProps(state) {
  271. const { peopleSearchUrl, inviteServiceUrl } = state['features/base/config'];
  272. return {
  273. _jwt: state['features/jwt'].jwt,
  274. _inviteUrl: getInviteURL(state),
  275. _inviteServiceUrl: inviteServiceUrl,
  276. _peopleSearchUrl: peopleSearchUrl
  277. };
  278. }
  279. export default translate(
  280. connect(_mapStateToProps, { hideDialog })(AddPeopleDialog));