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

AddPeopleDialog.js 13KB

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