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

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