import React from 'react' import FormType from 'antd/lib/form/Form' import { createStructuredSelector } from 'reselect' import RoleForm from './RoleForm' import CheckPanel from 'app/components/CheckPanel' // import RelRoleMember from './RelRoleMember' import { connect } from 'react-redux' import { Row, Col, Tooltip, Button, Input, Table, Modal, Popconfirm, Divider, message } from 'antd' import { ColumnProps } from 'antd/lib/table' const styles = require('../Organization.less') import { checkNameUniqueAction } from 'containers/App/actions' import { OrganizationActions } from '../actions' const { addRole, loadOrganizationRole, deleteRole, relRoleMember, editRole, getRelRoleMember } = OrganizationActions import ComponentPermission from 'containers/Account/components/checkMemberPermission' import { makeSelectRoleModalLoading, makeSelectCurrentOrganizationRole } from '../selectors' import { IOrganization, IOrganizationRole } from '../types' interface IRoleState { formType: string formVisible: boolean relFormVisible: boolean currentRoleId: number currentOrganizationRole: any[] groupTransfer: { id: string, targets: any[] } searchValue: string filteredTableSource: { type: 'origin' | 'filtered' dataSource: any[] } } interface IRoleProps { isLoginUserOwner?: boolean onAddRole?: (name: string, desc: string, id: number, resolve: () => any) => any onEditRole?: (name: string, desc: string, id: number, resolve: () => any) => any onDeleteRole?: (id, resolve) => any onRelRoleMember?: (id: number, memberIds: number[], resolve: () => any) => any onGetRelRoleMember?: (id: number, resolve: (result: any) => any) => any onLoadOrganizationRole?: (orgId: number) => any currentOrganization?: IOrganization currentOrganizationRole?: IOrganizationRole[] organizationMembers?: any[] organizations?: any roleModalLoading?: boolean onLoadOrganizationDetail?: (id: number) => any onCheckUniqueName?: (pathname: any, data: any, resolve: () => any, reject: (error: string) => any) => any } export interface ITeam { id?: number role?: number avatar?: string organization?: IOrganization name?: string visibility?: boolean description: string parentTeamId: number } export class RoleList extends React.PureComponent { constructor(props) { super(props) this.state = { formType: 'add', formVisible: false, relFormVisible: false, currentRoleId: 0, currentOrganizationRole: [], groupTransfer: { id: '', targets: [] }, searchValue: '', filteredTableSource: { type: 'origin', dataSource: [] } } } private RoleForm: FormType private RelRoleMember: FormType private refHandles = { RoleForm: (ref) => this.RoleForm = ref, RelRoleMember: (ref) => this.RelRoleMember = ref } private showRelRoleForm = (flag, roleId?: number) => (e) => { const { onGetRelRoleMember } = this.props e.stopPropagation() onGetRelRoleMember(roleId, (result) => { const targets = result && result.length ? result.map((re) => re.user.id) : [] // const targets = result && result.length ? result.map((re) => re.user) : [] this.setState({ relFormVisible: !this.state.relFormVisible, currentRoleId: roleId, groupTransfer: { id: `${roleId}`, targets } }) }) } public componentDidMount() { const { currentOrganizationRole } = this.props if (!currentOrganizationRole) { this.loadOrganizationRole() } else { this.setState({ currentOrganizationRole }) } } public componentWillReceiveProps(nextProps) { const { currentOrganizationRole } = nextProps if (currentOrganizationRole !== this.props.currentOrganizationRole) { this.setState({ currentOrganizationRole }) } } private loadOrganizationRole = () => this.props.onLoadOrganizationRole(this.props.currentOrganization['id']) private showRoleForm = (flag, roleId?: number) => (e) => { e.stopPropagation() this.setState({ formVisible: !this.state.formVisible, formType: flag }, () => { if (flag !== 'add') { setTimeout(() => { const { description, id, name } = this.props.currentOrganizationRole.find((role) => role.id === roleId) this.RoleForm.props.form.setFieldsValue({ description, id, name }) }, 0) } }) } private checkNameUnique = (rule, value = '', callback) => { const { onCheckUniqueName, currentOrganization: { id } } = this.props const data = { name: value, orgId: id, id: null } onCheckUniqueName('team', data, () => { callback() }, (err) => { callback(err) }) } private onSaveRelRowMember = () => { const { onRelRoleMember } = this.props const { currentRoleId, groupTransfer } = this.state onRelRoleMember(currentRoleId, groupTransfer.targets, () => { this.hideRelForm() }) } private hideTeamForm = () => { this.setState({ formVisible: false }) } private hideRelForm = () => { this.setState({ relFormVisible: false }) } private afterTeamFormClose = () => { this.RoleForm.props.form.resetFields() } // private afterRelFormClose = () => { // this.RelRoleMember.props.form.resetFields() // } private handleDelete = (roleId) => () => { const { onDeleteRole } = this.props this.loadOrganizationRole() if (roleId) { onDeleteRole(roleId, () => { message.success('删除成功') this.loadOrganizationRole() }) } } private onGroupTransferChange = (targets) => { this.setState({ groupTransfer: { id: this.state.groupTransfer.id, targets } }) } private searchChange = (e) => { const searchValue = e.target.value const { currentOrganizationRole } = this.props const result = (currentOrganizationRole as any[]).filter((role) => { return role && role.name.indexOf(searchValue.trim()) > -1 }) this.setState({ searchValue, currentOrganizationRole: searchValue && searchValue.length ? result : this.props.currentOrganizationRole }) } private createOrRole = () => { const { formType } = this.state const { onAddRole, onEditRole } = this.props this.RoleForm.props.form.validateFieldsAndScroll((err, values) => { if (!err) { const { name, description, id } = values const orgId = this.props.currentOrganization.id if (formType === 'add') { onAddRole(name, description, orgId, () => { this.loadOrganizationRole() this.hideTeamForm() }) } else { onEditRole(name, description, id, () => { this.loadOrganizationRole() this.hideTeamForm() }) } } }) } public render() { const { formVisible, relFormVisible, searchValue, filteredTableSource, formType, groupTransfer, currentOrganizationRole } = this.state const { isLoginUserOwner, currentOrganization, currentOrganization: { id }, roleModalLoading, organizationMembers } = this.props const roleModalTitle = formType === 'add' ? '新增角色' : '修改角色信息' const relRoleModalTitle = '关联成员' let CreateButton = void 0 if (currentOrganization) { CreateButton = ComponentPermission(currentOrganization, '')(Button) } let columns: Array> = [ { title: '角色名', dataIndex: 'name', key: 'name' }, { title: '描述', dataIndex: 'description', key: 'description' } ] if (isLoginUserOwner) { columns = columns.concat({ title: '操作', dataIndex: 'setting', key: 'setting', render: (text, record) => ( 关联成员 编辑 删除 ) }) } const addModalButtons = ( ) const relRoleModalButtons = ( ) const roleMembers = organizationMembers.map(v => v.user) return (
{/* */} ) } } type MappedStates = ReturnType type MappedDispatches = ReturnType const mapStateToProps = createStructuredSelector({ roleModalLoading: makeSelectRoleModalLoading(), currentOrganizationRole: makeSelectCurrentOrganizationRole(), }) export function mapDispatchToProps(dispatch) { return { onLoadOrganizationRole: (orgId) => dispatch(loadOrganizationRole(orgId)), onAddRole: (name, desc, id, resolve) => dispatch(addRole(name, desc, id, resolve)), onEditRole: (name, desc, id, resolve) => dispatch(editRole(name, desc, id, resolve)), onDeleteRole: (id, resolve) => dispatch(deleteRole(id, resolve)), onRelRoleMember: (id, memberIds, resolve) => dispatch(relRoleMember(id, memberIds, resolve)), onGetRelRoleMember: (id, resolve) => dispatch(getRelRoleMember(id, resolve)), onCheckUniqueName: (pathname, data, resolve, reject) => dispatch(checkNameUniqueAction(pathname, data, resolve, reject)) } } export default connect(mapStateToProps, mapDispatchToProps)(RoleList)