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

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