import React, { Component } from 'react' import PropTypes from 'prop-types' import { connect } from 'react-redux' import { Button, Icon, Popconfirm, Modal, Input, message, Menu, Row, Col } from 'antd' import { autobind } from 'core-decorators'; import axios from 'axios'; import { withRouter } from 'react-router'; const { TextArea } = Input; const Search = Input.Search; const TYPE_EDIT = 'edit'; import { fetchGroupList, setCurrGroup, setGroupList } from '../../../actions/group.js' import './GroupList.scss' @connect( state => ({ groupList: state.group.groupList, currGroup: state.group.currGroup }), { fetchGroupList, setCurrGroup, setGroupList } ) @withRouter export default class GroupList extends Component { static propTypes = { groupList: PropTypes.array, currGroup: PropTypes.object, fetchGroupList: PropTypes.func, setCurrGroup: PropTypes.func, setGroupList: PropTypes.func, match: PropTypes.object, history: PropTypes.object } state = { addGroupModalVisible: false, editGroupModalVisible: false, newGroupName: '', newGroupDesc: '', currGroupName: '', currGroupDesc: '', groupList: [] } constructor(props) { super(props) } componentWillMount() { const groupName = this.props.match.params.groupName; this.props.fetchGroupList().then(() => { let currGroup = this.props.groupList[0] || { group_name: '', group_desc: '' }; if(this.props.groupList.length && groupName){ for(let i = 0;i { if (res.data.errcode) { message.error(res.data.errmsg); } else { this.setState({ addGroupModalVisible: false }); this.props.fetchGroupList().then(() => { this.setState({groupList: this.props.groupList}); }) this.props.setCurrGroup(res.data.data) } }); } @autobind editGroup() { const { currGroupName: group_name, currGroupDesc: group_desc } = this.state; const id = this.props.currGroup._id; axios.post('/group/up', { group_name, group_desc, id }).then(res => { if (res.data.errcode) { message.error(res.data.errmsg); } else { this.setState({ editGroupModalVisible: false }); this.props.setCurrGroup({ group_name, group_desc, _id: id }); } }); } @autobind inputNewGroupName(e, type) { if (type === TYPE_EDIT) { this.setState({ currGroupName: e.target.value}) } else { this.setState({newGroupName: e.target.value}); } } @autobind inputNewGroupDesc(e, type) { if (type === TYPE_EDIT) { this.setState({ currGroupDesc: e.target.value}) } else { this.setState({newGroupDesc: e.target.value}); } } @autobind selectGroup(e) { const groupId = e.key; const currGroup = this.props.groupList.find((group) => { return +group._id === +groupId }); this.props.setCurrGroup(currGroup); this.props.history.replace(`${currGroup._id}`); } @autobind deleteGroup() { const self = this; const { currGroup } = self.props; axios.post('/group/del', {id: currGroup._id}).then(res => { if (res.data.errcode) { message.error(res.data.errmsg); } else { message.success('删除成功'); self.props.fetchGroupList().then(() => { const currGroup = self.props.groupList[0] || { group_name: '', group_desc: '' }; self.setState({groupList: self.props.groupList}); self.props.setCurrGroup(currGroup) }); } }); } @autobind searchGroup(e, value) { const v = value || e.target.value; const { groupList } = this.props; if (v === '') { this.setState({groupList}) } else { this.setState({groupList: groupList.filter(group => new RegExp(v, 'i').test(group.group_name))}) } } render () { const { currGroup } = this.props; return (
{currGroup.group_name}
this.showModal(TYPE_EDIT)}/>
简介:{currGroup.group_desc}
this.searchGroup(null, v)}/>
{ this.state.groupList.map((group) => ( {group.group_name} )) }
分组名:
简介:
this.hideModal(TYPE_EDIT)} className="add-group-modal" >
分组名:
this.inputNewGroupName(e, TYPE_EDIT)}>
简介:
) } }