123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- /*
- * <<
- * 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<IProjectAdminProps, IProjectAdminStates> {
- 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 <T> (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<number>(pAdmins, orgMemberId)
- }
- })
- const admins = projectAdmins && projectAdmins.length ? projectAdmins : []
- const addButton = (
- <Tooltip placement="bottom" title="添加">
- <Button
- type="primary"
- icon="plus"
- onClick={this.toggleAdminForm}
- >
- 添加管理员
- </Button>
- </Tooltip>
- )
- const columns = [
- {
- title: '管理员名称',
- dataIndex: 'user',
- key: 'userKey',
- render: (text) => {
- return <span>{text.username}</span>
- }
- },
- {
- title: '设置',
- dataIndex: 'id',
- // className: isHidden ? utilStyles.hide : '',
- key: 'settings',
- width: 200,
- render: (text, record) => {
- return (
- <span>
- <Popconfirm
- title="确定删除?"
- placement="bottom"
- onConfirm={this.deleteAdmin({id, relationId: Number(text)})}
- >
- <a href="javascript:;" onClick={this.stopPPG}>删除管理员</a>
- </Popconfirm>
- </span>
- )
- }
- }]
- const adminButton =
- (
- <Button
- key="submit"
- type="primary"
- // loading={adminModalLoading}
- // disabled={adminModalLoading}
- onClick={this.onSaveAdmin}
- >
- 保 存
- </Button>
- )
- return (
- <div className={styles.admin}>
- <Row>
- <Col span={14}>
- <Input.Search
- placeholder="搜索管理员"
- value={this.state.searchValue}
- onChange={this.searchChange}
- />
- </Col>
- <Col span={2} offset={6}>
- {addButton}
- </Col>
- </Row>
- <Row>
- <div className={styles.tableWrap}>
- <Table
- bordered
- columns={columns}
- dataSource={admins}
- pagination={false}
- />
- </div>
- </Row>
- <Modal
- key="adminFormKey"
- title="添加管理员"
- visible={this.state.adminFormVisible}
- footer={adminButton}
- onCancel={this.toggleAdminForm}
- afterClose={this.afterAdminFormClose}
- >
- <AdminForm
- wrappedComponentRef={this.refHandlers.AdminForm}
- dataSource={notAdminMembers}
- optionTitle={this.setTransferOptionTitle}
- adminTargetKeys={this.state.adminTargetKeys}
- targetKeys={this.state.adminTargetKeys}
- setTargetKeys={this.setAdminTargetKeys}
- rowKeys={this.setRowKeys}
- />
- </Modal>
- </div>
- )
- }
- }
- 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)
|