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

AddPeopleDialog.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. autoFocus = { true }
  145. onChangeText = { this._onTypeQuery }
  146. placeholder = {
  147. this.props.t(`inviteDialog.${placeholderKey}`)
  148. }
  149. ref = { this._setFieldRef }
  150. style = { styles.searchField } />
  151. </View>
  152. <FlatList
  153. ItemSeparatorComponent = { this._renderSeparator }
  154. data = { this.state.selectableItems }
  155. extraData = { inviteItems }
  156. keyExtractor = { this._keyExtractor }
  157. keyboardShouldPersistTaps = 'always'
  158. renderItem = { this._renderItem }
  159. style = { styles.resultList } />
  160. </SafeAreaView>
  161. </Modal>
  162. );
  163. }
  164. /**
  165. * Clears the dialog content.
  166. *
  167. * @returns {void}
  168. */
  169. _clearState() {
  170. this.setState(this.defaultState);
  171. }
  172. _invite: Array<Object> => Promise<Array<Object>>
  173. _isAddDisabled: () => boolean;
  174. _keyExtractor: Object => string
  175. /**
  176. * Key extractor for the flatlist.
  177. *
  178. * @param {Object} item - The flatlist item that we need the key to be
  179. * generated for.
  180. * @returns {string}
  181. */
  182. _keyExtractor(item) {
  183. return item.type === 'user' ? item.user_id : item.number;
  184. }
  185. _onCloseAddPeopleDialog: () => void
  186. /**
  187. * Closes the dialog.
  188. *
  189. * @returns {void}
  190. */
  191. _onCloseAddPeopleDialog() {
  192. this.props.dispatch(setAddPeopleDialogVisible(false));
  193. }
  194. _onInvite: () => void
  195. /**
  196. * Invites the selected entries.
  197. *
  198. * @returns {void}
  199. */
  200. _onInvite() {
  201. this._invite(this.state.inviteItems)
  202. .then(invitesLeftToSend => {
  203. if (invitesLeftToSend.length) {
  204. this.setState({
  205. inviteItems: invitesLeftToSend
  206. });
  207. this._showFailedInviteAlert();
  208. } else {
  209. this._onCloseAddPeopleDialog();
  210. }
  211. });
  212. }
  213. _onPressItem: Item => Function
  214. /**
  215. * Function to preapre a callback for the onPress event of the touchable.
  216. *
  217. * @param {Item} item - The item on which onPress was invoked.
  218. * @returns {Function}
  219. */
  220. _onPressItem(item) {
  221. return () => {
  222. const { inviteItems } = this.state;
  223. const finderKey = item.type === 'phone' ? 'number' : 'user_id';
  224. if (inviteItems.find(
  225. _.matchesProperty(finderKey, item[finderKey]))) {
  226. // Item is already selected, need to unselect it.
  227. this.setState({
  228. inviteItems: inviteItems.filter(
  229. element => item[finderKey] !== element[finderKey])
  230. });
  231. } else {
  232. // Item is not selected yet, need to add to the list.
  233. const items: Array<*> = inviteItems.concat(item);
  234. this.setState({
  235. // $FlowExpectedError
  236. inviteItems: _.orderBy(items, [ 'name' ], [ 'asc' ])
  237. });
  238. }
  239. };
  240. }
  241. _onTypeQuery: string => void
  242. /**
  243. * Handles the typing event of the text field on the dialog and performs the
  244. * search.
  245. *
  246. * @param {string} query - The query that is typed in the field.
  247. * @returns {void}
  248. */
  249. _onTypeQuery(query) {
  250. clearTimeout(this.searchTimeout);
  251. this.searchTimeout = setTimeout(() => {
  252. this.setState({
  253. searchInprogress: true
  254. }, () => {
  255. this._performSearch(query);
  256. });
  257. }, 500);
  258. }
  259. /**
  260. * Performs the actual search.
  261. *
  262. * @param {string} query - The query to search for.
  263. * @returns {void}
  264. */
  265. _performSearch(query) {
  266. this._query(query).then(results => {
  267. const { inviteItems } = this.state;
  268. let selectableItems = results.filter(result => {
  269. switch (result.type) {
  270. case 'phone':
  271. return result.allowed && result.number
  272. && !inviteItems.find(
  273. _.matchesProperty('number', result.number));
  274. case 'user':
  275. return result.user_id && !inviteItems.find(
  276. _.matchesProperty('user_id', result.user_id));
  277. default:
  278. return false;
  279. }
  280. });
  281. const items = this.state.inviteItems.concat(selectableItems);
  282. // $FlowExpectedError
  283. selectableItems = _.orderBy(items, [ 'name' ], [ 'asc' ]);
  284. this.setState({
  285. selectableItems
  286. });
  287. })
  288. .finally(() => {
  289. this.setState({
  290. searchInprogress: false
  291. }, () => {
  292. this.inputFieldRef && this.inputFieldRef.focus();
  293. });
  294. });
  295. }
  296. _query: (string) => Promise<Array<Object>>;
  297. _renderItem: Object => ?React$Element<*>
  298. /**
  299. * Renders a single item in the {@code FlatList}.
  300. *
  301. * @param {Object} flatListItem - An item of the data array of the
  302. * {@code FlatList}.
  303. * @param {number} index - The index of the currently rendered item.
  304. * @returns {?React$Element<*>}
  305. */
  306. _renderItem(flatListItem, index) {
  307. const { item } = flatListItem;
  308. const { inviteItems } = this.state;
  309. let selected = false;
  310. let renderableItem;
  311. switch (item.type) {
  312. case 'phone':
  313. selected
  314. = inviteItems.find(_.matchesProperty('number', item.number));
  315. renderableItem = {
  316. avatar: 'phone',
  317. key: item.number,
  318. title: item.number
  319. };
  320. break;
  321. case 'user':
  322. selected
  323. = inviteItems.find(_.matchesProperty('user_id', item.user_id));
  324. renderableItem = {
  325. avatar: item.avatar,
  326. key: item.user_id,
  327. title: item.name
  328. };
  329. break;
  330. default:
  331. return null;
  332. }
  333. return (
  334. <TouchableOpacity onPress = { this._onPressItem(item) } >
  335. <View
  336. pointerEvents = 'box-only'
  337. style = { styles.itemWrapper }>
  338. <Icon
  339. name = { selected
  340. ? 'radio_button_checked'
  341. : 'radio_button_unchecked' }
  342. style = { styles.radioButton } />
  343. <AvatarListItem
  344. avatarSize = { AVATAR_SIZE }
  345. avatarStyle = { styles.avatar }
  346. avatarTextStyle = { styles.avatarText }
  347. item = { renderableItem }
  348. key = { index }
  349. linesStyle = { styles.itemLinesStyle }
  350. titleStyle = { styles.itemText } />
  351. </View>
  352. </TouchableOpacity>
  353. );
  354. }
  355. _renderSeparator: () => React$Element<*> | null
  356. /**
  357. * Renders the item separator.
  358. *
  359. * @returns {?React$Element<*>}
  360. */
  361. _renderSeparator() {
  362. return (
  363. <View style = { styles.separator } />
  364. );
  365. }
  366. _setFieldRef: ?TextInput => void
  367. /**
  368. * Sets a reference to the input field for later use.
  369. *
  370. * @param {?TextInput} input - The reference to the input field.
  371. * @returns {void}
  372. */
  373. _setFieldRef(input) {
  374. this.inputFieldRef = input;
  375. }
  376. /**
  377. * Shows an alert telling the user that some invitees were failed to be
  378. * invited.
  379. *
  380. * NOTE: We're using an Alert here because we're on a modal and it makes
  381. * using our dialogs a tad more difficult.
  382. *
  383. * @returns {void}
  384. */
  385. _showFailedInviteAlert() {
  386. const { t } = this.props;
  387. Alert.alert(
  388. t('inviteDialog.alertTitle'),
  389. t('inviteDialog.alertText'),
  390. [
  391. {
  392. text: t('inviteDialog.alertOk')
  393. }
  394. ]
  395. );
  396. }
  397. }
  398. /**
  399. * Maps part of the Redux state to the props of this component.
  400. *
  401. * @param {Object} state - The Redux state.
  402. * @returns {{
  403. * _isVisible: boolean
  404. * }}
  405. */
  406. function _mapStateToProps(state: Object) {
  407. return {
  408. ..._abstractMapStateToProps(state),
  409. _isVisible: state['features/invite'].inviteDialogVisible
  410. };
  411. }
  412. export default translate(connect(_mapStateToProps)(AddPeopleDialog));