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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 { 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 HeaderNavigationButton
  28. from '../../../../mobile/navigation/components/HeaderNavigationButton';
  29. import ClearableInput from '../../../../participants-pane/components/native/ClearableInput';
  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. * TimeoutID to delay the search for the time the user is probably typing.
  96. */
  97. searchTimeout: TimeoutID;
  98. /**
  99. * Contrustor of the component.
  100. *
  101. * @inheritdoc
  102. */
  103. constructor(props: Props) {
  104. super(props);
  105. this.state = this.defaultState;
  106. this._keyExtractor = this._keyExtractor.bind(this);
  107. this._renderInvitedItem = this._renderInvitedItem.bind(this);
  108. this._renderItem = this._renderItem.bind(this);
  109. this._renderSeparator = this._renderSeparator.bind(this);
  110. this._onClearField = this._onClearField.bind(this);
  111. this._onInvite = this._onInvite.bind(this);
  112. this._onPressItem = this._onPressItem.bind(this);
  113. this._onShareMeeting = this._onShareMeeting.bind(this);
  114. this._onTypeQuery = this._onTypeQuery.bind(this);
  115. this._renderShareMeetingButton = this._renderShareMeetingButton.bind(this);
  116. }
  117. /**
  118. * Implements React's {@link Component#componentDidMount()}. Invoked
  119. * immediately after this component is mounted.
  120. *
  121. * @inheritdoc
  122. * @returns {void}
  123. */
  124. componentDidMount() {
  125. const { navigation, t } = this.props;
  126. navigation.setOptions({
  127. headerRight: () => (
  128. <HeaderNavigationButton
  129. disabled = { this._isAddDisabled() }
  130. label = { t('inviteDialog.send') }
  131. twoActions = { true } />
  132. )
  133. });
  134. }
  135. /**
  136. * Implements {@code Component#componentDidUpdate}.
  137. *
  138. * @inheritdoc
  139. */
  140. componentDidUpdate(prevProps) {
  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. twoActions = { true } />
  150. )
  151. });
  152. if (prevProps._isVisible !== this.props._isVisible) {
  153. // Clear state
  154. this._clearState();
  155. }
  156. }
  157. /**
  158. * Implements {@code Component#render}.
  159. *
  160. * @inheritdoc
  161. */
  162. render() {
  163. const {
  164. _addPeopleEnabled,
  165. _dialOutEnabled
  166. } = this.props;
  167. const { inviteItems, selectableItems } = this.state;
  168. const { theme } = this.props;
  169. const { palette } = theme;
  170. let placeholderKey = 'searchPlaceholder';
  171. if (!_addPeopleEnabled) {
  172. placeholderKey = 'searchCallOnlyPlaceholder';
  173. } else if (!_dialOutEnabled) {
  174. placeholderKey = 'searchPeopleOnlyPlaceholder';
  175. }
  176. return (
  177. <JitsiScreen
  178. footerComponent = { this._renderShareMeetingButton }
  179. style = { styles.addPeopleContainer }>
  180. <ClearableInput
  181. autoFocus = { false }
  182. customStyles = {{
  183. wrapper: styles.searchFieldWrapper,
  184. input: styles.searchField,
  185. clearButton: styles.clearButton,
  186. clearIcon: styles.clearIcon
  187. }}
  188. onChange = { this._onTypeQuery }
  189. placeholder = { this.props.t(`inviteDialog.${placeholderKey}`) }
  190. placeholderColor = { palette.text04 }
  191. prefixComponent = { <View style = { styles.searchIconWrapper }>
  192. {this.state.searchInprogress
  193. ? <ActivityIndicator
  194. color = { DARK_GREY }
  195. size = 'small' />
  196. : <Icon
  197. src = { IconSearch }
  198. style = { styles.searchIcon } />}
  199. </View> }
  200. value = { this.state.fieldValue } />
  201. { Boolean(inviteItems.length) && <View style = { styles.invitedList }>
  202. <FlatList
  203. data = { inviteItems }
  204. horizontal = { true }
  205. keyExtractor = { this._keyExtractor }
  206. renderItem = { this._renderInvitedItem } />
  207. </View> }
  208. <View style = { styles.resultList }>
  209. <FlatList
  210. ItemSeparatorComponent = { this._renderSeparator }
  211. data = { selectableItems }
  212. extraData = { inviteItems }
  213. keyExtractor = { this._keyExtractor }
  214. renderItem = { this._renderItem } />
  215. </View>
  216. </JitsiScreen>
  217. );
  218. }
  219. /**
  220. * Clears the dialog content.
  221. *
  222. * @returns {void}
  223. */
  224. _clearState() {
  225. this.setState(this.defaultState);
  226. }
  227. /**
  228. * Returns an object capable of being rendered by an {@code AvatarListItem}.
  229. *
  230. * @param {Object} flatListItem - An item of the data array of the {@code FlatList}.
  231. * @returns {?Object}
  232. */
  233. _getRenderableItem(flatListItem) {
  234. const { item } = flatListItem;
  235. switch (item.type) {
  236. case INVITE_TYPES.PHONE:
  237. return {
  238. avatar: IconPhone,
  239. key: item.number,
  240. title: item.number
  241. };
  242. case INVITE_TYPES.USER:
  243. return {
  244. avatar: item.avatar,
  245. key: item.id || item.user_id,
  246. title: item.name
  247. };
  248. default:
  249. return null;
  250. }
  251. }
  252. _invite: Array<Object> => Promise<Array<Object>>;
  253. _isAddDisabled: () => boolean;
  254. _keyExtractor: Object => string;
  255. /**
  256. * Key extractor for the flatlist.
  257. *
  258. * @param {Object} item - The flatlist item that we need the key to be
  259. * generated for.
  260. * @returns {string}
  261. */
  262. _keyExtractor(item) {
  263. return item.type === INVITE_TYPES.USER ? item.id || item.user_id : item.number;
  264. }
  265. _onClearField: () => void;
  266. /**
  267. * Callback to clear the text field.
  268. *
  269. * @returns {void}
  270. */
  271. _onClearField() {
  272. this.setState({
  273. fieldValue: ''
  274. });
  275. // Clear search results
  276. this._onTypeQuery('');
  277. }
  278. _onInvite: () => void;
  279. /**
  280. * Invites the selected entries.
  281. *
  282. * @returns {void}
  283. */
  284. _onInvite() {
  285. this._invite(this.state.inviteItems)
  286. .then(invitesLeftToSend => {
  287. if (invitesLeftToSend.length) {
  288. this.setState({
  289. inviteItems: invitesLeftToSend
  290. });
  291. this._showFailedInviteAlert();
  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. });
  374. }
  375. _query: (string) => Promise<Array<Object>>;
  376. _renderInvitedItem: Object => React$Element<any> | null;
  377. /**
  378. * Renders a single item in the invited {@code FlatList}.
  379. *
  380. * @param {Object} flatListItem - An item of the data array of the
  381. * {@code FlatList}.
  382. * @param {number} index - The index of the currently rendered item.
  383. * @returns {?React$Element<any>}
  384. */
  385. _renderInvitedItem(flatListItem, index): React$Element<any> | null {
  386. const { item } = flatListItem;
  387. const renderableItem = this._getRenderableItem(flatListItem);
  388. return (
  389. <TouchableOpacity onPress = { this._onPressItem(item) } >
  390. <View
  391. pointerEvents = 'box-only'
  392. style = { styles.itemWrapper }>
  393. <AvatarListItem
  394. avatarOnly = { true }
  395. avatarSize = { AVATAR_SIZE }
  396. avatarStatus = { item.status }
  397. avatarStyle = { styles.avatar }
  398. avatarTextStyle = { styles.avatarText }
  399. item = { renderableItem }
  400. key = { index }
  401. linesStyle = { styles.itemLinesStyle }
  402. titleStyle = { styles.itemText } />
  403. <Icon
  404. src = { IconCancelSelection }
  405. style = { styles.unselectIcon } />
  406. </View>
  407. </TouchableOpacity>
  408. );
  409. }
  410. _renderItem: Object => React$Element<any> | null;
  411. /**
  412. * Renders a single item in the search result {@code FlatList}.
  413. *
  414. * @param {Object} flatListItem - An item of the data array of the
  415. * {@code FlatList}.
  416. * @param {number} index - The index of the currently rendered item.
  417. * @returns {?React$Element<*>}
  418. */
  419. _renderItem(flatListItem, index): React$Element<any> | null {
  420. const { item } = flatListItem;
  421. const { inviteItems } = this.state;
  422. let selected = false;
  423. const renderableItem = this._getRenderableItem(flatListItem);
  424. if (!renderableItem) {
  425. return null;
  426. }
  427. switch (item.type) {
  428. case INVITE_TYPES.PHONE:
  429. selected = inviteItems.find(_.matchesProperty('number', item.number));
  430. break;
  431. case INVITE_TYPES.USER:
  432. selected = item.id
  433. ? inviteItems.find(_.matchesProperty('id', item.id))
  434. : inviteItems.find(_.matchesProperty('user_id', item.user_id));
  435. break;
  436. default:
  437. return null;
  438. }
  439. return (
  440. <TouchableOpacity onPress = { this._onPressItem(item) } >
  441. <View
  442. pointerEvents = 'box-only'
  443. style = { styles.itemWrapper }>
  444. <AvatarListItem
  445. avatarSize = { AVATAR_SIZE }
  446. avatarStatus = { item.status }
  447. avatarStyle = { styles.avatar }
  448. avatarTextStyle = { styles.avatarText }
  449. item = { renderableItem }
  450. key = { index }
  451. linesStyle = { styles.itemLinesStyle }
  452. titleStyle = { styles.itemText } />
  453. { selected && <Icon
  454. src = { IconCheck }
  455. style = { styles.selectedIcon } /> }
  456. </View>
  457. </TouchableOpacity>
  458. );
  459. }
  460. _renderSeparator: () => React$Element<*> | null;
  461. /**
  462. * Renders the item separator.
  463. *
  464. * @returns {?React$Element<*>}
  465. */
  466. _renderSeparator() {
  467. return (
  468. <View style = { styles.separator } />
  469. );
  470. }
  471. _renderShareMeetingButton: () => React$Element<any>;
  472. /**
  473. * Renders a button to share the meeting info.
  474. *
  475. * @returns {React#Element<*>}
  476. */
  477. _renderShareMeetingButton() {
  478. return (
  479. <View
  480. style = { [
  481. styles.bottomBar,
  482. this.state.bottomPadding ? styles.extraBarPadding : null
  483. ] }>
  484. <TouchableOpacity
  485. onPress = { this._onShareMeeting }>
  486. <Icon
  487. src = { IconShare }
  488. style = { styles.shareIcon } />
  489. </TouchableOpacity>
  490. </View>
  491. );
  492. }
  493. /**
  494. * Shows an alert telling the user that some invitees were failed to be
  495. * invited.
  496. *
  497. * NOTE: We're using an Alert here because we're on a modal and it makes
  498. * using our dialogs a tad more difficult.
  499. *
  500. * @returns {void}
  501. */
  502. _showFailedInviteAlert() {
  503. this.props.dispatch(openDialog(AlertDialog, {
  504. contentKey: {
  505. key: 'inviteDialog.alertText'
  506. }
  507. }));
  508. }
  509. }
  510. /**
  511. * Maps part of the Redux state to the props of this component.
  512. *
  513. * @param {Object} state - The Redux state.
  514. * @returns {{
  515. * _isVisible: boolean
  516. * }}
  517. */
  518. function _mapStateToProps(state: Object) {
  519. return {
  520. ..._abstractMapStateToProps(state)
  521. };
  522. }
  523. export default translate(connect(_mapStateToProps)(withTheme(AddPeopleDialog)));