Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AddPeopleDialog.tsx 17KB

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