JoinOrganization.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import React, { ChangeEvent, FormEvent } from 'react'
  2. import { compose } from 'redux'
  3. import { connect } from 'react-redux'
  4. import Helmet from 'react-helmet'
  5. import LoginForm from 'containers/Login/LoginForm'
  6. import { joinOrganization, login } from '../App/actions'
  7. import { createStructuredSelector } from 'reselect'
  8. import { makeSelectLoginLoading } from '../App/selectors'
  9. import styles from 'containers/Login/Login.less'
  10. interface IJoinOrganizationProps {
  11. onJoinOrganization: (token?: string, resolve?: (res?: {id?: number}) => any, reject?: (err?: string) => any) => any
  12. loginLoading: boolean
  13. onLogin: (username: string, password: string, resolve?: () => any) => any
  14. onLogged: () => any
  15. }
  16. interface IJoinOrganizationStates {
  17. username: string
  18. password: string
  19. needLogin: boolean
  20. }
  21. export class JoinOrganization extends React.PureComponent <IJoinOrganizationProps, IJoinOrganizationStates> {
  22. constructor (props) {
  23. super(props)
  24. this.state = {
  25. username: '',
  26. password: '',
  27. needLogin: false
  28. }
  29. }
  30. public componentWillMount () {
  31. this.joinOrganization()
  32. }
  33. private changeUsername = (e: ChangeEvent<HTMLInputElement>) => {
  34. this.setState({
  35. username: e.target.value.trim()
  36. })
  37. }
  38. private changePassword = (e: ChangeEvent<HTMLInputElement>) => {
  39. this.setState({
  40. password: e.target.value
  41. })
  42. }
  43. private joinOrganization = () => {
  44. const {onJoinOrganization} = this.props
  45. const token = this.getParamsByLocation('token')
  46. if (token) {
  47. onJoinOrganization(token, (res) => {
  48. if (res && res.id) {
  49. const path = `${window.location.protocol}//${window.location.host}/#/account/organization/${res.id}`
  50. location.replace(path)
  51. }
  52. }, (err) => {
  53. this.setState({
  54. needLogin: true
  55. })
  56. })
  57. }
  58. }
  59. private doLogin = (e: FormEvent<HTMLFormElement>) => {
  60. e.preventDefault()
  61. const {onLogin, onJoinOrganization} = this.props
  62. const {username, password} = this.state
  63. const token = this.getParamsByLocation('token')
  64. if (username && password) {
  65. onLogin(username, password, () => {
  66. if (token) {
  67. onJoinOrganization(token, (res) => {
  68. if (res && res.id) {
  69. const path = `${window.location.protocol}//${window.location.host}/#/account/organization/${res.id}`
  70. location.replace(path)
  71. }
  72. })
  73. }
  74. })
  75. }
  76. }
  77. private isEmptyOrNull = (str) => str == null || str === undefined || str === '' || str === 'null' || typeof str === 'undefined'
  78. private getParamsByLocation = (name) => {
  79. let values = decodeURIComponent((window.location.search.match(RegExp(`[?|&|/]${name}=([^\\&|?&]+)`)) || [void 0, null])[1])
  80. if (this.isEmptyOrNull(values)) {
  81. values = decodeURIComponent((window.location.hash.match(RegExp(`[?|&|/]${name}=([^\&|?&]+)`)) || [void 0, null])[1])
  82. }
  83. return this.isEmptyOrNull(values) || values === 'null' ? '' : values
  84. }
  85. public render () {
  86. const {username, password} = this.state
  87. const {loginLoading} = this.props
  88. return this.state.needLogin
  89. ? (
  90. <div className={styles.window}>
  91. <Helmet title="Login - Join Organization" />
  92. <LoginForm
  93. username={username}
  94. password={password}
  95. loading={loginLoading}
  96. onChangeUsername={this.changeUsername}
  97. onChangePassword={this.changePassword}
  98. onLogin={this.doLogin}
  99. />
  100. </div>
  101. )
  102. : (
  103. <div className={styles.window}>
  104. <Helmet title="Join Organization" />
  105. <h1 className={styles.joinOrganizationLoadingContent}>
  106. {this.state.needLogin ? '加入组织中,请稍候…' : ''}
  107. </h1>
  108. </div>
  109. )
  110. }
  111. }
  112. const mapStateToProps = createStructuredSelector({
  113. loginLoading: makeSelectLoginLoading()
  114. })
  115. export function mapDispatchToProps (dispatch) {
  116. return {
  117. onLogin: (username, password, resolve) => dispatch(login(username, password, resolve)),
  118. onJoinOrganization: (token, resolve, reject) => dispatch(joinOrganization(token, resolve, reject))
  119. }
  120. }
  121. const withConnect = connect<{}, {}, IJoinOrganizationProps>(mapStateToProps, mapDispatchToProps)
  122. export default compose(
  123. withConnect
  124. )(JoinOrganization)