| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 | import Button from '@atlaskit/button';
import { FieldText } from '@atlaskit/field-text';
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { setPassword } from '../../base/conference';
import { translate } from '../../base/i18n';
/**
 * A React {@code Component} for locking a JitsiConference with a password.
 */
class AddPasswordForm extends Component {
    /**
     * {@code AddPasswordForm}'s property types.
     *
     * @static
     */
    static propTypes = {
        /**
         * The JitsiConference on which to lock and set a password.
         *
         * @type {JitsiConference}
         */
        conference: PropTypes.object,
        /**
         * Invoked to set a password on the conference.
         */
        dispatch: PropTypes.func,
        /**
         * Invoked to obtain translated strings.
         */
        t: PropTypes.func
    };
    /**
     * Initializes a new {@code AddPasswordForm} instance.
     *
     * @param {Object} props - The read-only properties with which the new
     * instance is to be initialized.
     */
    constructor(props) {
        super(props);
        this.state = {
            /**
             * The current value to display in {@code AddPasswordForm}
             * component's input field. The value is also used as the desired
             * new password when creating a {@code setPassword} action.
             *
             * @type {string}
             */
            password: ''
        };
        /**
         * The internal reference to the React {@code component} for entering a
         * password.
         *
         * @private
         * @type {ReactComponent}
         */
        this._inputComponent = null;
        // Bind event handlers so they are only bound once for every instance.
        this._onKeyDown = this._onKeyDown.bind(this);
        this._onPasswordChange = this._onPasswordChange.bind(this);
        this._onSubmit = this._onSubmit.bind(this);
        this._setInput = this._setInput.bind(this);
    }
    /**
     * Directly bind a handler to the input element. This is done in order to
     * intercept enter presses so any outer forms do not become submitted.
     * Atlaskit Button does not expose a way to hook onto keydown events.
     *
     * @inheritdoc
     */
    componentDidMount() {
        this._inputComponent.input.onkeydown = this._onKeyDown;
    }
    /**
     * Remove any handlers set directly on DOM elements.
     *
     * @inheritdoc
     */
    componentWillUnmount() {
        this._inputComponent.input.onkeydown = null;
    }
    /**
     * Implements React's {@link Component#render()}.
     *
     * @inheritdoc
     * @returns {ReactElement}
     */
    render() {
        const { t } = this.props;
        return (
            <div
                className = 'form-control'
                onSubmit = { this._onSubmit } >
                <div className = 'form-control__container'>
                    <div className = 'form-control__input-container'>
                        <FieldText
                            autoFocus = { true }
                            compact = { true }
                            id = 'newPasswordInput'
                            isLabelHidden = { true }
                            label = 'Enter Password'
                            onChange = { this._onPasswordChange }
                            onKeyDown = { this._onKeyDown }
                            placeholder = { t('dialog.createPassword') }
                            ref = { this._setInput }
                            shouldFitContainer = { true }
                            type = 'text' />
                    </div>
                    <Button
                        id = 'addPasswordBtn'
                        isDisabled = { !this.state.password }
                        onClick = { this._onSubmit }
                        shouldFitContainer = { true }
                        type = 'button'>
                        { t('dialog.add') }
                    </Button>
                </div>
            </div>
        );
    }
    /**
     * Mimics form behavior by listening for enter key press and submitting the
     * entered password.
     *
     * @param {Object} event - DOM Event for keydown.
     * @private
     * @returns {void}
     */
    _onKeyDown(event) {
        event.stopPropagation();
        if (event.keyCode === /* Enter */ 13) {
            this._onSubmit();
        }
    }
    /**
     * Updates the internal state of the entered password.
     *
     * @param {Object} event - DOM Event for value change.
     * @private
     * @returns {void}
     */
    _onPasswordChange(event) {
        this.setState({ password: event.target.value });
    }
    /**
     * Dispatches a request to lock the conference with a password.
     *
     * @private
     * @returns {void}
     */
    _onSubmit() {
        if (!this.state.password) {
            return;
        }
        const { conference } = this.props;
        this.props.dispatch(setPassword(
            conference,
            conference.lock,
            this.state.password
        ));
        this.setState({ password: '' });
    }
    /**
     * Sets the instance variable for the React Component used for entering a
     * password.
     *
     * @param {Object} inputComponent - The React Component for the input
     * field.
     * @private
     * @returns {void}
     */
    _setInput(inputComponent) {
        if (inputComponent !== this._inputComponent) {
            this._inputComponent = inputComponent;
        }
    }
}
export default translate(connect()(AddPasswordForm));
 |