import { StatelessDropdownMenu } from '@atlaskit/dropdown-menu';
import ExpandIcon from '@atlaskit/icon/glyph/expand';
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { translate } from '../../base/i18n';
import { updateDialInNumbers } from '../actions';
const logger = require('jitsi-meet-logger').getLogger(__filename);
const EXPAND_ICON = ;
/**
* React {@code Component} responsible for fetching and displaying telephone
* numbers for dialing into the conference. Also supports copying a selected
* dial-in number to the clipboard.
*
* @extends Component
*/
class DialInNumbersForm extends Component {
/**
* {@code DialInNumbersForm}'s property types.
*
* @static
*/
static propTypes = {
/**
* The redux state representing the dial-in numbers feature.
*/
_dialIn: React.PropTypes.object,
/**
* The url for retrieving dial-in numbers.
*/
dialInNumbersUrl: React.PropTypes.string,
/**
* Invoked to send an ajax request for dial-in numbers.
*/
dispatch: React.PropTypes.func,
/**
* Invoked to obtain translated strings.
*/
t: React.PropTypes.func
}
/**
* Initializes a new {@code DialInNumbersForm} instance.
*
* @param {Object} props - The read-only properties with which the new
* instance is to be initialized.
*/
constructor(props) {
super(props);
this.state = {
/**
* Whether or not the dropdown should be open.
*
* @type {boolean}
*/
isDropdownOpen: false,
/**
* The dial-in number to display as currently selected in the
* dropdown. The value should be an object which has two key/value
* pairs, content and number. The value of "content" will display in
* the dropdown while the value of "number" is a substring of
* "content" which will be copied to clipboard.
*
* @type {object}
*/
selectedNumber: null
};
/**
* The internal reference to the DOM/HTML element backing the React
* {@code Component} input. It is necessary for the implementation of
* copying to the clipboard.
*
* @private
* @type {HTMLInputElement}
*/
this._inputElement = null;
// Bind event handlers so they are only bound once for every instance.
this._onClick = this._onClick.bind(this);
this._onOpenChange = this._onOpenChange.bind(this);
this._onSelect = this._onSelect.bind(this);
this._setInput = this._setInput.bind(this);
}
/**
* Dispatches a request for numbers if not already present in the redux
* store. If numbers are present, sets a default number to display in the
* dropdown trigger.
*
* @inheritdoc
* returns {void}
*/
componentDidMount() {
if (this.props._dialIn.numbers) {
this._setDefaultNumber(this.props._dialIn.numbers);
} else {
this.props.dispatch(
updateDialInNumbers(this.props.dialInNumbersUrl));
}
}
/**
* Monitors for number updates and sets a default number to display in the
* dropdown trigger if not already set.
*
* @inheritdoc
* returns {void}
*/
componentWillReceiveProps(nextProps) {
if (!this.state.selectedNumber && nextProps._dialIn.numbers) {
this._setDefaultNumber(nextProps._dialIn.numbers);
}
}
/**
* Implements React's {@link Component#render()}.
*
* @inheritdoc
* @returns {ReactElement}
*/
render() {
const { t, _dialIn } = this.props;
const numbers = _dialIn.numbers;
const items = numbers ? this._formatNumbers(numbers) : [];
const isEnabled = this._isDropdownEnabled();
const inputWrapperClassNames
= `form-control__container ${isEnabled ? '' : 'is-disabled'}
${_dialIn.loading ? 'is-loading' : ''}`;
let triggerText = '';
if (!_dialIn.numbersEnabled) {
triggerText = t('invite.numbersDisabled');
} else if (this.state.selectedNumber
&& this.state.selectedNumber.content) {
triggerText = this.state.selectedNumber.content;
} else if (!numbers && _dialIn.loading) {
triggerText = t('invite.loadingNumbers');
} else if (_dialIn.error) {
triggerText = t('invite.errorFetchingNumbers');
} else {
triggerText = t('invite.noNumbers');
}
return (
{ this._createDropdownMenu(items, triggerText) }
);
}
/**
* Creates a {@code StatelessDropdownMenu} instance.
*
* @param {Array} items - The content to display within the dropdown.
* @param {string} triggerText - The text to display within the
* trigger element.
* @returns {ReactElement}
*/
_createDropdownMenu(items, triggerText) {
return (
{ this._createDropdownTrigger(triggerText) }
);
}
/**
* Creates a React {@code Component} with a redonly HTMLInputElement as a
* trigger for displaying the dropdown menu. The {@code Component} will also
* display the currently selected number.
*
* @param {string} triggerText - Text to display in the HTMLInputElement.
* @private
* @returns {ReactElement}
*/
_createDropdownTrigger(triggerText) {
return (
{ EXPAND_ICON }
);
}
/**
* Detects whether the response from dialInNumbersUrl returned an array or
* an object with dial-in numbers and calls the appropriate method to
* transform the numbers into the format expected by
* {@code StatelessDropdownMenu}.
*
* @param {Array|Object} dialInNumbers - The numbers returned from
* requesting dialInNumbersUrl.
* @private
* @returns {Array