index.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import React from 'react'
  2. import { connect } from 'react-redux'
  3. import { Link } from 'react-router-dom'
  4. import { Icon, Col, message, Row, Input, Form, Button, Breadcrumb } from 'antd'
  5. const FormItem = Form.Item
  6. const styles = require('./profile.less')
  7. import Box from 'components/Box'
  8. import UploadAvatar from 'components/UploadAvatar'
  9. import { createStructuredSelector } from 'reselect'
  10. import { makeSelectLoginUser } from '../App/selectors'
  11. import { compose } from 'redux'
  12. import { updateProfile, checkNameUniqueAction, uploadAvatarSuccess } from '../App/actions'
  13. const utilStyles = require('assets/less/util.less')
  14. interface IProfileProps {
  15. form: any
  16. type: string
  17. loginUser: any
  18. profileForm: any,
  19. onUploadAvatarSuccess: (path: string) => any,
  20. onUpdateProfile: (id: number, name: string, description: string, department: string, resolve: (data: any) => any) => any
  21. onCheckUniqueName: (pathname: any, data: any, resolve: () => any, reject: (error: string) => any) => any
  22. }
  23. export class Profile extends React.PureComponent<IProfileProps, {}> {
  24. private checkNameUnique = (rule, value = '', callback) => {
  25. const { onCheckUniqueName, loginUser: {id} } = this.props
  26. const data = {
  27. username: value,
  28. id
  29. }
  30. onCheckUniqueName('user', data,
  31. () => {
  32. callback()
  33. }, (err) => {
  34. callback(err)
  35. })
  36. }
  37. private submit = () => {
  38. const { onUpdateProfile, loginUser: {id} } = this.props
  39. const values = this.props.form.getFieldsValue()
  40. const {name, description, department} = values
  41. onUpdateProfile(id, name, description, department, (data) => {
  42. message.success(data.header && data.header.msg)
  43. })
  44. }
  45. public componentDidMount () {
  46. const { name, description, department } = this.props.loginUser
  47. this.props.form.setFieldsValue({name, description, department })
  48. }
  49. public uploadAvatarSuccessCallback = (path) => {
  50. const { onUploadAvatarSuccess, loginUser } = this.props
  51. const newLoginUser = {...loginUser, ...{avatar: path}}
  52. if (onUploadAvatarSuccess) {
  53. onUploadAvatarSuccess(path)
  54. }
  55. localStorage.setItem('loginUser', JSON.stringify(newLoginUser))
  56. }
  57. public render () {
  58. const {getFieldDecorator} = this.props.form
  59. const { id, avatar } = this.props.loginUser
  60. const commonFormItemStyle = {
  61. labelCol: { span: 4 },
  62. wrapperCol: { span: 18 }
  63. }
  64. return (
  65. <Box>
  66. <Box.Header>
  67. <Box.Title>
  68. <Breadcrumb className={utilStyles.breadcrumb}>
  69. <Breadcrumb.Item>
  70. <Link to="/account/profile">
  71. <Icon type="bars" />个人信息
  72. </Link>
  73. </Breadcrumb.Item>
  74. </Breadcrumb>
  75. </Box.Title>
  76. </Box.Header>
  77. <Box.Body>
  78. <div className={styles.container}>
  79. <div className={styles.uploadWrapper}>
  80. <UploadAvatar type="profile" xhrParams={{id, callback: this.uploadAvatarSuccessCallback}} path={avatar}/>
  81. </div>
  82. <hr/>
  83. <div className={styles.form}>
  84. <Form
  85. className={styles.formView}
  86. >
  87. <Row>
  88. <Col>
  89. <FormItem
  90. className={styles.hide}
  91. {...commonFormItemStyle}
  92. >
  93. {getFieldDecorator('id', {})(
  94. <Input />
  95. )}
  96. </FormItem>
  97. <FormItem
  98. {...commonFormItemStyle}
  99. label="姓名"
  100. >
  101. {getFieldDecorator('name', {
  102. initialValue: '',
  103. rules: [{ required: true }, {validator: this.checkNameUnique}]
  104. })(
  105. <Input size="large"/>
  106. )}
  107. </FormItem>
  108. </Col>
  109. <Col>
  110. <FormItem
  111. {...commonFormItemStyle}
  112. label="描述"
  113. >
  114. {getFieldDecorator('description', {
  115. initialValue: ''
  116. })(
  117. <Input />
  118. )}
  119. </FormItem>
  120. </Col>
  121. <Col>
  122. <FormItem
  123. {...commonFormItemStyle}
  124. label="部门"
  125. >
  126. {getFieldDecorator('department', {
  127. initialValue: ''
  128. })(
  129. <Input size="large"/>
  130. )}
  131. </FormItem>
  132. </Col>
  133. <Col offset={4}>
  134. <Button size="large" type="primary" onClick={this.submit}>保存设置</Button>
  135. </Col>
  136. </Row>
  137. </Form>
  138. </div>
  139. </div>
  140. </Box.Body>
  141. </Box>
  142. )
  143. }
  144. }
  145. export function mapDispatchToProps (dispatch) {
  146. return {
  147. onUpdateProfile: (id, name, description, department, resolve) => dispatch(updateProfile(id, name, description, department, resolve)),
  148. onCheckUniqueName: (pathname, data, resolve, reject) => dispatch(checkNameUniqueAction(pathname, data, resolve, reject)),
  149. onUploadAvatarSuccess: (path) => dispatch(uploadAvatarSuccess(path))
  150. }
  151. }
  152. const mapStateToProps = createStructuredSelector({
  153. loginUser: makeSelectLoginUser()
  154. })
  155. const withConnect = connect(mapStateToProps, mapDispatchToProps)
  156. export default compose(
  157. withConnect
  158. )(Form.create()(Profile))