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

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