

import {connect} from 'react-redux';
import {
  fetchItems,
  deleteItem,
  submitItem,
} from 'store/items/items.actions';
import {
  getResourceViews,
  getViewPages,
} from 'store/customViews/customViews.selectors'
import {
  getCurrentPageResults
} from 'store/selectors';
import t from 'store/types';

export const mapStateToProps = (state, props) => {
  const viewPages = getViewPages(state.items.views, state.items.currentViewId);

  return {
    views: getResourceViews(state, 'items'),
    currentPageItems: getCurrentPageResults(
      state.items.items,
      viewPages,
      state.items.currentPage,
    ),
    bulkSelected: state.items.bulkActions,
    itemsTableLoading: state.items.loading,
  };
};

export const mapDispatchToProps = (dispatch) => ({
  requestFetchItems: (query) => dispatch(fetchItems({ query })),
  requestDeleteItem: (id) => dispatch(deleteItem({ id })),
  requestSubmitItem: (form) => dispatch(submitItem({ form })),
  addBulkActionItem: (id) => dispatch({
    type: t.ITEM_BULK_ACTION_ADD, itemId: id
  }),
  removeBulkActionItem: (id) => dispatch({
    type: t.ITEM_BULK_ACTION_REMOVE, itemId: id,
  }),

  setItemsTableQuery: (key, value) => dispatch({
    type: t.ITEMS_TABLE_QUERY_SET, key, value,
  }),
  addItemsTableQueries: (queries) =>
    dispatch({
      type: t.ITEMS_TABLE_QUERIES_ADD, queries,
    }),
});

export default connect(mapStateToProps, mapDispatchToProps);
