ProjectAdmin.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React from 'react'
  21. import { compose } from 'redux'
  22. import { connect } from 'react-redux'
  23. import classnames from 'classnames'
  24. import { Icon, Button, Row, Col, Input, Tooltip, Popconfirm, Table, Modal, Form } from 'antd'
  25. const FormItem = Form.Item
  26. const InputGroup = Input.Group
  27. import AdminForm from './Transfer'
  28. import AntdFormType from 'antd/lib/form/Form'
  29. import Auth from './ProjectAuth'
  30. const styles = require('../Project.less')
  31. const utilStyles = require('assets/less/util.less')
  32. import { createStructuredSelector } from 'reselect'
  33. import { makeSelectCurrentOrganizationProject, makeSelectCurrentOrganizationMembers, makeSelectCurrentOrganizationProjectAdmins } from '../selectors'
  34. import { ProjectActions } from 'containers/Projects/actions'
  35. const { addProjectAdmin, deleteProjectAdmin } = ProjectActions
  36. import { OrganizationActions } from '../actions'
  37. const { loadProjectAdmin } = OrganizationActions
  38. interface IProjectAdminStates {
  39. relationRoleVisible: boolean
  40. authSettingVisible: boolean
  41. adminTargetKeys: []
  42. searchValue: string
  43. projectAdmins: any[]
  44. adminFormVisible: boolean
  45. }
  46. interface IProjectAdminProps {
  47. form?: any
  48. projectDetail: any
  49. organizationMembers: any[]
  50. projectAdmins: any[]
  51. onLoadProjectAdmin: (projectId: number) => any
  52. onAddProjectAdmin: (projectId: number, adminIds: number[], resolve?: (result: any) => any) => any
  53. onDeleteProjectAdmin: (projectId: number, adminId: number , resolve: () => any) => any
  54. }
  55. export class ProjectAdmin extends React.PureComponent<IProjectAdminProps, IProjectAdminStates> {
  56. private AdminForm: AntdFormType = null
  57. private refHandlers = {
  58. AdminForm: (ref) => this.AdminForm = ref
  59. }
  60. constructor (props) {
  61. super(props)
  62. this.state = {
  63. searchValue: '',
  64. projectAdmins: [],
  65. relationRoleVisible: false,
  66. authSettingVisible: false,
  67. adminTargetKeys: [],
  68. adminFormVisible: false
  69. }
  70. }
  71. public componentWillMount () {
  72. this.loadAdmins(this.props.projectDetail['id'])
  73. }
  74. private loadAdmins = (id) => this.props.onLoadProjectAdmin(id)
  75. private onSaveAdmin = () => {
  76. const { adminTargetKeys } = this.state
  77. const { projectDetail: {id} } = this.props
  78. const adminIds = adminTargetKeys.map((admin) => Number(admin))
  79. this.props.onAddProjectAdmin(id, adminIds, (result) => {
  80. this.loadAdmins(id)
  81. this.toggleAdminForm()
  82. this.setState({ adminTargetKeys: []})
  83. })
  84. }
  85. public componentWillReceiveProps (nextProps) {
  86. const { projectAdmins, projectDetail: {id} } = nextProps
  87. if (projectAdmins !== this.props.projectAdmins) {
  88. this.setState ({
  89. projectAdmins
  90. })
  91. }
  92. if (id !== this.props.projectDetail['id']) {
  93. this.loadAdmins(id)
  94. }
  95. }
  96. private searchChange = (e) => {
  97. const searchValue = e.target.value
  98. const result = (this.props.projectAdmins as any[]).filter((admin) => {
  99. return admin && admin.user.username.indexOf(searchValue.trim()) > -1
  100. })
  101. this.setState({
  102. searchValue,
  103. projectAdmins: searchValue && searchValue.length ? result : this.props.projectAdmins
  104. })
  105. }
  106. private deleteAdmin = (option) => () => {
  107. const { projectDetail, onDeleteProjectAdmin } = this.props
  108. const {id, relationId} = option
  109. onDeleteProjectAdmin(id, relationId, () => {
  110. this.loadAdmins(projectDetail.id)
  111. this.setState({ adminTargetKeys: []})
  112. })
  113. }
  114. private toggleModal = (flag: string) => () => {
  115. if (flag === 'relationRoleVisible') {
  116. this.setState({
  117. relationRoleVisible: !this.state[flag]
  118. })
  119. } else {
  120. this.setState({
  121. authSettingVisible: !this.state[flag]
  122. })
  123. }
  124. }
  125. private stopPPG = (e) => {
  126. e.stopPropagation()
  127. }
  128. private toggleAdminForm = () => {
  129. this.setState({adminFormVisible: !this.state.adminFormVisible})
  130. }
  131. private afterAdminFormClose = () => {
  132. this.AdminForm.props.form.resetFields()
  133. }
  134. private setRowKeys = (item) => item.user.id
  135. private setTransferOptionTitle = (item) => item.user.username
  136. private setAdminTargetKeys = (newTargetKeys) => {
  137. if (newTargetKeys) {
  138. this.setState({adminTargetKeys: newTargetKeys})
  139. }
  140. }
  141. private isInArray <T> (source: T[], target: T) {
  142. return source.some((s) => s === target)
  143. }
  144. public render () {
  145. const { projectAdmins } = this.state
  146. const { organizationMembers, projectDetail: {id} } = this.props
  147. let pAdmins = []
  148. if (this.props.projectAdmins && this.props.projectAdmins.length) {
  149. pAdmins = this.props.projectAdmins.map((admin) => admin && admin.user ? admin.user.id : void 0).filter((s) => s)
  150. }
  151. const notAdminMembers = organizationMembers.filter((member) => {
  152. const orgMemberId = member && member.user && member.user.id
  153. if (typeof orgMemberId === 'number') {
  154. return !this.isInArray<number>(pAdmins, orgMemberId)
  155. }
  156. })
  157. const admins = projectAdmins && projectAdmins.length ? projectAdmins : []
  158. const addButton = (
  159. <Tooltip placement="bottom" title="添加">
  160. <Button
  161. type="primary"
  162. icon="plus"
  163. onClick={this.toggleAdminForm}
  164. >
  165. 添加管理员
  166. </Button>
  167. </Tooltip>
  168. )
  169. const columns = [
  170. {
  171. title: '管理员名称',
  172. dataIndex: 'user',
  173. key: 'userKey',
  174. render: (text) => {
  175. return <span>{text.username}</span>
  176. }
  177. },
  178. {
  179. title: '设置',
  180. dataIndex: 'id',
  181. // className: isHidden ? utilStyles.hide : '',
  182. key: 'settings',
  183. width: 200,
  184. render: (text, record) => {
  185. return (
  186. <span>
  187. <Popconfirm
  188. title="确定删除?"
  189. placement="bottom"
  190. onConfirm={this.deleteAdmin({id, relationId: Number(text)})}
  191. >
  192. <a href="javascript:;" onClick={this.stopPPG}>删除管理员</a>
  193. </Popconfirm>
  194. </span>
  195. )
  196. }
  197. }]
  198. const adminButton =
  199. (
  200. <Button
  201. key="submit"
  202. type="primary"
  203. // loading={adminModalLoading}
  204. // disabled={adminModalLoading}
  205. onClick={this.onSaveAdmin}
  206. >
  207. 保 存
  208. </Button>
  209. )
  210. return (
  211. <div className={styles.admin}>
  212. <Row>
  213. <Col span={14}>
  214. <Input.Search
  215. placeholder="搜索管理员"
  216. value={this.state.searchValue}
  217. onChange={this.searchChange}
  218. />
  219. </Col>
  220. <Col span={2} offset={6}>
  221. {addButton}
  222. </Col>
  223. </Row>
  224. <Row>
  225. <div className={styles.tableWrap}>
  226. <Table
  227. bordered
  228. columns={columns}
  229. dataSource={admins}
  230. pagination={false}
  231. />
  232. </div>
  233. </Row>
  234. <Modal
  235. key="adminFormKey"
  236. title="添加管理员"
  237. visible={this.state.adminFormVisible}
  238. footer={adminButton}
  239. onCancel={this.toggleAdminForm}
  240. afterClose={this.afterAdminFormClose}
  241. >
  242. <AdminForm
  243. wrappedComponentRef={this.refHandlers.AdminForm}
  244. dataSource={notAdminMembers}
  245. optionTitle={this.setTransferOptionTitle}
  246. adminTargetKeys={this.state.adminTargetKeys}
  247. targetKeys={this.state.adminTargetKeys}
  248. setTargetKeys={this.setAdminTargetKeys}
  249. rowKeys={this.setRowKeys}
  250. />
  251. </Modal>
  252. </div>
  253. )
  254. }
  255. }
  256. const mapStateToProps = createStructuredSelector({
  257. projectDetail: makeSelectCurrentOrganizationProject(),
  258. organizationMembers: makeSelectCurrentOrganizationMembers(),
  259. projectAdmins: makeSelectCurrentOrganizationProjectAdmins()
  260. })
  261. export function mapDispatchToProps (dispatch) {
  262. return {
  263. onLoadProjectAdmin: (projectId) => dispatch(loadProjectAdmin(projectId)),
  264. onAddProjectAdmin: (projectId, adminIds, resolve) => dispatch(addProjectAdmin(projectId, adminIds, resolve)),
  265. onDeleteProjectAdmin: (projectId, relationId , resolve) => dispatch(deleteProjectAdmin (projectId, relationId , resolve))
  266. }
  267. }
  268. const withConnect = connect(mapStateToProps, mapDispatchToProps)
  269. export default compose(withConnect)(ProjectAdmin)