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.

AbstractAddPeopleDialog.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // @flow
  2. import { Component } from 'react';
  3. import { createInviteDialogEvent, sendAnalytics } from '../../../analytics';
  4. import {
  5. NOTIFICATION_TIMEOUT,
  6. showNotification
  7. } from '../../../notifications';
  8. import { invite } from '../../actions';
  9. import {
  10. getInviteResultsForQuery,
  11. getInviteTypeCounts,
  12. isAddPeopleEnabled,
  13. isDialOutEnabled,
  14. isSipInviteEnabled
  15. } from '../../functions';
  16. import logger from '../../logger';
  17. export type Props = {
  18. /**
  19. * Whether or not to show Add People functionality.
  20. */
  21. _addPeopleEnabled: boolean,
  22. /**
  23. * Whether or not call flows are enabled.
  24. */
  25. _callFlowsEnabled: boolean,
  26. /**
  27. * The URL for validating if a phone number can be called.
  28. */
  29. _dialOutAuthUrl: string,
  30. /**
  31. * Whether or not to show Dial Out functionality.
  32. */
  33. _dialOutEnabled: boolean,
  34. /**
  35. * Whether or not to allow sip invites.
  36. */
  37. _sipInviteEnabled: boolean,
  38. /**
  39. * The JWT token.
  40. */
  41. _jwt: string,
  42. /**
  43. * The query types used when searching people.
  44. */
  45. _peopleSearchQueryTypes: Array<string>,
  46. /**
  47. * The URL pointing to the service allowing for people search.
  48. */
  49. _peopleSearchUrl: string,
  50. /**
  51. * The Redux dispatch function.
  52. */
  53. dispatch: Function
  54. };
  55. export type State = {
  56. /**
  57. * Indicating that an error occurred when adding people to the call.
  58. */
  59. addToCallError: boolean,
  60. /**
  61. * Indicating that we're currently adding the new people to the
  62. * call.
  63. */
  64. addToCallInProgress: boolean,
  65. /**
  66. * The list of invite items.
  67. */
  68. inviteItems: Array<Object>,
  69. };
  70. /**
  71. * Implements an abstract dialog to invite people to the conference.
  72. */
  73. export default class AbstractAddPeopleDialog<P: Props, S: State>
  74. extends Component<P, S> {
  75. /**
  76. * Constructor of the component.
  77. *
  78. * @inheritdoc
  79. */
  80. constructor(props: P) {
  81. super(props);
  82. this._query = this._query.bind(this);
  83. }
  84. /**
  85. * Invite people and numbers to the conference. The logic works by inviting
  86. * numbers, people/rooms, sip endpoints and videosipgw in parallel. All invitees are
  87. * stored in an array. As each invite succeeds, the invitee is removed
  88. * from the array. After all invites finish, close the modal if there are
  89. * no invites left to send. If any are left, that means an invite failed
  90. * and an error state should display.
  91. *
  92. * @param {Array<Object>} invitees - The items to be invited.
  93. * @returns {Promise<Array<Object>>}
  94. */
  95. _invite(invitees) {
  96. const inviteTypeCounts = getInviteTypeCounts(invitees);
  97. sendAnalytics(createInviteDialogEvent(
  98. 'clicked', 'inviteButton', {
  99. ...inviteTypeCounts,
  100. inviteAllowed: this._isAddDisabled()
  101. }));
  102. if (this._isAddDisabled()) {
  103. return Promise.resolve([]);
  104. }
  105. this.setState({
  106. addToCallInProgress: true
  107. });
  108. const { _callFlowsEnabled, dispatch } = this.props;
  109. return dispatch(invite(invitees))
  110. .then(invitesLeftToSend => {
  111. this.setState({
  112. addToCallInProgress: false
  113. });
  114. // If any invites are left that means something failed to send
  115. // so treat it as an error.
  116. if (invitesLeftToSend.length) {
  117. const erroredInviteTypeCounts
  118. = getInviteTypeCounts(invitesLeftToSend);
  119. logger.error(`${invitesLeftToSend.length} invites failed`,
  120. erroredInviteTypeCounts);
  121. sendAnalytics(createInviteDialogEvent(
  122. 'error', 'invite', {
  123. ...erroredInviteTypeCounts
  124. }));
  125. this.setState({
  126. addToCallError: true
  127. });
  128. } else if (!_callFlowsEnabled) {
  129. const invitedCount = invitees.length;
  130. let notificationProps;
  131. if (invitedCount >= 3) {
  132. notificationProps = {
  133. titleArguments: {
  134. name: invitees[0].name || invitees[0].address,
  135. count: invitedCount - 1
  136. },
  137. titleKey: 'notify.invitedThreePlusMembers'
  138. };
  139. } else if (invitedCount === 2) {
  140. notificationProps = {
  141. titleArguments: {
  142. first: invitees[0].name || invitees[0].address,
  143. second: invitees[1].name || invitees[1].address
  144. },
  145. titleKey: 'notify.invitedTwoMembers'
  146. };
  147. } else if (invitedCount) {
  148. notificationProps = {
  149. titleArguments: {
  150. name: invitees[0].name || invitees[0].address
  151. },
  152. titleKey: 'notify.invitedOneMember'
  153. };
  154. }
  155. if (notificationProps) {
  156. dispatch(
  157. showNotification(notificationProps, NOTIFICATION_TIMEOUT));
  158. }
  159. }
  160. return invitesLeftToSend;
  161. });
  162. }
  163. /**
  164. * Indicates if the Add button should be disabled.
  165. *
  166. * @private
  167. * @returns {boolean} - True to indicate that the Add button should
  168. * be disabled, false otherwise.
  169. */
  170. _isAddDisabled() {
  171. return !this.state.inviteItems.length
  172. || this.state.addToCallInProgress;
  173. }
  174. _query: (?string) => Promise<Array<Object>>;
  175. /**
  176. * Performs a people and phone number search request.
  177. *
  178. * @param {string} query - The search text.
  179. * @private
  180. * @returns {Promise}
  181. */
  182. _query(query = '') {
  183. const {
  184. _addPeopleEnabled: addPeopleEnabled,
  185. _dialOutAuthUrl: dialOutAuthUrl,
  186. _dialOutEnabled: dialOutEnabled,
  187. _jwt: jwt,
  188. _peopleSearchQueryTypes: peopleSearchQueryTypes,
  189. _peopleSearchUrl: peopleSearchUrl,
  190. _sipInviteEnabled: sipInviteEnabled
  191. } = this.props;
  192. const options = {
  193. addPeopleEnabled,
  194. dialOutAuthUrl,
  195. dialOutEnabled,
  196. jwt,
  197. peopleSearchQueryTypes,
  198. peopleSearchUrl,
  199. sipInviteEnabled
  200. };
  201. return getInviteResultsForQuery(query, options);
  202. }
  203. }
  204. /**
  205. * Maps (parts of) the Redux state to the props of this component.
  206. *
  207. * @param {Object} state - The Redux state.
  208. * @private
  209. * @returns {{
  210. * _addPeopleEnabled: boolean,
  211. * _dialOutAuthUrl: string,
  212. * _dialOutEnabled: boolean,
  213. * _jwt: string,
  214. * _peopleSearchQueryTypes: Array<string>,
  215. * _peopleSearchUrl: string
  216. * }}
  217. */
  218. export function _mapStateToProps(state: Object) {
  219. const {
  220. callFlowsEnabled,
  221. dialOutAuthUrl,
  222. peopleSearchQueryTypes,
  223. peopleSearchUrl
  224. } = state['features/base/config'];
  225. return {
  226. _addPeopleEnabled: isAddPeopleEnabled(state),
  227. _callFlowsEnabled: callFlowsEnabled,
  228. _dialOutAuthUrl: dialOutAuthUrl,
  229. _dialOutEnabled: isDialOutEnabled(state),
  230. _jwt: state['features/base/jwt'].jwt,
  231. _peopleSearchQueryTypes: peopleSearchQueryTypes,
  232. _peopleSearchUrl: peopleSearchUrl,
  233. _sipInviteEnabled: isSipInviteEnabled(state)
  234. };
  235. }