import React, { Fragment, useCallback, useState } from 'react';
import { connect } from 'react-redux';
import CategoryEditModal from '../modulePanel/CategoryEditModal';
import { addCategory, updateCategory, deleteCategory, sortCategory, getTemplates } from 'models/template';
import EasyvButton from 'components/EasyvButton';

function CategoryEdit({ categorys, addCategory, updateCategory, deleteCategory, sortCategory }) {
  const [visible, setVisible] = useState(false);

  const handleCancel = useCallback(() => {
    setVisible(false);
    // getTemplates();
  }, []);

  return (
    <Fragment>
      <EasyvButton style={{ marginRight: 12 }} onClick={() => setVisible(true)}>
        分类管理
      </EasyvButton>
      <CategoryEditModal
        visible={visible}
        categorys={categorys}
        onCancel={handleCancel}
        onAdd={addCategory}
        onUpdate={updateCategory}
        onDelete={deleteCategory}
        onSort={sortCategory}
      />
    </Fragment>
  );
}

const mapStateToProps = ({ templates }) => {
  return {
    categorys: templates.categorys
  };
};

const mapDispatchToProps = {
  addCategory,
  updateCategory,
  deleteCategory,
  sortCategory,
  getTemplates
};

export default connect(mapStateToProps, mapDispatchToProps)(CategoryEdit);
