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

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