index.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import React, { createRef, RefObject } from 'react'
  2. import { compose } from 'redux'
  3. import { connect } from 'react-redux'
  4. import { Link } from 'react-router-dom'
  5. import { Icon, message, Breadcrumb } from 'antd'
  6. import FormType, { FormComponentProps } from 'antd/lib/form/Form'
  7. import Box from 'components/Box'
  8. const styles = require('../Profile/profile.less')
  9. const utilStyles = require('assets/less/util.less')
  10. import ResetPasswordForm from './ResetPasswordForm'
  11. import { changeUserPassword } from '../App/actions'
  12. import { makeSelectLoginUser } from '../App/selectors'
  13. import { createStructuredSelector } from 'reselect'
  14. interface IResetPasswordProps {
  15. type: string
  16. loginUser: any,
  17. onChangeUserPassword: (user: IUser, resolve: () => any, reject: (msg: string) => any) => any
  18. }
  19. interface IUser {
  20. id: number
  21. oldPassword?: string
  22. password?: string
  23. }
  24. export class ResetPassword extends React.PureComponent<IResetPasswordProps & FormComponentProps> {
  25. private resetPasswordForm: FormType
  26. private refHandler = {
  27. resetPasswordForm: (ref) => this.resetPasswordForm = ref
  28. }
  29. public componentWillMount () {
  30. const {id} = this.props.loginUser
  31. this.forceUpdate(() => {
  32. this.resetPasswordForm.props.form.setFieldsValue({id})
  33. })
  34. }
  35. private submit = () => {
  36. this.resetPasswordForm.props.form.validateFieldsAndScroll((err, values) => {
  37. if (!err) {
  38. this.props.onChangeUserPassword(values, () => {
  39. message.success('success')
  40. }, (msg) => {
  41. message.error(msg)
  42. })
  43. }
  44. })
  45. }
  46. public render () {
  47. return (
  48. <Box>
  49. <Box.Header>
  50. <Box.Title>
  51. <Breadcrumb className={utilStyles.breadcrumb}>
  52. <Breadcrumb.Item>
  53. <Link to="/account/resetPassword">
  54. <Icon type="bars" />修改密码
  55. </Link>
  56. </Breadcrumb.Item>
  57. </Breadcrumb>
  58. </Box.Title>
  59. </Box.Header>
  60. <Box.Body>
  61. <div className={styles.container}>
  62. <ResetPasswordForm
  63. wrappedComponentRef={this.refHandler.resetPasswordForm}
  64. submit={this.submit}
  65. />
  66. </div>
  67. </Box.Body>
  68. </Box>
  69. )
  70. }
  71. }
  72. const mapStateToProps = createStructuredSelector({
  73. loginUser: makeSelectLoginUser()
  74. })
  75. function mapDispatchToProps (dispatch) {
  76. return {
  77. onChangeUserPassword: (user, resolve, reject) => dispatch(changeUserPassword(user, resolve, reject))
  78. }
  79. }
  80. const withConnect = connect<{}, {}, IResetPasswordProps & FormComponentProps>(mapStateToProps, mapDispatchToProps)
  81. export default compose(
  82. withConnect
  83. )(ResetPassword)