import React, { Component } from 'react';

import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import PropertiesActions from 'cem/actions/properties';

// import Filter from 'cem/components/properties/list/filter';
import Card from 'cem/components/properties/card';

import styles from 'cem/styles/header';
import UI from 'cem/components/ui';
const { Grid } = UI;

class List extends Component {
  componentWillMount() {
    this.props.actions.loadProperties();
  }

  render() {
    const { list: { items = [] }, isFetching = false } = this.props.state;

    return (
      <section>
        <Grid.Container fluid>
          <div className={styles.header}>
            <Grid.Row>
              <Grid.Col xs="20">
                <h1 className={styles.title}>
                  Объекты <UI.Button className={styles.btnAdd} to="/properties/create" kind="success">добавить</UI.Button>
                </h1>
              </Grid.Col>
            </Grid.Row>
            <Grid.Row>
              {/* <Filter /> */}
            </Grid.Row>
          </div>
        </Grid.Container>

        {(!isFetching && items.length > 0) && (items.map(property => <Card property={property} key={property.id} />))}
        {(!items.length) && (<h2 className={styles.titleNotFound}>Не найдено объектов</h2>)}
      </section>
    );
  }
}

const pickState = ({ properties }) => ({
  state: properties,
});

const mapDispatch = (dispatch) => ({
  actions: bindActionCreators(PropertiesActions, dispatch),
});

export default connect(pickState, mapDispatch)(List);
