/* * << * Davinci * == * Copyright (C) 2016 - 2017 EDP * == * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * >> */ import React from 'react' import { compose } from 'redux' import { connect } from 'react-redux' import classnames from 'classnames' import { Icon, Button, Row, Col, Input, Tooltip, Popconfirm, Table, Modal, Form } from 'antd' const FormItem = Form.Item const InputGroup = Input.Group import AdminForm from './Transfer' import AntdFormType from 'antd/lib/form/Form' import Auth from './ProjectAuth' const styles = require('../Project.less') const utilStyles = require('assets/less/util.less') import { createStructuredSelector } from 'reselect' import { makeSelectCurrentOrganizationProject, makeSelectCurrentOrganizationMembers, makeSelectCurrentOrganizationProjectAdmins } from '../selectors' import { ProjectActions } from 'containers/Projects/actions' const { addProjectAdmin, deleteProjectAdmin } = ProjectActions import { OrganizationActions } from '../actions' const { loadProjectAdmin } = OrganizationActions interface IProjectAdminStates { relationRoleVisible: boolean authSettingVisible: boolean adminTargetKeys: [] searchValue: string projectAdmins: any[] adminFormVisible: boolean } interface IProjectAdminProps { form?: any projectDetail: any organizationMembers: any[] projectAdmins: any[] onLoadProjectAdmin: (projectId: number) => any onAddProjectAdmin: (projectId: number, adminIds: number[], resolve?: (result: any) => any) => any onDeleteProjectAdmin: (projectId: number, adminId: number , resolve: () => any) => any } export class ProjectAdmin extends React.PureComponent { private AdminForm: AntdFormType = null private refHandlers = { AdminForm: (ref) => this.AdminForm = ref } constructor (props) { super(props) this.state = { searchValue: '', projectAdmins: [], relationRoleVisible: false, authSettingVisible: false, adminTargetKeys: [], adminFormVisible: false } } public componentWillMount () { this.loadAdmins(this.props.projectDetail['id']) } private loadAdmins = (id) => this.props.onLoadProjectAdmin(id) private onSaveAdmin = () => { const { adminTargetKeys } = this.state const { projectDetail: {id} } = this.props const adminIds = adminTargetKeys.map((admin) => Number(admin)) this.props.onAddProjectAdmin(id, adminIds, (result) => { this.loadAdmins(id) this.toggleAdminForm() this.setState({ adminTargetKeys: []}) }) } public componentWillReceiveProps (nextProps) { const { projectAdmins, projectDetail: {id} } = nextProps if (projectAdmins !== this.props.projectAdmins) { this.setState ({ projectAdmins }) } if (id !== this.props.projectDetail['id']) { this.loadAdmins(id) } } private searchChange = (e) => { const searchValue = e.target.value const result = (this.props.projectAdmins as any[]).filter((admin) => { return admin && admin.user.username.indexOf(searchValue.trim()) > -1 }) this.setState({ searchValue, projectAdmins: searchValue && searchValue.length ? result : this.props.projectAdmins }) } private deleteAdmin = (option) => () => { const { projectDetail, onDeleteProjectAdmin } = this.props const {id, relationId} = option onDeleteProjectAdmin(id, relationId, () => { this.loadAdmins(projectDetail.id) this.setState({ adminTargetKeys: []}) }) } private toggleModal = (flag: string) => () => { if (flag === 'relationRoleVisible') { this.setState({ relationRoleVisible: !this.state[flag] }) } else { this.setState({ authSettingVisible: !this.state[flag] }) } } private stopPPG = (e) => { e.stopPropagation() } private toggleAdminForm = () => { this.setState({adminFormVisible: !this.state.adminFormVisible}) } private afterAdminFormClose = () => { this.AdminForm.props.form.resetFields() } private setRowKeys = (item) => item.user.id private setTransferOptionTitle = (item) => item.user.username private setAdminTargetKeys = (newTargetKeys) => { if (newTargetKeys) { this.setState({adminTargetKeys: newTargetKeys}) } } private isInArray (source: T[], target: T) { return source.some((s) => s === target) } public render () { const { projectAdmins } = this.state const { organizationMembers, projectDetail: {id} } = this.props let pAdmins = [] if (this.props.projectAdmins && this.props.projectAdmins.length) { pAdmins = this.props.projectAdmins.map((admin) => admin && admin.user ? admin.user.id : void 0).filter((s) => s) } const notAdminMembers = organizationMembers.filter((member) => { const orgMemberId = member && member.user && member.user.id if (typeof orgMemberId === 'number') { return !this.isInArray(pAdmins, orgMemberId) } }) const admins = projectAdmins && projectAdmins.length ? projectAdmins : [] const addButton = ( ) const columns = [ { title: '管理员名称', dataIndex: 'user', key: 'userKey', render: (text) => { return {text.username} } }, { title: '设置', dataIndex: 'id', // className: isHidden ? utilStyles.hide : '', key: 'settings', width: 200, render: (text, record) => { return ( 删除管理员 ) } }] const adminButton = ( ) return (
{addButton}
) } } const mapStateToProps = createStructuredSelector({ projectDetail: makeSelectCurrentOrganizationProject(), organizationMembers: makeSelectCurrentOrganizationMembers(), projectAdmins: makeSelectCurrentOrganizationProjectAdmins() }) export function mapDispatchToProps (dispatch) { return { onLoadProjectAdmin: (projectId) => dispatch(loadProjectAdmin(projectId)), onAddProjectAdmin: (projectId, adminIds, resolve) => dispatch(addProjectAdmin(projectId, adminIds, resolve)), onDeleteProjectAdmin: (projectId, relationId , resolve) => dispatch(deleteProjectAdmin (projectId, relationId , resolve)) } } const withConnect = connect(mapStateToProps, mapDispatchToProps) export default compose(withConnect)(ProjectAdmin)