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

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