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.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. // @flow
  2. import _ from 'lodash';
  3. import React from 'react';
  4. import {
  5. ActivityIndicator,
  6. FlatList,
  7. Platform,
  8. SafeAreaView,
  9. TextInput,
  10. TouchableOpacity,
  11. View
  12. } from 'react-native';
  13. import { ColorSchemeRegistry } from '../../../../base/color-scheme';
  14. import { AlertDialog, openDialog } from '../../../../base/dialog';
  15. import { translate } from '../../../../base/i18n';
  16. import {
  17. Icon,
  18. IconCancelSelection,
  19. IconCheck,
  20. IconClose,
  21. IconPhone,
  22. IconSearch,
  23. IconShare
  24. } from '../../../../base/icons';
  25. import { JitsiModal, setActiveModalId } from '../../../../base/modal';
  26. import {
  27. AvatarListItem,
  28. type Item
  29. } from '../../../../base/react';
  30. import { connect } from '../../../../base/redux';
  31. import { beginShareRoom } from '../../../../share-room';
  32. import { ADD_PEOPLE_DIALOG_VIEW_ID, INVITE_TYPES } from '../../../constants';
  33. import AbstractAddPeopleDialog, {
  34. type Props as AbstractProps,
  35. type State as AbstractState,
  36. _mapStateToProps as _abstractMapStateToProps
  37. } from '../AbstractAddPeopleDialog';
  38. import styles, {
  39. AVATAR_SIZE,
  40. DARK_GREY
  41. } from './styles';
  42. type Props = AbstractProps & {
  43. /**
  44. * The color schemed style of the Header.
  45. */
  46. _headerStyles: Object,
  47. /**
  48. * True if the invite dialog should be open, false otherwise.
  49. */
  50. _isVisible: boolean,
  51. /**
  52. * Function used to translate i18n labels.
  53. */
  54. t: Function
  55. };
  56. type State = AbstractState & {
  57. /**
  58. * Boolean to show if an extra padding needs to be added to the bottom bar.
  59. */
  60. bottomPadding: boolean,
  61. /**
  62. * State variable to keep track of the search field value.
  63. */
  64. fieldValue: string,
  65. /**
  66. * True if a search is in progress, false otherwise.
  67. */
  68. searchInprogress: boolean,
  69. /**
  70. * An array of items that are selectable on this dialog. This is usually
  71. * populated by an async search.
  72. */
  73. selectableItems: Array<Object>
  74. };
  75. /**
  76. * Implements a special dialog to invite people from a directory service.
  77. */
  78. class AddPeopleDialog extends AbstractAddPeopleDialog<Props, State> {
  79. /**
  80. * Default state object to reset the state to when needed.
  81. */
  82. defaultState = {
  83. addToCallError: false,
  84. addToCallInProgress: false,
  85. bottomPadding: false,
  86. fieldValue: '',
  87. inviteItems: [],
  88. searchInprogress: false,
  89. selectableItems: []
  90. };
  91. /**
  92. * Ref of the search field.
  93. */
  94. inputFieldRef: ?TextInput;
  95. /**
  96. * TimeoutID to delay the search for the time the user is probably typing.
  97. */
  98. searchTimeout: TimeoutID;
  99. /**
  100. * Contrustor of the component.
  101. *
  102. * @inheritdoc
  103. */
  104. constructor(props: Props) {
  105. super(props);
  106. this.state = this.defaultState;
  107. this._keyExtractor = this._keyExtractor.bind(this);
  108. this._renderInvitedItem = this._renderInvitedItem.bind(this);
  109. this._renderItem = this._renderItem.bind(this);
  110. this._renderSeparator = this._renderSeparator.bind(this);
  111. this._onClearField = this._onClearField.bind(this);
  112. this._onInvite = this._onInvite.bind(this);
  113. this._onPressItem = this._onPressItem.bind(this);
  114. this._onShareMeeting = this._onShareMeeting.bind(this);
  115. this._onTypeQuery = this._onTypeQuery.bind(this);
  116. this._renderShareMeetingButton = this._renderShareMeetingButton.bind(this);
  117. this._setFieldRef = this._setFieldRef.bind(this);
  118. }
  119. /**
  120. * Implements {@code Component#componentDidUpdate}.
  121. *
  122. * @inheritdoc
  123. */
  124. componentDidUpdate(prevProps) {
  125. if (prevProps._isVisible !== this.props._isVisible) {
  126. // Clear state
  127. this._clearState();
  128. }
  129. }
  130. /**
  131. * Implements {@code Component#render}.
  132. *
  133. * @inheritdoc
  134. */
  135. render() {
  136. const {
  137. _addPeopleEnabled,
  138. _dialOutEnabled
  139. } = this.props;
  140. const { inviteItems, selectableItems } = this.state;
  141. let placeholderKey = 'searchPlaceholder';
  142. if (!_addPeopleEnabled) {
  143. placeholderKey = 'searchCallOnlyPlaceholder';
  144. } else if (!_dialOutEnabled) {
  145. placeholderKey = 'searchPeopleOnlyPlaceholder';
  146. }
  147. return (
  148. <JitsiModal
  149. footerComponent = { this._renderShareMeetingButton }
  150. headerProps = {{
  151. forwardDisabled: this._isAddDisabled(),
  152. forwardLabelKey: 'inviteDialog.send',
  153. headerLabelKey: 'inviteDialog.header',
  154. onPressForward: this._onInvite
  155. }}
  156. modalId = { ADD_PEOPLE_DIALOG_VIEW_ID }>
  157. <View
  158. style = { styles.searchFieldWrapper }>
  159. <View style = { styles.searchIconWrapper }>
  160. { this.state.searchInprogress
  161. ? <ActivityIndicator
  162. color = { DARK_GREY }
  163. size = 'small' />
  164. : <Icon
  165. src = { IconSearch }
  166. style = { styles.searchIcon } />}
  167. </View>
  168. <TextInput
  169. autoCorrect = { false }
  170. autoFocus = { false }
  171. onBlur = { this._onFocused(false) }
  172. onChangeText = { this._onTypeQuery }
  173. onFocus = { this._onFocused(true) }
  174. placeholder = {
  175. this.props.t(`inviteDialog.${placeholderKey}`)
  176. }
  177. ref = { this._setFieldRef }
  178. style = { styles.searchField }
  179. value = { this.state.fieldValue } />
  180. { this._renderClearButton() }
  181. </View>
  182. { Boolean(inviteItems.length) && <View style = { styles.invitedList }>
  183. <FlatList
  184. data = { inviteItems }
  185. horizontal = { true }
  186. keyExtractor = { this._keyExtractor }
  187. keyboardShouldPersistTaps = 'always'
  188. renderItem = { this._renderInvitedItem } />
  189. </View> }
  190. <View style = { styles.resultList }>
  191. <FlatList
  192. ItemSeparatorComponent = { this._renderSeparator }
  193. data = { selectableItems }
  194. extraData = { inviteItems }
  195. keyExtractor = { this._keyExtractor }
  196. keyboardShouldPersistTaps = 'always'
  197. renderItem = { this._renderItem } />
  198. </View>
  199. </JitsiModal>
  200. );
  201. }
  202. /**
  203. * Clears the dialog content.
  204. *
  205. * @returns {void}
  206. */
  207. _clearState() {
  208. this.setState(this.defaultState);
  209. }
  210. /**
  211. * Returns an object capable of being rendered by an {@code AvatarListItem}.
  212. *
  213. * @param {Object} flatListItem - An item of the data array of the {@code FlatList}.
  214. * @returns {?Object}
  215. */
  216. _getRenderableItem(flatListItem) {
  217. const { item } = flatListItem;
  218. switch (item.type) {
  219. case INVITE_TYPES.PHONE:
  220. return {
  221. avatar: IconPhone,
  222. key: item.number,
  223. title: item.number
  224. };
  225. case INVITE_TYPES.USER:
  226. return {
  227. avatar: item.avatar,
  228. key: item.id || item.user_id,
  229. title: item.name
  230. };
  231. default:
  232. return null;
  233. }
  234. }
  235. _invite: Array<Object> => Promise<Array<Object>>
  236. _isAddDisabled: () => boolean;
  237. _keyExtractor: Object => string
  238. /**
  239. * Key extractor for the flatlist.
  240. *
  241. * @param {Object} item - The flatlist item that we need the key to be
  242. * generated for.
  243. * @returns {string}
  244. */
  245. _keyExtractor(item) {
  246. return item.type === INVITE_TYPES.USER ? item.id || item.user_id : item.number;
  247. }
  248. _onClearField: () => void
  249. /**
  250. * Callback to clear the text field.
  251. *
  252. * @returns {void}
  253. */
  254. _onClearField() {
  255. this.setState({
  256. fieldValue: ''
  257. });
  258. // Clear search results
  259. this._onTypeQuery('');
  260. }
  261. _onFocused: boolean => Function;
  262. /**
  263. * Constructs a callback to be used to update the padding of the field if necessary.
  264. *
  265. * @param {boolean} focused - True of the field is focused.
  266. * @returns {Function}
  267. */
  268. _onFocused(focused) {
  269. return () => {
  270. Platform.OS === 'android' && this.setState({
  271. bottomPadding: focused
  272. });
  273. };
  274. }
  275. _onInvite: () => void
  276. /**
  277. * Invites the selected entries.
  278. *
  279. * @returns {void}
  280. */
  281. _onInvite() {
  282. this._invite(this.state.inviteItems)
  283. .then(invitesLeftToSend => {
  284. if (invitesLeftToSend.length) {
  285. this.setState({
  286. inviteItems: invitesLeftToSend
  287. });
  288. this._showFailedInviteAlert();
  289. } else {
  290. this.props.dispatch(setActiveModalId());
  291. }
  292. });
  293. }
  294. _onPressItem: Item => Function
  295. /**
  296. * Function to prepare a callback for the onPress event of the touchable.
  297. *
  298. * @param {Item} item - The item on which onPress was invoked.
  299. * @returns {Function}
  300. */
  301. _onPressItem(item) {
  302. return () => {
  303. const { inviteItems } = this.state;
  304. const finderKey = item.type === INVITE_TYPES.PHONE ? 'number' : 'user_id';
  305. if (inviteItems.find(
  306. _.matchesProperty(finderKey, item[finderKey]))) {
  307. // Item is already selected, need to unselect it.
  308. this.setState({
  309. inviteItems: inviteItems.filter(
  310. element => item[finderKey] !== element[finderKey])
  311. });
  312. } else {
  313. // Item is not selected yet, need to add to the list.
  314. const items: Array<Object> = inviteItems.concat(item);
  315. this.setState({
  316. inviteItems: _.sortBy(items, [ 'name', 'number' ])
  317. });
  318. }
  319. };
  320. }
  321. _onShareMeeting: () => void
  322. /**
  323. * Shows the system share sheet to share the meeting information.
  324. *
  325. * @returns {void}
  326. */
  327. _onShareMeeting() {
  328. if (this.state.inviteItems.length > 0) {
  329. // The use probably intended to invite people.
  330. this._onInvite();
  331. } else {
  332. this.props.dispatch(beginShareRoom());
  333. }
  334. }
  335. _onTypeQuery: string => void
  336. /**
  337. * Handles the typing event of the text field on the dialog and performs the
  338. * search.
  339. *
  340. * @param {string} query - The query that is typed in the field.
  341. * @returns {void}
  342. */
  343. _onTypeQuery(query) {
  344. this.setState({
  345. fieldValue: query
  346. });
  347. clearTimeout(this.searchTimeout);
  348. this.searchTimeout = setTimeout(() => {
  349. this.setState({
  350. searchInprogress: true
  351. }, () => {
  352. this._performSearch(query);
  353. });
  354. }, 500);
  355. }
  356. /**
  357. * Performs the actual search.
  358. *
  359. * @param {string} query - The query to search for.
  360. * @returns {void}
  361. */
  362. _performSearch(query) {
  363. this._query(query).then(results => {
  364. this.setState({
  365. selectableItems: _.sortBy(results, [ 'name', 'number' ])
  366. });
  367. })
  368. .finally(() => {
  369. this.setState({
  370. searchInprogress: false
  371. }, () => {
  372. this.inputFieldRef && this.inputFieldRef.focus();
  373. });
  374. });
  375. }
  376. _query: (string) => Promise<Array<Object>>;
  377. /**
  378. * Renders a button to clear the text field.
  379. *
  380. * @returns {React#Element<*>}
  381. */
  382. _renderClearButton() {
  383. if (!this.state.fieldValue.length) {
  384. return null;
  385. }
  386. return (
  387. <TouchableOpacity
  388. onPress = { this._onClearField }
  389. style = { styles.clearButton }>
  390. <View style = { styles.clearIconContainer }>
  391. <Icon
  392. src = { IconClose }
  393. style = { styles.clearIcon } />
  394. </View>
  395. </TouchableOpacity>
  396. );
  397. }
  398. _renderInvitedItem: Object => React$Element<any> | null
  399. /**
  400. * Renders a single item in the invited {@code FlatList}.
  401. *
  402. * @param {Object} flatListItem - An item of the data array of the
  403. * {@code FlatList}.
  404. * @param {number} index - The index of the currently rendered item.
  405. * @returns {?React$Element<any>}
  406. */
  407. _renderInvitedItem(flatListItem, index): React$Element<any> | null {
  408. const { item } = flatListItem;
  409. const renderableItem = this._getRenderableItem(flatListItem);
  410. return (
  411. <TouchableOpacity onPress = { this._onPressItem(item) } >
  412. <View
  413. pointerEvents = 'box-only'
  414. style = { styles.itemWrapper }>
  415. <AvatarListItem
  416. avatarOnly = { true }
  417. avatarSize = { AVATAR_SIZE }
  418. avatarStatus = { item.status }
  419. avatarStyle = { styles.avatar }
  420. avatarTextStyle = { styles.avatarText }
  421. item = { renderableItem }
  422. key = { index }
  423. linesStyle = { styles.itemLinesStyle }
  424. titleStyle = { styles.itemText } />
  425. <Icon
  426. src = { IconCancelSelection }
  427. style = { styles.unselectIcon } />
  428. </View>
  429. </TouchableOpacity>
  430. );
  431. }
  432. _renderItem: Object => React$Element<any> | null
  433. /**
  434. * Renders a single item in the search result {@code FlatList}.
  435. *
  436. * @param {Object} flatListItem - An item of the data array of the
  437. * {@code FlatList}.
  438. * @param {number} index - The index of the currently rendered item.
  439. * @returns {?React$Element<*>}
  440. */
  441. _renderItem(flatListItem, index): React$Element<any> | null {
  442. const { item } = flatListItem;
  443. const { inviteItems } = this.state;
  444. let selected = false;
  445. const renderableItem = this._getRenderableItem(flatListItem);
  446. if (!renderableItem) {
  447. return null;
  448. }
  449. switch (item.type) {
  450. case INVITE_TYPES.PHONE:
  451. selected = inviteItems.find(_.matchesProperty('number', item.number));
  452. break;
  453. case INVITE_TYPES.USER:
  454. selected = item.id
  455. ? inviteItems.find(_.matchesProperty('id', item.id))
  456. : inviteItems.find(_.matchesProperty('user_id', item.user_id));
  457. break;
  458. default:
  459. return null;
  460. }
  461. return (
  462. <TouchableOpacity onPress = { this._onPressItem(item) } >
  463. <View
  464. pointerEvents = 'box-only'
  465. style = { styles.itemWrapper }>
  466. <AvatarListItem
  467. avatarSize = { AVATAR_SIZE }
  468. avatarStatus = { item.status }
  469. avatarStyle = { styles.avatar }
  470. avatarTextStyle = { styles.avatarText }
  471. item = { renderableItem }
  472. key = { index }
  473. linesStyle = { styles.itemLinesStyle }
  474. titleStyle = { styles.itemText } />
  475. { selected && <Icon
  476. src = { IconCheck }
  477. style = { styles.selectedIcon } /> }
  478. </View>
  479. </TouchableOpacity>
  480. );
  481. }
  482. _renderSeparator: () => React$Element<*> | null
  483. /**
  484. * Renders the item separator.
  485. *
  486. * @returns {?React$Element<*>}
  487. */
  488. _renderSeparator() {
  489. return (
  490. <View style = { styles.separator } />
  491. );
  492. }
  493. _renderShareMeetingButton: () => React$Element<any>;
  494. /**
  495. * Renders a button to share the meeting info.
  496. *
  497. * @returns {React#Element<*>}
  498. */
  499. _renderShareMeetingButton() {
  500. const { _headerStyles } = this.props;
  501. return (
  502. <SafeAreaView
  503. style = { [
  504. styles.bottomBar,
  505. _headerStyles.headerOverlay,
  506. this.state.bottomPadding ? styles.extraBarPadding : null
  507. ] }>
  508. <TouchableOpacity
  509. onPress = { this._onShareMeeting }>
  510. <Icon
  511. src = { IconShare }
  512. style = { [ _headerStyles.headerButtonText, styles.shareIcon ] } />
  513. </TouchableOpacity>
  514. </SafeAreaView>
  515. );
  516. }
  517. _setFieldRef: ?TextInput => void
  518. /**
  519. * Sets a reference to the input field for later use.
  520. *
  521. * @param {?TextInput} input - The reference to the input field.
  522. * @returns {void}
  523. */
  524. _setFieldRef(input) {
  525. this.inputFieldRef = input;
  526. }
  527. /**
  528. * Shows an alert telling the user that some invitees were failed to be
  529. * invited.
  530. *
  531. * NOTE: We're using an Alert here because we're on a modal and it makes
  532. * using our dialogs a tad more difficult.
  533. *
  534. * @returns {void}
  535. */
  536. _showFailedInviteAlert() {
  537. this.props.dispatch(openDialog(AlertDialog, {
  538. contentKey: {
  539. key: 'inviteDialog.alertText'
  540. }
  541. }));
  542. }
  543. }
  544. /**
  545. * Maps part of the Redux state to the props of this component.
  546. *
  547. * @param {Object} state - The Redux state.
  548. * @returns {{
  549. * _isVisible: boolean
  550. * }}
  551. */
  552. function _mapStateToProps(state: Object) {
  553. return {
  554. ..._abstractMapStateToProps(state),
  555. _headerStyles: ColorSchemeRegistry.get(state, 'Header'),
  556. _isVisible: state['features/base/modal'].activeModalId === ADD_PEOPLE_DIALOG_VIEW_ID
  557. };
  558. }
  559. export default translate(connect(_mapStateToProps)(AddPeopleDialog));