您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AddPeopleDialog.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. // @flow
  2. import _ from 'lodash';
  3. import React from 'react';
  4. import {
  5. ActivityIndicator,
  6. Alert,
  7. FlatList,
  8. SafeAreaView,
  9. TextInput,
  10. TouchableOpacity,
  11. View
  12. } from 'react-native';
  13. import { Icon } from '../../../../base/font-icons';
  14. import { translate } from '../../../../base/i18n';
  15. import {
  16. AvatarListItem,
  17. HeaderWithNavigation,
  18. Modal,
  19. type Item
  20. } from '../../../../base/react';
  21. import { connect } from '../../../../base/redux';
  22. import { setAddPeopleDialogVisible } from '../../../actions';
  23. import AbstractAddPeopleDialog, {
  24. type Props as AbstractProps,
  25. type State as AbstractState,
  26. _mapStateToProps as _abstractMapStateToProps
  27. } from '../AbstractAddPeopleDialog';
  28. import styles, {
  29. AVATAR_SIZE,
  30. DARK_GREY
  31. } from './styles';
  32. type Props = AbstractProps & {
  33. /**
  34. * True if the invite dialog should be open, false otherwise.
  35. */
  36. _isVisible: boolean,
  37. /**
  38. * Function used to translate i18n labels.
  39. */
  40. t: Function
  41. };
  42. type State = AbstractState & {
  43. /**
  44. * True if a search is in progress, false otherwise.
  45. */
  46. searchInprogress: boolean,
  47. /**
  48. * An array of items that are selectable on this dialog. This is usually
  49. * populated by an async search.
  50. */
  51. selectableItems: Array<Object>
  52. };
  53. /**
  54. * Implements a special dialog to invite people from a directory service.
  55. */
  56. class AddPeopleDialog extends AbstractAddPeopleDialog<Props, State> {
  57. /**
  58. * Default state object to reset the state to when needed.
  59. */
  60. defaultState = {
  61. addToCallError: false,
  62. addToCallInProgress: false,
  63. inviteItems: [],
  64. searchInprogress: false,
  65. selectableItems: []
  66. };
  67. /**
  68. * Ref of the search field.
  69. */
  70. inputFieldRef: ?TextInput;
  71. /**
  72. * TimeoutID to delay the search for the time the user is probably typing.
  73. */
  74. searchTimeout: TimeoutID;
  75. /**
  76. * Contrustor of the component.
  77. *
  78. * @inheritdoc
  79. */
  80. constructor(props: Props) {
  81. super(props);
  82. this.state = this.defaultState;
  83. this._keyExtractor = this._keyExtractor.bind(this);
  84. this._renderItem = this._renderItem.bind(this);
  85. this._renderSeparator = this._renderSeparator.bind(this);
  86. this._onCloseAddPeopleDialog = this._onCloseAddPeopleDialog.bind(this);
  87. this._onInvite = this._onInvite.bind(this);
  88. this._onPressItem = this._onPressItem.bind(this);
  89. this._onTypeQuery = this._onTypeQuery.bind(this);
  90. this._setFieldRef = this._setFieldRef.bind(this);
  91. }
  92. /**
  93. * Implements {@code Component#componentDidUpdate}.
  94. *
  95. * @inheritdoc
  96. */
  97. componentDidUpdate(prevProps) {
  98. if (prevProps._isVisible !== this.props._isVisible) {
  99. // Clear state
  100. this._clearState();
  101. }
  102. }
  103. /**
  104. * Implements {@code Component#render}.
  105. *
  106. * @inheritdoc
  107. */
  108. render() {
  109. const {
  110. _addPeopleEnabled,
  111. _dialOutEnabled
  112. } = this.props;
  113. const { inviteItems } = this.state;
  114. let placeholderKey = 'searchPlaceholder';
  115. if (!_addPeopleEnabled) {
  116. placeholderKey = 'searchCallOnlyPlaceholder';
  117. } else if (!_dialOutEnabled) {
  118. placeholderKey = 'searchPeopleOnlyPlaceholder';
  119. }
  120. return (
  121. <Modal
  122. onRequestClose = { this._onCloseAddPeopleDialog }
  123. visible = { this.props._isVisible }>
  124. <HeaderWithNavigation
  125. forwardDisabled = { this._isAddDisabled() }
  126. forwardLabelKey = 'inviteDialog.send'
  127. headerLabelKey = 'inviteDialog.header'
  128. onPressBack = { this._onCloseAddPeopleDialog }
  129. onPressForward = { this._onInvite } />
  130. <SafeAreaView style = { styles.dialogWrapper }>
  131. <View
  132. style = { styles.searchFieldWrapper }>
  133. <View style = { styles.searchIconWrapper }>
  134. { this.state.searchInprogress
  135. ? <ActivityIndicator
  136. color = { DARK_GREY }
  137. size = 'small' />
  138. : <Icon
  139. name = { 'search' }
  140. style = { styles.searchIcon } />}
  141. </View>
  142. <TextInput
  143. autoCorrect = { false }
  144. onChangeText = { this._onTypeQuery }
  145. placeholder = {
  146. this.props.t(`inviteDialog.${placeholderKey}`)
  147. }
  148. ref = { this._setFieldRef }
  149. style = { styles.searchField } />
  150. </View>
  151. <FlatList
  152. ItemSeparatorComponent = { this._renderSeparator }
  153. data = { this.state.selectableItems }
  154. extraData = { inviteItems }
  155. keyExtractor = { this._keyExtractor }
  156. renderItem = { this._renderItem }
  157. style = { styles.resultList } />
  158. </SafeAreaView>
  159. </Modal>
  160. );
  161. }
  162. /**
  163. * Clears the dialog content.
  164. *
  165. * @returns {void}
  166. */
  167. _clearState() {
  168. this.setState(this.defaultState);
  169. }
  170. _invite: Array<Object> => Promise<Array<Object>>
  171. _isAddDisabled: () => boolean;
  172. _keyExtractor: Object => string
  173. /**
  174. * Key extractor for the flatlist.
  175. *
  176. * @param {Object} item - The flatlist item that we need the key to be
  177. * generated for.
  178. * @returns {string}
  179. */
  180. _keyExtractor(item) {
  181. return item.type === 'user' ? item.user_id : item.number;
  182. }
  183. _onCloseAddPeopleDialog: () => void
  184. /**
  185. * Closes the dialog.
  186. *
  187. * @returns {void}
  188. */
  189. _onCloseAddPeopleDialog() {
  190. this.props.dispatch(setAddPeopleDialogVisible(false));
  191. }
  192. _onInvite: () => void
  193. /**
  194. * Invites the selected entries.
  195. *
  196. * @returns {void}
  197. */
  198. _onInvite() {
  199. this._invite(this.state.inviteItems)
  200. .then(invitesLeftToSend => {
  201. if (invitesLeftToSend.length) {
  202. this.setState({
  203. inviteItems: invitesLeftToSend
  204. });
  205. this._showFailedInviteAlert();
  206. } else {
  207. this._onCloseAddPeopleDialog();
  208. }
  209. });
  210. }
  211. _onPressItem: Item => Function
  212. /**
  213. * Function to preapre a callback for the onPress event of the touchable.
  214. *
  215. * @param {Item} item - The item on which onPress was invoked.
  216. * @returns {Function}
  217. */
  218. _onPressItem(item) {
  219. return () => {
  220. const { inviteItems } = this.state;
  221. const finderKey = item.type === 'phone' ? 'number' : 'user_id';
  222. if (inviteItems.find(
  223. _.matchesProperty(finderKey, item[finderKey]))) {
  224. // Item is already selected, need to unselect it.
  225. this.setState({
  226. inviteItems: inviteItems.filter(
  227. element => item[finderKey] !== element[finderKey])
  228. });
  229. } else {
  230. // Item is not selected yet, need to add to the list.
  231. const items: Array<*> = inviteItems.concat(item);
  232. this.setState({
  233. // $FlowExpectedError
  234. inviteItems: _.orderBy(items, [ 'name' ], [ 'asc' ])
  235. });
  236. }
  237. };
  238. }
  239. _onTypeQuery: string => void
  240. /**
  241. * Handles the typing event of the text field on the dialog and performs the
  242. * search.
  243. *
  244. * @param {string} query - The query that is typed in the field.
  245. * @returns {void}
  246. */
  247. _onTypeQuery(query) {
  248. clearTimeout(this.searchTimeout);
  249. this.searchTimeout = setTimeout(() => {
  250. this.setState({
  251. searchInprogress: true
  252. }, () => {
  253. this._performSearch(query);
  254. });
  255. }, 500);
  256. }
  257. /**
  258. * Performs the actual search.
  259. *
  260. * @param {string} query - The query to search for.
  261. * @returns {void}
  262. */
  263. _performSearch(query) {
  264. this._query(query).then(results => {
  265. const { inviteItems } = this.state;
  266. let selectableItems = results.filter(result => {
  267. switch (result.type) {
  268. case 'phone':
  269. return result.allowed && result.number
  270. && !inviteItems.find(
  271. _.matchesProperty('number', result.number));
  272. case 'user':
  273. return result.user_id && !inviteItems.find(
  274. _.matchesProperty('user_id', result.user_id));
  275. default:
  276. return false;
  277. }
  278. });
  279. const items = this.state.inviteItems.concat(selectableItems);
  280. // $FlowExpectedError
  281. selectableItems = _.orderBy(items, [ 'name' ], [ 'asc' ]);
  282. this.setState({
  283. selectableItems
  284. });
  285. })
  286. .finally(() => {
  287. this.setState({
  288. searchInprogress: false
  289. }, () => {
  290. this.inputFieldRef && this.inputFieldRef.focus();
  291. });
  292. });
  293. }
  294. _query: (string) => Promise<Array<Object>>;
  295. _renderItem: Object => ?React$Element<*>
  296. /**
  297. * Renders a single item in the {@code FlatList}.
  298. *
  299. * @param {Object} flatListItem - An item of the data array of the
  300. * {@code FlatList}.
  301. * @param {number} index - The index of the currently rendered item.
  302. * @returns {?React$Element<*>}
  303. */
  304. _renderItem(flatListItem, index) {
  305. const { item } = flatListItem;
  306. const { inviteItems } = this.state;
  307. let selected = false;
  308. let renderableItem;
  309. switch (item.type) {
  310. case 'phone':
  311. selected
  312. = inviteItems.find(_.matchesProperty('number', item.number));
  313. renderableItem = {
  314. avatar: 'phone',
  315. key: item.number,
  316. title: item.number
  317. };
  318. break;
  319. case 'user':
  320. selected
  321. = inviteItems.find(_.matchesProperty('user_id', item.user_id));
  322. renderableItem = {
  323. avatar: item.avatar,
  324. key: item.user_id,
  325. title: item.name
  326. };
  327. break;
  328. default:
  329. return null;
  330. }
  331. return (
  332. <TouchableOpacity onPress = { this._onPressItem(item) } >
  333. <View
  334. pointerEvents = 'box-only'
  335. style = { styles.itemWrapper }>
  336. <Icon
  337. name = { selected
  338. ? 'radio_button_checked'
  339. : 'radio_button_unchecked' }
  340. style = { styles.radioButton } />
  341. <AvatarListItem
  342. avatarSize = { AVATAR_SIZE }
  343. avatarStyle = { styles.avatar }
  344. avatarTextStyle = { styles.avatarText }
  345. item = { renderableItem }
  346. key = { index }
  347. linesStyle = { styles.itemLinesStyle }
  348. titleStyle = { styles.itemText } />
  349. </View>
  350. </TouchableOpacity>
  351. );
  352. }
  353. _renderSeparator: () => React$Element<*> | null
  354. /**
  355. * Renders the item separator.
  356. *
  357. * @returns {?React$Element<*>}
  358. */
  359. _renderSeparator() {
  360. return (
  361. <View style = { styles.separator } />
  362. );
  363. }
  364. _setFieldRef: ?TextInput => void
  365. /**
  366. * Sets a reference to the input field for later use.
  367. *
  368. * @param {?TextInput} input - The reference to the input field.
  369. * @returns {void}
  370. */
  371. _setFieldRef(input) {
  372. this.inputFieldRef = input;
  373. }
  374. /**
  375. * Shows an alert telling the user that some invitees were failed to be
  376. * invited.
  377. *
  378. * NOTE: We're using an Alert here because we're on a modal and it makes
  379. * using our dialogs a tad more difficult.
  380. *
  381. * @returns {void}
  382. */
  383. _showFailedInviteAlert() {
  384. const { t } = this.props;
  385. Alert.alert(
  386. t('inviteDialog.alertTitle'),
  387. t('inviteDialog.alertText'),
  388. [
  389. {
  390. text: t('inviteDialog.alertOk')
  391. }
  392. ]
  393. );
  394. }
  395. }
  396. /**
  397. * Maps part of the Redux state to the props of this component.
  398. *
  399. * @param {Object} state - The Redux state.
  400. * @returns {{
  401. * _isVisible: boolean
  402. * }}
  403. */
  404. function _mapStateToProps(state: Object) {
  405. return {
  406. ..._abstractMapStateToProps(state),
  407. _isVisible: state['features/invite'].inviteDialogVisible
  408. };
  409. }
  410. export default translate(connect(_mapStateToProps)(AddPeopleDialog));