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

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