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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. showHeaderWithNavigation = { true }>
  158. <View
  159. style = { styles.searchFieldWrapper }>
  160. <View style = { styles.searchIconWrapper }>
  161. { this.state.searchInprogress
  162. ? <ActivityIndicator
  163. color = { DARK_GREY }
  164. size = 'small' />
  165. : <Icon
  166. src = { IconSearch }
  167. style = { styles.searchIcon } />}
  168. </View>
  169. <TextInput
  170. autoCorrect = { false }
  171. autoFocus = { false }
  172. onBlur = { this._onFocused(false) }
  173. onChangeText = { this._onTypeQuery }
  174. onFocus = { this._onFocused(true) }
  175. placeholder = {
  176. this.props.t(`inviteDialog.${placeholderKey}`)
  177. }
  178. ref = { this._setFieldRef }
  179. style = { styles.searchField }
  180. value = { this.state.fieldValue } />
  181. { this._renderClearButton() }
  182. </View>
  183. { Boolean(inviteItems.length) && <View style = { styles.invitedList }>
  184. <FlatList
  185. data = { inviteItems }
  186. horizontal = { true }
  187. keyExtractor = { this._keyExtractor }
  188. keyboardShouldPersistTaps = 'always'
  189. renderItem = { this._renderInvitedItem } />
  190. </View> }
  191. <View style = { styles.resultList }>
  192. <FlatList
  193. ItemSeparatorComponent = { this._renderSeparator }
  194. data = { selectableItems }
  195. extraData = { inviteItems }
  196. keyExtractor = { this._keyExtractor }
  197. keyboardShouldPersistTaps = 'always'
  198. renderItem = { this._renderItem } />
  199. </View>
  200. </JitsiModal>
  201. );
  202. }
  203. /**
  204. * Clears the dialog content.
  205. *
  206. * @returns {void}
  207. */
  208. _clearState() {
  209. this.setState(this.defaultState);
  210. }
  211. /**
  212. * Returns an object capable of being rendered by an {@code AvatarListItem}.
  213. *
  214. * @param {Object} flatListItem - An item of the data array of the {@code FlatList}.
  215. * @returns {?Object}
  216. */
  217. _getRenderableItem(flatListItem) {
  218. const { item } = flatListItem;
  219. switch (item.type) {
  220. case INVITE_TYPES.PHONE:
  221. return {
  222. avatar: IconPhone,
  223. key: item.number,
  224. title: item.number
  225. };
  226. case INVITE_TYPES.USER:
  227. return {
  228. avatar: item.avatar,
  229. key: item.id || item.user_id,
  230. title: item.name
  231. };
  232. default:
  233. return null;
  234. }
  235. }
  236. _invite: Array<Object> => Promise<Array<Object>>
  237. _isAddDisabled: () => boolean;
  238. _keyExtractor: Object => string
  239. /**
  240. * Key extractor for the flatlist.
  241. *
  242. * @param {Object} item - The flatlist item that we need the key to be
  243. * generated for.
  244. * @returns {string}
  245. */
  246. _keyExtractor(item) {
  247. return item.type === INVITE_TYPES.USER ? item.id || item.user_id : item.number;
  248. }
  249. _onClearField: () => void
  250. /**
  251. * Callback to clear the text field.
  252. *
  253. * @returns {void}
  254. */
  255. _onClearField() {
  256. this.setState({
  257. fieldValue: ''
  258. });
  259. // Clear search results
  260. this._onTypeQuery('');
  261. }
  262. _onFocused: boolean => Function;
  263. /**
  264. * Constructs a callback to be used to update the padding of the field if necessary.
  265. *
  266. * @param {boolean} focused - True of the field is focused.
  267. * @returns {Function}
  268. */
  269. _onFocused(focused) {
  270. return () => {
  271. Platform.OS === 'android' && this.setState({
  272. bottomPadding: focused
  273. });
  274. };
  275. }
  276. _onInvite: () => void
  277. /**
  278. * Invites the selected entries.
  279. *
  280. * @returns {void}
  281. */
  282. _onInvite() {
  283. this._invite(this.state.inviteItems)
  284. .then(invitesLeftToSend => {
  285. if (invitesLeftToSend.length) {
  286. this.setState({
  287. inviteItems: invitesLeftToSend
  288. });
  289. this._showFailedInviteAlert();
  290. } else {
  291. this.props.dispatch(setActiveModalId());
  292. }
  293. });
  294. }
  295. _onPressItem: Item => Function
  296. /**
  297. * Function to prepare a callback for the onPress event of the touchable.
  298. *
  299. * @param {Item} item - The item on which onPress was invoked.
  300. * @returns {Function}
  301. */
  302. _onPressItem(item) {
  303. return () => {
  304. const { inviteItems } = this.state;
  305. const finderKey = item.type === INVITE_TYPES.PHONE ? 'number' : 'user_id';
  306. if (inviteItems.find(
  307. _.matchesProperty(finderKey, item[finderKey]))) {
  308. // Item is already selected, need to unselect it.
  309. this.setState({
  310. inviteItems: inviteItems.filter(
  311. element => item[finderKey] !== element[finderKey])
  312. });
  313. } else {
  314. // Item is not selected yet, need to add to the list.
  315. const items: Array<Object> = inviteItems.concat(item);
  316. this.setState({
  317. inviteItems: _.sortBy(items, [ 'name', 'number' ])
  318. });
  319. }
  320. };
  321. }
  322. _onShareMeeting: () => void
  323. /**
  324. * Shows the system share sheet to share the meeting information.
  325. *
  326. * @returns {void}
  327. */
  328. _onShareMeeting() {
  329. if (this.state.inviteItems.length > 0) {
  330. // The use probably intended to invite people.
  331. this._onInvite();
  332. } else {
  333. this.props.dispatch(beginShareRoom());
  334. }
  335. }
  336. _onTypeQuery: string => void
  337. /**
  338. * Handles the typing event of the text field on the dialog and performs the
  339. * search.
  340. *
  341. * @param {string} query - The query that is typed in the field.
  342. * @returns {void}
  343. */
  344. _onTypeQuery(query) {
  345. this.setState({
  346. fieldValue: query
  347. });
  348. clearTimeout(this.searchTimeout);
  349. this.searchTimeout = setTimeout(() => {
  350. this.setState({
  351. searchInprogress: true
  352. }, () => {
  353. this._performSearch(query);
  354. });
  355. }, 500);
  356. }
  357. /**
  358. * Performs the actual search.
  359. *
  360. * @param {string} query - The query to search for.
  361. * @returns {void}
  362. */
  363. _performSearch(query) {
  364. this._query(query).then(results => {
  365. this.setState({
  366. selectableItems: _.sortBy(results, [ 'name', 'number' ])
  367. });
  368. })
  369. .finally(() => {
  370. this.setState({
  371. searchInprogress: false
  372. }, () => {
  373. this.inputFieldRef && this.inputFieldRef.focus();
  374. });
  375. });
  376. }
  377. _query: (string) => Promise<Array<Object>>;
  378. /**
  379. * Renders a button to clear the text field.
  380. *
  381. * @returns {React#Element<*>}
  382. */
  383. _renderClearButton() {
  384. if (!this.state.fieldValue.length) {
  385. return null;
  386. }
  387. return (
  388. <TouchableOpacity
  389. onPress = { this._onClearField }
  390. style = { styles.clearButton }>
  391. <View style = { styles.clearIconContainer }>
  392. <Icon
  393. src = { IconClose }
  394. style = { styles.clearIcon } />
  395. </View>
  396. </TouchableOpacity>
  397. );
  398. }
  399. _renderInvitedItem: Object => React$Element<any> | null
  400. /**
  401. * Renders a single item in the invited {@code FlatList}.
  402. *
  403. * @param {Object} flatListItem - An item of the data array of the
  404. * {@code FlatList}.
  405. * @param {number} index - The index of the currently rendered item.
  406. * @returns {?React$Element<any>}
  407. */
  408. _renderInvitedItem(flatListItem, index): React$Element<any> | null {
  409. const { item } = flatListItem;
  410. const renderableItem = this._getRenderableItem(flatListItem);
  411. return (
  412. <TouchableOpacity onPress = { this._onPressItem(item) } >
  413. <View
  414. pointerEvents = 'box-only'
  415. style = { styles.itemWrapper }>
  416. <AvatarListItem
  417. avatarOnly = { true }
  418. avatarSize = { AVATAR_SIZE }
  419. avatarStatus = { item.status }
  420. avatarStyle = { styles.avatar }
  421. avatarTextStyle = { styles.avatarText }
  422. item = { renderableItem }
  423. key = { index }
  424. linesStyle = { styles.itemLinesStyle }
  425. titleStyle = { styles.itemText } />
  426. <Icon
  427. src = { IconCancelSelection }
  428. style = { styles.unselectIcon } />
  429. </View>
  430. </TouchableOpacity>
  431. );
  432. }
  433. _renderItem: Object => React$Element<any> | null
  434. /**
  435. * Renders a single item in the search result {@code FlatList}.
  436. *
  437. * @param {Object} flatListItem - An item of the data array of the
  438. * {@code FlatList}.
  439. * @param {number} index - The index of the currently rendered item.
  440. * @returns {?React$Element<*>}
  441. */
  442. _renderItem(flatListItem, index): React$Element<any> | null {
  443. const { item } = flatListItem;
  444. const { inviteItems } = this.state;
  445. let selected = false;
  446. const renderableItem = this._getRenderableItem(flatListItem);
  447. if (!renderableItem) {
  448. return null;
  449. }
  450. switch (item.type) {
  451. case INVITE_TYPES.PHONE:
  452. selected = inviteItems.find(_.matchesProperty('number', item.number));
  453. break;
  454. case INVITE_TYPES.USER:
  455. selected = item.id
  456. ? inviteItems.find(_.matchesProperty('id', item.id))
  457. : inviteItems.find(_.matchesProperty('user_id', item.user_id));
  458. break;
  459. default:
  460. return null;
  461. }
  462. return (
  463. <TouchableOpacity onPress = { this._onPressItem(item) } >
  464. <View
  465. pointerEvents = 'box-only'
  466. style = { styles.itemWrapper }>
  467. <AvatarListItem
  468. avatarSize = { AVATAR_SIZE }
  469. avatarStatus = { item.status }
  470. avatarStyle = { styles.avatar }
  471. avatarTextStyle = { styles.avatarText }
  472. item = { renderableItem }
  473. key = { index }
  474. linesStyle = { styles.itemLinesStyle }
  475. titleStyle = { styles.itemText } />
  476. { selected && <Icon
  477. src = { IconCheck }
  478. style = { styles.selectedIcon } /> }
  479. </View>
  480. </TouchableOpacity>
  481. );
  482. }
  483. _renderSeparator: () => React$Element<*> | null
  484. /**
  485. * Renders the item separator.
  486. *
  487. * @returns {?React$Element<*>}
  488. */
  489. _renderSeparator() {
  490. return (
  491. <View style = { styles.separator } />
  492. );
  493. }
  494. _renderShareMeetingButton: () => React$Element<any>;
  495. /**
  496. * Renders a button to share the meeting info.
  497. *
  498. * @returns {React#Element<*>}
  499. */
  500. _renderShareMeetingButton() {
  501. const { _headerStyles } = this.props;
  502. return (
  503. <SafeAreaView
  504. style = { [
  505. styles.bottomBar,
  506. _headerStyles.headerOverlay,
  507. this.state.bottomPadding ? styles.extraBarPadding : null
  508. ] }>
  509. <TouchableOpacity
  510. onPress = { this._onShareMeeting }>
  511. <Icon
  512. src = { IconShare }
  513. style = { [ _headerStyles.headerButtonText, styles.shareIcon ] } />
  514. </TouchableOpacity>
  515. </SafeAreaView>
  516. );
  517. }
  518. _setFieldRef: ?TextInput => void
  519. /**
  520. * Sets a reference to the input field for later use.
  521. *
  522. * @param {?TextInput} input - The reference to the input field.
  523. * @returns {void}
  524. */
  525. _setFieldRef(input) {
  526. this.inputFieldRef = input;
  527. }
  528. /**
  529. * Shows an alert telling the user that some invitees were failed to be
  530. * invited.
  531. *
  532. * NOTE: We're using an Alert here because we're on a modal and it makes
  533. * using our dialogs a tad more difficult.
  534. *
  535. * @returns {void}
  536. */
  537. _showFailedInviteAlert() {
  538. this.props.dispatch(openDialog(AlertDialog, {
  539. contentKey: {
  540. key: 'inviteDialog.alertText'
  541. }
  542. }));
  543. }
  544. }
  545. /**
  546. * Maps part of the Redux state to the props of this component.
  547. *
  548. * @param {Object} state - The Redux state.
  549. * @returns {{
  550. * _isVisible: boolean
  551. * }}
  552. */
  553. function _mapStateToProps(state: Object) {
  554. return {
  555. ..._abstractMapStateToProps(state),
  556. _headerStyles: ColorSchemeRegistry.get(state, 'Header'),
  557. _isVisible: state['features/base/modal'].activeModalId === ADD_PEOPLE_DIALOG_VIEW_ID
  558. };
  559. }
  560. export default translate(connect(_mapStateToProps)(AddPeopleDialog));