const preact = require('preact');
const { Component } = require('preact');

class SpawnButton extends Component {
    constructor(props) {
        super(props);

        this.state = { value: null, enabled: false };

        this.handleInput = this.handleInput.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
        this.enable = this.enable.bind(this);
    }

    handleInput(event) {
        console.log(event.target.value);
        this.setState({ value: event.target.value });
    }

    handleSubmit(event) {
        event.preventDefault();
        this.props.spawn(this.state.value);
        this.setState({ value: null, enabled: false });
    }

    enable() {
        this.setState({ enabled: true });
    }

    render() {
        return (
            <div
                key={this.props.i}
                class="menu-construct spawn-btn"
                onClick={() => this.enable()} >
                <h2>+</h2>
                <input
                    class="login-input"
                    type="text"
                    disabled={!this.state.enabled}
                    value={this.state.value}
                    placeholder="name"
                    onInput={this.handleInput}
                />
                <button
                    class="login-btn"
                    disabled={!this.state.enabled}
                    onClick={this.handleSubmit}
                    type="submit">
                    spawn
                </button>
            </div>
        );
    }
}

module.exports = SpawnButton;