index.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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, { ChangeEvent, FormEvent } from 'react'
  21. import { connect } from 'react-redux'
  22. import Helmet from 'react-helmet'
  23. import { createStructuredSelector } from 'reselect'
  24. import { compose } from 'redux'
  25. import injectReducer from 'utils/injectReducer'
  26. import injectSaga from 'utils/injectSaga'
  27. import reducer from './reducer'
  28. import saga from './sagas'
  29. import { message } from 'antd'
  30. import RegisterForm from './RegisterForm'
  31. import SendEmailTips from './SendEmailTips'
  32. import { checkNameAction } from '../App/actions'
  33. import { signup, sendMailAgain } from './actions'
  34. import { makeSelectSignupLoading } from './selectors'
  35. import { RouteComponentProps } from 'react-router-dom'
  36. import styles from 'containers/Login/Login.less'
  37. interface IRegisterProps {
  38. signupLoading: boolean
  39. onSendEmailOnceMore: (email: string, resolve?: (res: any) => any) => any
  40. onSignup: (username: string, email: string, password: string, resolve?: (res: any) => any) => any
  41. onCheckName: (id: number, name: string, type: string, param?: any, resolve?: (res: any) => any, reject?: (error: any) => any) => any
  42. }
  43. interface IRegisterStates {
  44. step: string
  45. username: string
  46. email: string
  47. password: string
  48. password2: string
  49. }
  50. export class Register extends React.PureComponent<IRegisterProps & RouteComponentProps, IRegisterStates> {
  51. constructor (props) {
  52. super(props)
  53. this.state = {
  54. step: 'first',
  55. username: '',
  56. email: '',
  57. password: '',
  58. password2: ''
  59. }
  60. }
  61. private changeUsername = (e: ChangeEvent<HTMLInputElement>) => {
  62. this.setState({
  63. username: e.target.value.trim()
  64. })
  65. }
  66. private changeEmail = (e: ChangeEvent<HTMLInputElement>) => {
  67. this.setState({
  68. email: e.target.value.trim()
  69. })
  70. }
  71. private changePassword = (e: ChangeEvent<HTMLInputElement>) => {
  72. this.setState({
  73. password: e.target.value.trim()
  74. })
  75. }
  76. private changePassword2 = (e: ChangeEvent<HTMLInputElement>) => {
  77. this.setState({
  78. password2: e.target.value.trim()
  79. })
  80. }
  81. private signUp = (e: FormEvent<HTMLFormElement>) => {
  82. e.preventDefault()
  83. const { onSignup } = this.props
  84. const { username, email, password, password2} = this.state
  85. const emailRep = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/
  86. if (username && password && email && password2) {
  87. if (!emailRep.test(email)) {
  88. message.error('无效的邮箱地址')
  89. return
  90. }
  91. if (password.length < 6 || password.length > 20) {
  92. message.error('密码长度为6-20位')
  93. return
  94. }
  95. if (password !== password2) {
  96. message.error('两次输入的密码不一致')
  97. return
  98. }
  99. onSignup(username, email, password, () => {
  100. this.setState({
  101. step: 'second'
  102. })
  103. })
  104. }
  105. }
  106. private goBack = () => {
  107. this.setState({
  108. step: 'first'
  109. })
  110. }
  111. private toLogin = () => {
  112. const { history } = this.props
  113. history.replace('/login')
  114. }
  115. private sendEmailOnceMore = () => {
  116. const { onSendEmailOnceMore } = this.props
  117. const { email } = this.state
  118. onSendEmailOnceMore(email, (res) => {
  119. message.success(res)
  120. })
  121. }
  122. public render () {
  123. const { step, email } = this.state
  124. const { onCheckName, signupLoading} = this.props
  125. const firstStep = (
  126. <div className={styles.window}>
  127. <Helmet title="Register" />
  128. <RegisterForm
  129. username={this.state.username}
  130. email={this.state.email}
  131. password={this.state.password}
  132. password2={this.state.password2}
  133. loading={signupLoading}
  134. onChangeUsername={this.changeUsername}
  135. onChangeEmail={this.changeEmail}
  136. onChangePassword={this.changePassword}
  137. onChangePassword2={this.changePassword2}
  138. onCheckName={onCheckName}
  139. onSignup={this.signUp}
  140. />
  141. <p className={styles.tips}>
  142. <span>已有davinci账号, </span>
  143. <a href="javascript:;" onClick={this.toLogin}>点击登录</a>
  144. </p>
  145. </div>
  146. )
  147. const secondStep = (
  148. <div className={styles.window}>
  149. <Helmet title="Register" />
  150. <SendEmailTips
  151. email={email}
  152. goBack={this.goBack}
  153. sendEmailOnceMore={this.sendEmailOnceMore}
  154. />
  155. </div>
  156. )
  157. return step === 'first' ? firstStep : secondStep
  158. }
  159. }
  160. const mapStateToProps = createStructuredSelector({
  161. signupLoading: makeSelectSignupLoading()
  162. })
  163. export function mapDispatchToProps (dispatch) {
  164. return {
  165. onSignup: (username, email, password, resolve) => dispatch(signup(username, email, password, resolve)),
  166. onCheckName: (id, name, type, params, resolve, reject) => dispatch(checkNameAction(id, name, type, params, resolve, reject)),
  167. onSendEmailOnceMore: (email, resolve) => dispatch(sendMailAgain(email, resolve))
  168. }
  169. }
  170. const withConnect = connect<{}, {}, IRegisterProps>(mapStateToProps, mapDispatchToProps)
  171. const withReducer = injectReducer({ key: 'register', reducer })
  172. const withSaga = injectSaga({ key: 'register', saga })
  173. export default compose(
  174. withReducer,
  175. withSaga,
  176. withConnect
  177. )(Register)