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.9KB

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