import React, { Component } from 'react';
import { connect } from 'react-redux';

import { SearchBox, searchRequest } from '../modules/search/index';
import { Icon } from '../components/index';

import { generateColour } from '../helpers/index';
import { openPane } from '../utils/index';
import Url from "../domain/Url";

class Header extends Component {

    getColour(str) {
        return generateColour(str);
    }

    onSearchSubmit(q, index) {
        const { dispatch, datasources, fields } = this.props;

        Url.addQueryParam('search', q);

        dispatch(searchRequest({
            query: q,
            datasources: datasources.filter(datasource => datasource.active),
            fields: fields
        }));
    }

    openPane() {
        const { dispatch } = this.props;
        dispatch(openPane('configuration'));
    }

    render() {
        const { connected, total, fields, datasources } = this.props;

        let errors = null;

        if (this.props.errors) {
            errors = <div className="alert alert-danger"><strong>Error executing query: </strong>{ this.props.errors }</div>;
        }

        return (
            <header className="header">
                <SearchBox
                    total={total}
                    onSubmit={(q, index) => this.onSearchSubmit(q, index)}
                    connected={connected}
                    indexes={datasources}
                    enabled={fields.length > 0 && datasources.length > 0}
                />
                { errors }
            </header>
        );
    }
}


function select(state) {
    return {
        itemsFetching: state.entries.itemsFetching,
        connected: state.entries.connected,
        errors: state.entries.errors,
        datasources: state.datasources.datasources.filter(datasource => datasource.active),
        queries: state.entries.searches,
        total: state.entries.total,
        fields: state.entries.fields
    };
}


export default connect(select)(Header);

@@ -434,23 +434,16 @@
 Submit(q
-, index
 ) %7B%0A    
@@ -466,29 +466,8 @@
 atch
-, datasources, fields
  %7D =
@@ -556,146 +556,9 @@
 est(
-%7B%0A            query: q,%0A            datasources: datasources.filter(datasource =%3E datasource.active),%0A            fields: fields%0A        %7D
+q
 ));%0A
@@ -1197,50 +1197,8 @@
 ed%7D%0A
-                    indexes=%7Bdatasources%7D%0A
     

fix search in header