UserProfile.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import React from 'react'
  2. import { connect } from 'react-redux'
  3. import { Link } from 'react-router-dom'
  4. import { Icon, Col, Row, Input, Form, Tooltip, Breadcrumb } from 'antd'
  5. const FormItem = Form.Item
  6. const styles = require('./profile.less')
  7. import Box from 'components/Box'
  8. import Avatar from 'components/Avatar'
  9. import { createStructuredSelector } from 'reselect'
  10. import { makeSelectLoading, makeSelectUserProfile } from './selectors'
  11. import { compose } from 'redux'
  12. import injectReducer from 'utils/injectReducer'
  13. import { getUserProfile } from './actions'
  14. import injectSaga from 'utils/injectSaga'
  15. import reducer from './reducer'
  16. import saga from './sagas'
  17. import { RouteComponentWithParams } from 'utils/types'
  18. const utilStyles = require('assets/less/util.less')
  19. interface IProfileProps {
  20. form: any
  21. loading: boolean
  22. userProfile: any
  23. onGetUserProfile: (id: number) => any
  24. }
  25. export class UserProfile extends React.PureComponent<IProfileProps & RouteComponentWithParams, {}> {
  26. public componentDidMount () {
  27. const { match, onGetUserProfile } = this.props
  28. const userId = +match.params.userId
  29. onGetUserProfile(userId)
  30. }
  31. public componentWillReceiveProps (nextProps) {
  32. if (nextProps && nextProps.userProfile !== this.props.userProfile) {
  33. const { name, description, department } = nextProps.userProfile
  34. this.props.form.setFieldsValue({name, description, department})
  35. }
  36. }
  37. public render () {
  38. const { userProfile } = this.props
  39. const commonFormItemStyle = {
  40. labelCol: { span: 20 },
  41. wrapperCol: { span: 20 }
  42. }
  43. let organizations = void 0
  44. if (userProfile) {
  45. organizations = userProfile.organizations.map((org, index) => (
  46. <Tooltip key={`${org}_${index}_tooltip`} placement="bottom" title={org.name}>
  47. <div key={`${org}_${index}`} className={styles.avatarWrapper}>
  48. <Avatar path={org.avatar} size="small"/>
  49. </div>
  50. </Tooltip>)
  51. )
  52. }
  53. const name = userProfile.name ? userProfile.name : '他'
  54. const { getFieldDecorator } = this.props.form
  55. return (
  56. <Box>
  57. <Box.Header>
  58. <Box.Title>
  59. <Breadcrumb className={utilStyles.breadcrumb}>
  60. <Breadcrumb.Item>
  61. <Link to="/account/profile">
  62. <Icon type="bars" />{`${name}的信息`}
  63. </Link>
  64. </Breadcrumb.Item>
  65. </Breadcrumb>
  66. </Box.Title>
  67. </Box.Header>
  68. <Box.Body>
  69. <div className={styles.containerWarp}>
  70. <div className={styles.form}>
  71. <Form
  72. layout="vertical"
  73. className={styles.formView}
  74. >
  75. <Row>
  76. <Col>
  77. <FormItem
  78. {...commonFormItemStyle}
  79. label="姓名"
  80. >
  81. {getFieldDecorator('name', {})(
  82. <Input disabled/>
  83. )}
  84. </FormItem>
  85. </Col>
  86. <Col>
  87. <FormItem
  88. {...commonFormItemStyle}
  89. label="描述"
  90. >
  91. {getFieldDecorator('description', {})(
  92. <Input disabled/>
  93. )}
  94. </FormItem>
  95. </Col>
  96. <Col>
  97. <FormItem
  98. {...commonFormItemStyle}
  99. label="部门"
  100. >
  101. {getFieldDecorator('department', {})(
  102. <Input disabled/>
  103. )}
  104. </FormItem>
  105. </Col>
  106. <Col>
  107. <FormItem
  108. {...commonFormItemStyle}
  109. label="组织"
  110. >
  111. {getFieldDecorator('org', {})(
  112. <div className={styles.orgIcon}>{organizations}</div>
  113. )}
  114. </FormItem>
  115. </Col>
  116. </Row>
  117. </Form>
  118. </div>
  119. <div className={styles.avatar}>
  120. <Avatar path="" size="large"/>
  121. </div>
  122. </div>
  123. </Box.Body>
  124. </Box>
  125. )
  126. }
  127. }
  128. export function mapDispatchToProps (dispatch) {
  129. return {
  130. onGetUserProfile: (id) => dispatch(getUserProfile(id))
  131. }
  132. }
  133. const mapStateToProps = createStructuredSelector({
  134. loading: makeSelectLoading(),
  135. userProfile: makeSelectUserProfile()
  136. })
  137. const withConnect = connect(mapStateToProps, mapDispatchToProps)
  138. const withReducerProfile = injectReducer({key: 'profile', reducer})
  139. const withSagaProfile = injectSaga({key: 'profile', saga})
  140. export default compose(
  141. withReducerProfile,
  142. withSagaProfile,
  143. withConnect
  144. )(Form.create()(UserProfile))