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.tsx 17KB

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